fix problem from last commit

This commit is contained in:
Thomas Fussell 2014-06-13 15:05:24 -04:00
parent 21163cfb23
commit ff84734e2d
11 changed files with 138 additions and 86 deletions

View File

@ -139,7 +139,7 @@ public:
cell &operator=(const time &value); cell &operator=(const time &value);
cell &operator=(const datetime &value); cell &operator=(const datetime &value);
bool operator==(const cell &comparand) const { return d_ == comparand.d_; } bool operator==(const cell &comparand) const;
bool operator==(std::nullptr_t) const; bool operator==(std::nullptr_t) const;
bool operator==(bool comparand) const; bool operator==(bool comparand) const;
bool operator==(int comparand) const; bool operator==(int comparand) const;

View File

@ -29,14 +29,14 @@ namespace xlnt {
struct date struct date
{ {
static date today(); static date today();
static date from_number(long double number, int base_year = 1900); static date from_number(int days_since_base_year, int base_year = 1900);
date(int year, int month, int day) date(int year, int month, int day)
: year(year), month(month), day(day) : year(year), month(month), day(day)
{ {
} }
double to_number(int base_year = 1900) const; int to_number(int base_year = 1900) const;
bool operator==(const date &comparand) const; bool operator==(const date &comparand) const;
int year; int year;
@ -47,7 +47,7 @@ struct date
struct time struct time
{ {
static time now(); static time now();
static time from_number(long double number, int base_year = 1900); static time from_number(long double number);
time(int hour = 0, int minute = 0, int second = 0, int microsecond = 0) time(int hour = 0, int minute = 0, int second = 0, int microsecond = 0)
: hour(hour), minute(minute), second(second), microsecond(microsecond) : hour(hour), minute(minute), second(second), microsecond(microsecond)
@ -55,7 +55,7 @@ struct time
} }
explicit time(const std::string &time_string); explicit time(const std::string &time_string);
double to_number(int base_year = 1900) const; double to_number() const;
bool operator==(const time &comparand) const; bool operator==(const time &comparand) const;
int hour; int hour;

View File

@ -23,6 +23,7 @@
// @author: see AUTHORS file // @author: see AUTHORS file
#pragma once #pragma once
#include <memory>
#include <string> #include <string>
#include <unordered_map> #include <unordered_map>
#include <utility> #include <utility>
@ -161,7 +162,7 @@ public:
private: private:
friend class worksheet; friend class worksheet;
bool get_already_saved() const; bool get_already_saved() const;
detail::workbook_impl *d_; std::unique_ptr<detail::workbook_impl> d_;
}; };
} // namespace xlnt } // namespace xlnt

View File

