This commit is contained in:
Elena.Subbotina
2023-02-28 12:48:47 +03:00
parent cdb72bab80
commit ffe2806bc8
2 changed files with 63 additions and 46 deletions

View File

@ -191,14 +191,22 @@ void CSVWriter::Close()
impl_->Close(); impl_->Close();
} }
//--------------------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------------------
static std::wstring replace_unwanted(boost::wsmatch const & what)
{
return L"";
}
int CSVWriter::Impl::detect_format(std::wstring & format_code) int CSVWriter::Impl::detect_format(std::wstring & format_code)
{ {
if (format_code.empty()) return SimpleTypes::Spreadsheet::celltypeStr; if (format_code.empty()) return SimpleTypes::Spreadsheet::celltypeStr;
boost::wregex re_unwanted(L"([\"'])(.+?)\\1");
std::wstring strFormatCode = boost::regex_replace(format_code, re_unwanted, &replace_unwanted, boost::match_default | boost::format_all);
//find [$<Currency String>-<language info>]. //find [$<Currency String>-<language info>].
boost::wregex re(L"(?:\\[)(?:\\$)(\\S+)?\-(\\S+)(?:\\])"); boost::wregex re(L"(?:\\[)(?:\\$)(\\S+)?\-(\\S+)(?:\\])");
boost::wsmatch result; boost::wsmatch result;
bool b = boost::regex_search(format_code, result, re); bool b = boost::regex_search(strFormatCode, result, re);
std::wstring currency_str; std::wstring currency_str;
@ -223,30 +231,37 @@ int CSVWriter::Impl::detect_format(std::wstring & format_code)
if (false == format_code.empty()) //any if (false == format_code.empty()) //any
{ {
std::wstring tmp = format_code; boost::wregex re1(L"([mMhHs{2,}S{2,}]+)");
XmlUtils::GetLower(tmp); boost::wregex re2(L"([mMdDy{2,}Y{2,}]+)");
if (std::wstring::npos != tmp.find(L"at") || std::wstring tmp = strFormatCode;
std::wstring::npos != tmp.find(L"pm") ||
(std::wstring::npos != tmp.find(L"h") && std::wstring::npos != tmp.find(L"s")) || std::list<std::wstring> result1;
language_code == 0xF400) bool b1 = boost::regex_split(std::back_inserter(result1), tmp, re1);
tmp = strFormatCode;
std::list<std::wstring> result2;
bool b2 = boost::regex_split(std::back_inserter(result2), tmp, re2);
if (b1 && b2 && result1.size() > 2 && result2.size() > 2)
{
return SimpleTypes::Spreadsheet::celltypeDateTime;
}
if (b1 && result1.size() > 2)
{ {
return SimpleTypes::Spreadsheet::celltypeTime; return SimpleTypes::Spreadsheet::celltypeTime;
} }
if ((std::wstring::npos != tmp.find(L"y") && std::wstring::npos != tmp.find(L"d")) || if (b2 && result2.size() > 2)
(std::wstring::npos != tmp.find(L"d") && std::wstring::npos != tmp.find(L"m")) ||
(std::wstring::npos != tmp.find(L"y") && std::wstring::npos != tmp.find(L"m")) ||
language_code == 0xF800)
{ {
return SimpleTypes::Spreadsheet::celltypeDate; return SimpleTypes::Spreadsheet::celltypeDate;
} }
if (std::wstring::npos != tmp.find(L"%")) if (std::wstring::npos != strFormatCode.find(L"%"))
{ {
return SimpleTypes::Spreadsheet::celltypePercentage; return SimpleTypes::Spreadsheet::celltypePercentage;
} }
if (std::wstring::npos != tmp.find(L"#") || if (std::wstring::npos != strFormatCode.find(L"#") ||
std::wstring::npos != tmp.find(L"?") || std::wstring::npos != strFormatCode.find(L"?") ||
std::wstring::npos != tmp.find(L"0")) std::wstring::npos != strFormatCode.find(L"0"))
{ {
return SimpleTypes::Spreadsheet::celltypeNumber; return SimpleTypes::Spreadsheet::celltypeNumber;
} }

View File

@ -850,16 +850,23 @@ void odf_number_styles_context::create_text_style(number_format_state & state, o
{ {
create_element(L"number", L"text-style", root_elm, odf_context_); create_element(L"number", L"text-style", root_elm, odf_context_);
} }
static std::wstring replace_unwanted(boost::wsmatch const & what)
{
return L"";
}
void odf_number_styles_context::detect_format(number_format_state & state) void odf_number_styles_context::detect_format(number_format_state & state)
{ {
if (state.ods_type != office_value_type::Custom) return; if (state.ods_type != office_value_type::Custom) return;
if (state.format_code.empty())return; if (state.format_code.empty()) return;
boost::wregex re_unwanted(L"([\"'])(.+?)\\1");
std::wstring strFormatCode = boost::regex_replace(state.format_code[0], re_unwanted, &replace_unwanted, boost::match_default | boost::format_all);
//find [$<Currency String>-<language info>]. //find [$<Currency String>-<language info>].
boost::wregex re(L"(?:\\[)(?:\\$)(\\S+)?\-(\\S+)(?:\\])"); boost::wregex re(L"(?:\\[)(?:\\$)(\\S+)?\-(\\S+)(?:\\])");
boost::wsmatch result; boost::wsmatch result;
bool b = boost::regex_search(state.format_code[0], result, re); bool b = boost::regex_search(strFormatCode, result, re);
if (b && result.size() >= 3) if (b && result.size() >= 3)
{ {
@ -879,48 +886,43 @@ void odf_number_styles_context::detect_format(number_format_state & state)
state.ods_type = office_value_type::Currency; state.ods_type = office_value_type::Currency;
return; return;
} }
//if (state.format_code.size() == 2)//>0, <0
//{
//}
//else if (state.format_code.size() == 3)//>0, <0, ==0
//{
//}
if (state.format_code.size() > 0) //any if (state.format_code.size() > 0) //any
{ {
std::wstring tmp = state.format_code[0]; boost::wregex re1(L"([mMhHs{2,}S{2,}]+)");
XmlUtils::GetLower(tmp); boost::wregex re2(L"([mMdDy{2,}Y{2,}]+)");
if (std::wstring::npos != tmp.find(L"at") || std::wstring tmp = strFormatCode;
std::wstring::npos != tmp.find(L"pm") ||
std::wstring::npos != tmp.find(L"h") || std::list<std::wstring> result1;
std::wstring::npos != tmp.find(L"s") || state.language_code == 0xF400) bool b1 = boost::regex_split(std::back_inserter(result1), tmp, re1);
tmp = strFormatCode;
std::list<std::wstring> result2;
bool b2 = boost::regex_split(std::back_inserter(result2), tmp, re2);
if (b1 && b2 && result1.size() > 2 && result2.size() > 2)
{ {
state.ods_type = office_value_type::DateTime; state.ods_type = office_value_type::DateTime;
if (b)
state.format_code[0] = boost::regex_replace( state.format_code[0], re, L"");
return; return;
} }
if (std::wstring::npos != tmp.find(L"y") || if (b1 && result1.size() > 2)
std::wstring::npos != tmp.find(L"d") || {
std::wstring::npos != tmp.find(L"m") || state.language_code == 0xF800)//minutes отсеялись выше state.ods_type = office_value_type::Time;
return;
}
if (b2 && result2.size() > 2)
{ {
state.ods_type = office_value_type::Date; state.ods_type = office_value_type::Date;
if (b)
state.format_code[0] = boost::regex_replace( state.format_code[0], re, L"");
return; return;
} }
if (std::wstring::npos != tmp.find(L"%")) if (std::wstring::npos != strFormatCode.find(L"%"))
{ {
state.ods_type = office_value_type::Percentage; state.ods_type = office_value_type::Percentage;
return; return;
} }
if (std::wstring::npos != tmp.find(L"#") || if (std::wstring::npos != strFormatCode.find(L"#") ||
std::wstring::npos != tmp.find(L"?") || std::wstring::npos != strFormatCode.find(L"?") ||
std::wstring::npos != tmp.find(L"0")) std::wstring::npos != strFormatCode.find(L"0"))
{ {
state.ods_type = office_value_type::Float; state.ods_type = office_value_type::Float;
return; return;