@ -287,8 +287,7 @@ bool cell::operator==(const time &comparand) const
return false; return false;
} }
auto base_year = worksheet(d_->parent_).get_parent().get_base_year(); return time::from_number(d_->numeric_value) == comparand;
return time::from_number(d_->numeric_value, base_year) == comparand;
} }
bool cell::operator==(const date &comparand) const bool cell::operator==(const date &comparand) const
@ -299,7 +298,7 @@ bool cell::operator==(const date &comparand) const
} }
auto base_year = worksheet(d_->parent_).get_parent().get_base_year(); auto base_year = worksheet(d_->parent_).get_parent().get_base_year();
return date::from_number(d_->numeric_value, base_year) == comparand; return date::from_number((int)d_->numeric_value, base_year) == comparand;
} }
bool cell::operator==(const datetime &comparand) const bool cell::operator==(const datetime &comparand) const
@ -313,6 +312,43 @@ bool cell::operator==(const datetime &comparand) const
return datetime::from_number(d_->numeric_value, base_year) == comparand; return datetime::from_number(d_->numeric_value, base_year) == comparand;
} }
bool cell::operator==(const cell &comparand) const
{
if(comparand == nullptr)
{
return d_ == nullptr;
}
if(comparand.get_data_type() != get_data_type())
{
return false;
}
switch(get_data_type())
{
case type::boolean:
return d_->numeric_value == comparand.d_->numeric_value;
case type::error:
return d_->string_value == comparand.d_->string_value;
case type::string:
return d_->string_value == comparand.d_->string_value;
case type::formula:
return d_->string_value == comparand.d_->string_value;
case type::null:
return true;
case type::numeric:
if(is_date() && comparand.is_date())
{
auto base_year = worksheet(d_->parent_).get_parent().get_base_year();
auto other_base_year = worksheet(comparand.d_->parent_).get_parent().get_base_year();
return date::from_number((int)d_->numeric_value, base_year) == date::from_number((int)comparand.d_->numeric_value, other_base_year);
}
return d_->numeric_value == comparand.d_->numeric_value;
}
return false;
}
bool operator==(int comparand, const xlnt::cell &cell) bool operator==(int comparand, const xlnt::cell &cell)
{ {
return cell == comparand; return cell == comparand;
@ -481,7 +517,8 @@ cell &cell::operator=(const time &value)
cell &cell::operator=(const date &value) cell &cell::operator=(const date &value)
{ {
d_->type_ = type::numeric; d_->type_ = type::numeric;
d_->numeric_value = value.to_number(); auto base_year = worksheet(d_->parent_).get_parent().get_base_year();
d_->numeric_value = value.to_number(base_year);
d_->is_date_ = true; d_->is_date_ = true;
return *this; return *this;
} }
@ -489,7 +526,8 @@ cell &cell::operator=(const date &value)
cell &cell::operator=(const datetime &value) cell &cell::operator=(const datetime &value)
{ {
d_->type_ = type::numeric; d_->type_ = type::numeric;
d_->numeric_value = value.to_number(); auto base_year = worksheet(d_->parent_).get_parent().get_base_year();
d_->numeric_value = value.to_number(base_year);
d_->is_date_ = true; d_->is_date_ = true;
return *this; return *this;
} }

View File

@ -5,7 +5,7 @@
namespace xlnt { namespace xlnt {
time time::from_number(long double raw_time, int base_year) time time::from_number(long double raw_time)
{ {
double integer_part; double integer_part;
double fractional_part = std::modf((double)raw_time, &integer_part); double fractional_part = std::modf((double)raw_time, &integer_part);
@ -35,49 +35,45 @@ time time::from_number(long double raw_time, int base_year)
return time(hour, minute, second, microsecond); return time(hour, minute, second, microsecond);
} }
date date::from_number(long double number, int base_year) date date::from_number(int days_since_base_year, int base_year)
{ {
int year = (int)number / 365; date result(0, 0, 0);
number -= year * 365;
int month = (int)number / 30; if(base_year == 1904)
number -= month * 30; {
int day = (int)number; days_since_base_year += 1462;
return date(year + base_year, month, day + 1); }
if(days_since_base_year == 60)
{
result.day = 29;
result.month = 2;
result.year = 1900;
}
else if(days_since_base_year < 60)
{
days_since_base_year++;
}
int l = days_since_base_year + 68569 + 2415019;
int n = int((4 * l) / 146097);
l = l - int((146097 * n + 3) / 4);
int i = int((4000 * (l + 1)) / 1461001);
l = l - int((1461 * i) / 4) + 31;
int j = int((80 * l) / 2447);
result.day = l - int((2447 * j) / 80);
l = int(j / 11);
result.month = j + 2 - (12 * l);
result.year = 100 * (n - 49) + i + l;
return result;
} }
datetime datetime::from_number(long double raw_time, int base_year) datetime datetime::from_number(long double raw_time, int base_year)
{ {
double integer_part; auto date_part = date::from_number((int)raw_time, base_year);
double fractional_part = std::modf((double)raw_time, &integer_part); auto time_part = time::from_number(raw_time);
fractional_part *= 24; return datetime(date_part.year, date_part.month, date_part.day, time_part.hour, time_part.minute, time_part.second, time_part.microsecond);
int hour = (int)fractional_part;
fractional_part = 60 * (fractional_part - hour);
int minute = (int)fractional_part;
fractional_part = 60 * (fractional_part - minute);
int second = (int)fractional_part;
fractional_part = 1000000 * (fractional_part - second);
int microsecond = (int)fractional_part;
if(microsecond == 999999 && fractional_part - microsecond > 0.5)
{
microsecond = 0;
second += 1;
if(second == 60)
{
second = 0;
minute += 1;
if(minute == 60)
{
minute = 0;
hour += 1;
}
}
}
int year = (int)integer_part / 365;
integer_part -= year * 365;
int month = (int)integer_part / 30;
integer_part -= month * 30;
int day = (int)integer_part;
return datetime(year + base_year, month, day + 1, hour, minute, second, microsecond);
} }
bool date::operator==(const date &comparand) const bool date::operator==(const date &comparand) const
@ -122,7 +118,7 @@ time::time(const std::string &time_string) : hour(0), minute(0), second(0), micr
} }
} }
double time::to_number(int base_year) const double time::to_number() const
{ {
double number = microsecond; double number = microsecond;
number /= 1000000; number /= 1000000;
@ -135,38 +131,49 @@ double time::to_number(int base_year) const
return number; return number;
} }
double date::to_number(int base_year) const int date::to_number(int base_year) const
{ {
double number = (day - 1) + month * 30 + (year - base_year) * 365; if(day == 29 && month == 2 && year == 1900)
return number; {
return 60;
}
int days_since_1900 = int((1461 * (year + 4800 + int((month - 14) / 12))) / 4)
+ int((367 * (month - 2 - 12 * ((month - 14) / 12))) / 12)
- int((3 * (int((year + 4900 + int((month - 14) / 12)) / 100))) / 4)
+ day - 2415019 - 32075;
if(days_since_1900 <= 60)
{
days_since_1900--;
}
if(base_year == 1904)
{
return days_since_1900 - 1461;
}
return days_since_1900;
} }
double datetime::to_number(int base_year) const double datetime::to_number(int base_year) const
{ {
double number = microsecond; return date(year, month, day).to_number(base_year)
number /= 1000000; + time(hour, minute, second, microsecond).to_number();
number += second;
number /= 60;
number += minute;
number /= 60;
number += hour;
number /= 24;
number += (day - 1) + month * 30 + (year - base_year) * 365;
return number;
} }
date date::today() date date::today()
{ {
std::time_t raw_time = std::time(0); std::time_t raw_time = std::time(0);
std::tm now = *std::localtime(&raw_time); std::tm now = *std::localtime(&raw_time);
return date(now.tm_year, now.tm_mon + 1, now.tm_mday); return date(1900 + now.tm_year, now.tm_mon + 1, now.tm_mday);
} }
datetime datetime::now() datetime datetime::now()
{ {
std::time_t raw_time = std::time(0); std::time_t raw_time = std::time(0);
std::tm now = *std::localtime(&raw_time); std::tm now = *std::localtime(&raw_time);
return datetime(now.tm_year, now.tm_mon + 1, now.tm_mday, now.tm_hour, now.tm_min, now.tm_sec); return datetime(1900 + now.tm_year, now.tm_mon + 1, now.tm_mday, now.tm_hour, now.tm_min, now.tm_sec);
} }
} // namespace xlnt } // namespace xlnt

View File

@ -8,6 +8,8 @@ namespace detail {
struct workbook_impl struct workbook_impl
{ {
workbook_impl(optimization o); workbook_impl(optimization o);
workbook_impl &operator=(const workbook_impl &) = delete;
workbook_impl(const workbook_impl &) = delete;
bool already_saved_; bool already_saved_;
bool optimized_read_; bool optimized_read_;
bool optimized_write_; bool optimized_write_;

View File

@ -34,6 +34,13 @@ struct worksheet_impl
title_ = other.title_; title_ = other.title_;
freeze_panes_ = other.freeze_panes_; freeze_panes_ = other.freeze_panes_;
cell_map_ = other.cell_map_; cell_map_ = other.cell_map_;
for(auto &row : cell_map_)
{
for(auto &cell : row.second)
{
cell.second.parent_ = this;
}
}
relationships_ = other.relationships_; relationships_ = other.relationships_;
page_setup_ = other.page_setup_; page_setup_ = other.page_setup_;
auto_filter_ = other.auto_filter_; auto_filter_ = other.auto_filter_;

View File

@ -188,10 +188,8 @@ worksheet workbook::create_sheet()
title = "Sheet" + std::to_string(++index); title = "Sheet" + std::to_string(++index);
} }
d_->worksheets_.emplace_back(this, title); d_->worksheets_.push_back(detail::worksheet_impl(this, title));
worksheet ws(&d_->worksheets_.back()); return worksheet(&d_->worksheets_.back());
return ws;
} }
void workbook::add_sheet(xlnt::worksheet worksheet) void workbook::add_sheet(xlnt::worksheet worksheet)
@ -551,7 +549,7 @@ bool workbook::save(const std::string &filename)
bool workbook::operator==(const workbook &rhs) const bool workbook::operator==(const workbook &rhs) const
{ {
return d_ == rhs.d_; return d_.get() == rhs.d_.get();
} }
} }

View File

@ -21,7 +21,7 @@ int main( int argc, char *argv[] ) {
return status; return status;
} }
bool suite_test_cell_init = false; bool suite_test_cell_init = false;
#include "/Users/thomas/Development/xlnt/tests/test_cell.hpp" #include "C:\Users\taf656\Development\xlnt\tests\test_cell.hpp"
static test_cell suite_test_cell; static test_cell suite_test_cell;
@ -238,7 +238,7 @@ public:
void runTest() { suite_test_cell.test_is_not_date_color_format(); } void runTest() { suite_test_cell.test_is_not_date_color_format(); }
} testDescription_suite_test_cell_test_is_not_date_color_format; } testDescription_suite_test_cell_test_is_not_date_color_format;
#include "/Users/thomas/Development/xlnt/tests/test_chart.hpp" #include "C:\Users\taf656\Development\xlnt\tests\test_chart.hpp"
static test_chart suite_test_chart; static test_chart suite_test_chart;
@ -329,7 +329,7 @@ public:
void runTest() { suite_test_chart.test_write_chart_scatter(); } void runTest() { suite_test_chart.test_write_chart_scatter(); }
} testDescription_suite_test_chart_test_write_chart_scatter; } testDescription_suite_test_chart_test_write_chart_scatter;
#include "/Users/thomas/Development/xlnt/tests/test_dump.hpp" #include "C:\Users\taf656\Development\xlnt\tests\test_dump.hpp"
static test_dump suite_test_dump; static test_dump suite_test_dump;
@ -366,7 +366,7 @@ public:
void runTest() { suite_test_dump.test_append_after_save(); } void runTest() { suite_test_dump.test_append_after_save(); }
} testDescription_suite_test_dump_test_append_after_save; } testDescription_suite_test_dump_test_append_after_save;
#include "/Users/thomas/Development/xlnt/tests/test_named_range.hpp" #include "C:\Users\taf656\Development\xlnt\tests\test_named_range.hpp"
static test_named_range suite_test_named_range; static test_named_range suite_test_named_range;
@ -457,7 +457,7 @@ public:
void runTest() { suite_test_named_range.test_can_be_saved(); } void runTest() { suite_test_named_range.test_can_be_saved(); }
} testDescription_suite_test_named_range_test_can_be_saved; } testDescription_suite_test_named_range_test_can_be_saved;
#include "/Users/thomas/Development/xlnt/tests/test_number_format.hpp" #include "C:\Users\taf656\Development\xlnt\tests\test_number_format.hpp"
static test_number_format suite_test_number_format; static test_number_format suite_test_number_format;
@ -560,7 +560,7 @@ public:
void runTest() { suite_test_number_format.test_mac_date(); } void runTest() { suite_test_number_format.test_mac_date(); }
} testDescription_suite_test_number_format_test_mac_date; } testDescription_suite_test_number_format_test_mac_date;
#include "/Users/thomas/Development/xlnt/tests/test_props.hpp" #include "C:\Users\taf656\Development\xlnt\tests\test_props.hpp"
static test_props suite_test_props; static test_props suite_test_props;
@ -603,7 +603,7 @@ public:
void runTest() { suite_test_props.test_write_properties_app(); } void runTest() { suite_test_props.test_write_properties_app(); }
} testDescription_suite_test_props_test_write_properties_app; } testDescription_suite_test_props_test_write_properties_app;
#include "/Users/thomas/Development/xlnt/tests/test_read.hpp" #include "C:\Users\taf656\Development\xlnt\tests\test_read.hpp"
static test_read suite_test_read; static test_read suite_test_read;
@ -736,7 +736,7 @@ public:
void runTest() { suite_test_read.test_read_date_value(); } void runTest() { suite_test_read.test_read_date_value(); }
} testDescription_suite_test_read_test_read_date_value; } testDescription_suite_test_read_test_read_date_value;
#include "/Users/thomas/Development/xlnt/tests/test_strings.hpp" #include "C:\Users\taf656\Development\xlnt\tests\test_strings.hpp"
static test_strings suite_test_strings; static test_strings suite_test_strings;
@ -767,7 +767,7 @@ public:
void runTest() { suite_test_strings.test_formatted_string_table(); } void runTest() { suite_test_strings.test_formatted_string_table(); }
} testDescription_suite_test_strings_test_formatted_string_table; } testDescription_suite_test_strings_test_formatted_string_table;
#include "/Users/thomas/Development/xlnt/tests/test_style.hpp" #include "C:\Users\taf656\Development\xlnt\tests\test_style.hpp"
static test_style suite_test_style; static test_style suite_test_style;
@ -864,7 +864,7 @@ public:
void runTest() { suite_test_style.test_read_cell_style(); } void runTest() { suite_test_style.test_read_cell_style(); }
} testDescription_suite_test_style_test_read_cell_style; } testDescription_suite_test_style_test_read_cell_style;
#include "/Users/thomas/Development/xlnt/tests/test_theme.hpp" #include "C:\Users\taf656\Development\xlnt\tests\test_theme.hpp"
static test_theme suite_test_theme; static test_theme suite_test_theme;
@ -877,7 +877,7 @@ public:
void runTest() { suite_test_theme.test_write_theme(); } void runTest() { suite_test_theme.test_write_theme(); }
} testDescription_suite_test_theme_test_write_theme; } testDescription_suite_test_theme_test_write_theme;
#include "/Users/thomas/Development/xlnt/tests/test_workbook.hpp" #include "C:\Users\taf656\Development\xlnt\tests\test_workbook.hpp"
static test_workbook suite_test_workbook; static test_workbook suite_test_workbook;
@ -1004,7 +1004,7 @@ public:
void runTest() { suite_test_workbook.test_write_regular_float(); } void runTest() { suite_test_workbook.test_write_regular_float(); }
} testDescription_suite_test_workbook_test_write_regular_float; } testDescription_suite_test_workbook_test_write_regular_float;
#include "/Users/thomas/Development/xlnt/tests/test_worksheet.hpp" #include "C:\Users\taf656\Development\xlnt\tests\test_worksheet.hpp"
static test_worksheet suite_test_worksheet; static test_worksheet suite_test_worksheet;
@ -1173,7 +1173,7 @@ public:
void runTest() { suite_test_worksheet.test_printer_settings(); } void runTest() { suite_test_worksheet.test_printer_settings(); }
} testDescription_suite_test_worksheet_test_printer_settings; } testDescription_suite_test_worksheet_test_printer_settings;
#include "/Users/thomas/Development/xlnt/tests/test_write.hpp" #include "C:\Users\taf656\Development\xlnt\tests\test_write.hpp"
static test_write suite_test_write; static test_write suite_test_write;

View File

@ -57,7 +57,7 @@ public:
std::vector<xlnt::date> current_row; std::vector<xlnt::date> current_row;
for(std::size_t x = 0; x < letters.size(); x++) for(std::size_t x = 0; x < letters.size(); x++)
{ {
current_row.push_back(xlnt::date(2010 + x, 5, row + 1)); current_row.push_back(xlnt::date(2010 + (int)x, 5, row + 1));
} }
ws.append(current_row); ws.append(current_row);
} }

View File

@ -160,7 +160,6 @@ public:
void test_read_date_value() void test_read_date_value()
{ {
TS_SKIP("something bad is happening here...");
//auto path = PathHelper::GetDataDirectory() + "/genuine/empty-with-styles.xlsx"; //auto path = PathHelper::GetDataDirectory() + "/genuine/empty-with-styles.xlsx";
//wb_with_styles.load(path); //wb_with_styles.load(path);
//worksheet_with_styles = wb_with_styles.get_sheet_by_name("Sheet1"); //worksheet_with_styles = wb_with_styles.get_sheet_by_name("Sheet1");