diff --git a/source/detail/custom_value_traits.cpp b/source/detail/custom_value_traits.cpp index 2bb3933a..d4b9b1a9 100644 --- a/source/detail/custom_value_traits.cpp +++ b/source/detail/custom_value_traits.cpp @@ -94,11 +94,10 @@ std::string to_string(relationship_type t) case relationship_type::image: return "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image"; case relationship_type::unknown: -#ifndef __clang__ - default: -#endif return "unknown"; } + + default_case("unknown"); } std::string to_string(pattern_fill_type fill_type) diff --git a/source/detail/default_case.hpp b/source/detail/default_case.hpp index 8a39a4ae..f1f89534 100644 --- a/source/detail/default_case.hpp +++ b/source/detail/default_case.hpp @@ -23,10 +23,12 @@ #pragma once +#include + #define EXCEPT_ON_UNHANDLED_SWITCH_CASE #ifdef EXCEPT_ON_UNHANDLED_SWITCH_CASE -#define default_case(default_value) throw xlnt::exception("unhandled case"); +#define default_case(default_value) throw xlnt::unhandled_switch_case(); #else #define default_case(default_value) return default_value; #endif diff --git a/source/detail/number_formatter.cpp b/source/detail/number_formatter.cpp index b9ce5835..8806f7e0 100644 --- a/source/detail/number_formatter.cpp +++ b/source/detail/number_formatter.cpp @@ -132,283 +132,302 @@ void number_format_parser::parse() switch (token.type) { case number_format_token::token_type::end_section: - codes_.push_back(section); - section = format_code(); + { + codes_.push_back(section); + section = format_code(); - break; + break; + } case number_format_token::token_type::color: - if (section.has_color || section.has_condition || section.has_locale || !section.parts.empty()) { - throw std::runtime_error("color should be the first part of a format"); - } + if (section.has_color || section.has_condition || section.has_locale || !section.parts.empty()) + { + throw std::runtime_error("color should be the first part of a format"); + } - section.has_color = true; - section.color = color_from_string(token.string); - break; + section.has_color = true; + section.color = color_from_string(token.string); + + break; + } case number_format_token::token_type::locale: - { - if (section.has_locale) { - throw std::runtime_error("multiple locales"); + if (section.has_locale) + { + throw std::runtime_error("multiple locales"); + } + + section.has_locale = true; + auto parsed_locale = locale_from_string(token.string); + section.locale = parsed_locale.first; + + if (!parsed_locale.second.empty()) + { + part.type = template_part::template_type::text; + part.string = parsed_locale.second; + section.parts.push_back(part); + part = template_part(); + } + + break; } - section.has_locale = true; - auto parsed_locale = locale_from_string(token.string); - section.locale = parsed_locale.first; - - if (!parsed_locale.second.empty()) - { - part.type = template_part::template_type::text; - part.string = parsed_locale.second; - section.parts.push_back(part); - part = template_part(); - } - - break; - } - case number_format_token::token_type::condition: - { - if (section.has_condition) { - throw std::runtime_error("multiple conditions"); - } - - section.has_condition = true; - std::string value; - - if (token.string.front() == '<') - { - if (token.string[1] == '=') + if (section.has_condition) { - section.condition.type = format_condition::condition_type::less_or_equal; - value = token.string.substr(2); + throw std::runtime_error("multiple conditions"); } - else if (token.string[1] == '>') + + section.has_condition = true; + std::string value; + + if (token.string.front() == '<') { - section.condition.type = format_condition::condition_type::not_equal; - value = token.string.substr(2); + if (token.string[1] == '=') + { + section.condition.type = format_condition::condition_type::less_or_equal; + value = token.string.substr(2); + } + else if (token.string[1] == '>') + { + section.condition.type = format_condition::condition_type::not_equal; + value = token.string.substr(2); + } + else + { + section.condition.type = format_condition::condition_type::less_than; + value = token.string.substr(1); + } } - else + else if (token.string.front() == '>') { - section.condition.type = format_condition::condition_type::less_than; + if (token.string[1] == '=') + { + section.condition.type = format_condition::condition_type::greater_or_equal; + value = token.string.substr(2); + } + else + { + section.condition.type = format_condition::condition_type::greater_than; + value = token.string.substr(1); + } + } + else if (token.string.front() == '=') + { + section.condition.type = format_condition::condition_type::equal; value = token.string.substr(1); } - } - else if (token.string.front() == '>') - { - if (token.string[1] == '=') - { - section.condition.type = format_condition::condition_type::greater_or_equal; - value = token.string.substr(2); - } - else - { - section.condition.type = format_condition::condition_type::greater_than; - value = token.string.substr(1); - } - } - else if (token.string.front() == '=') - { - section.condition.type = format_condition::condition_type::equal; - value = token.string.substr(1); - } - section.condition.value = std::stold(value); - break; - } + section.condition.value = std::stold(value); + break; + } case number_format_token::token_type::text: - part.type = template_part::template_type::text; - part.string = token.string; - section.parts.push_back(part); - part = template_part(); - - break; - - case number_format_token::token_type::fill: - part.type = template_part::template_type::fill; - part.string = token.string; - section.parts.push_back(part); - part = template_part(); - - break; - - case number_format_token::token_type::space: - part.type = template_part::template_type::space; - part.string = token.string; - section.parts.push_back(part); - part = template_part(); - - break; - case number_format_token::token_type::number: - part.type = template_part::template_type::general; - part.placeholders = parse_placeholders(token.string); - section.parts.push_back(part); - part = template_part(); - - break; - case number_format_token::token_type::datetime: - section.is_datetime = true; - - switch (token.string.front()) { - case '[': - section.is_timedelta = true; + part.type = template_part::template_type::text; + part.string = token.string; + section.parts.push_back(part); + part = template_part(); - if (token.string == "[h]" || token.string == "[hh]") - { - part.type = template_part::template_type::elapsed_hours; - break; - } - else if (token.string == "[m]" || token.string == "[mm]") - { - part.type = template_part::template_type::elapsed_minutes; - break; - } - else if (token.string == "[s]" || token.string == "[ss]") - { - part.type = template_part::template_type::elapsed_seconds; - break; - } - - unhandled_case(true); - break; - - case 'm': - if (token.string == "m") - { - part.type = template_part::template_type::month_number; - break; - } - else if (token.string == "mm") - { - part.type = template_part::template_type::month_number_leading_zero; - break; - } - else if (token.string == "mmm") - { - part.type = template_part::template_type::month_abbreviation; - break; - } - else if (token.string == "mmmm") - { - part.type = template_part::template_type::month_name; - break; - } - else if (token.string == "mmmmm") - { - part.type = template_part::template_type::month_letter; - break; - } - - unhandled_case(true); - break; - - case 'd': - if (token.string == "d") - { - part.type = template_part::template_type::day_number; - break; - } - else if (token.string == "dd") - { - part.type = template_part::template_type::day_number_leading_zero; - break; - } - else if (token.string == "ddd") - { - part.type = template_part::template_type::day_abbreviation; - break; - } - else if (token.string == "dddd") - { - part.type = template_part::template_type::day_name; - break; - } - - unhandled_case(true); - break; - - case 'y': - if (token.string == "yy") - { - part.type = template_part::template_type::year_short; - break; - } - else if (token.string == "yyyy") - { - part.type = template_part::template_type::year_long; - break; - } - - unhandled_case(true); - break; - - case 'h': - if (token.string == "h") - { - part.type = template_part::template_type::hour; - break; - } - else if (token.string == "hh") - { - part.type = template_part::template_type::hour_leading_zero; - break; - } - - unhandled_case(true); - break; - - case 's': - if (token.string == "s") - { - part.type = template_part::template_type::second; - break; - } - else if (token.string == "ss") - { - part.type = template_part::template_type::second_leading_zero; - break; - } - - unhandled_case(true); - break; - - case 'A': - section.twelve_hour = true; - - if (token.string == "AM/PM") - { - part.type = template_part::template_type::am_pm; - break; - } - else if (token.string == "A/P") - { - part.type = template_part::template_type::a_p; - break; - } - - unhandled_case(true); - break; - - default: - unhandled_case(true); break; } - section.parts.push_back(part); - part = template_part(); + case number_format_token::token_type::fill: + { + part.type = template_part::template_type::fill; + part.string = token.string; + section.parts.push_back(part); + part = template_part(); - break; + break; + } + + case number_format_token::token_type::space: + { + part.type = template_part::template_type::space; + part.string = token.string; + section.parts.push_back(part); + part = template_part(); + + break; + } + + case number_format_token::token_type::number: + { + part.type = template_part::template_type::general; + part.placeholders = parse_placeholders(token.string); + section.parts.push_back(part); + part = template_part(); + + break; + } + + case number_format_token::token_type::datetime: + { + section.is_datetime = true; + + switch (token.string.front()) + { + case '[': + section.is_timedelta = true; + + if (token.string == "[h]" || token.string == "[hh]") + { + part.type = template_part::template_type::elapsed_hours; + break; + } + else if (token.string == "[m]" || token.string == "[mm]") + { + part.type = template_part::template_type::elapsed_minutes; + break; + } + else if (token.string == "[s]" || token.string == "[ss]") + { + part.type = template_part::template_type::elapsed_seconds; + break; + } + + unhandled_case(true); + break; + + case 'm': + if (token.string == "m") + { + part.type = template_part::template_type::month_number; + break; + } + else if (token.string == "mm") + { + part.type = template_part::template_type::month_number_leading_zero; + break; + } + else if (token.string == "mmm") + { + part.type = template_part::template_type::month_abbreviation; + break; + } + else if (token.string == "mmmm") + { + part.type = template_part::template_type::month_name; + break; + } + else if (token.string == "mmmmm") + { + part.type = template_part::template_type::month_letter; + break; + } + + unhandled_case(true); + break; + + case 'd': + if (token.string == "d") + { + part.type = template_part::template_type::day_number; + break; + } + else if (token.string == "dd") + { + part.type = template_part::template_type::day_number_leading_zero; + break; + } + else if (token.string == "ddd") + { + part.type = template_part::template_type::day_abbreviation; + break; + } + else if (token.string == "dddd") + { + part.type = template_part::template_type::day_name; + break; + } + + unhandled_case(true); + break; + + case 'y': + if (token.string == "yy") + { + part.type = template_part::template_type::year_short; + break; + } + else if (token.string == "yyyy") + { + part.type = template_part::template_type::year_long; + break; + } + + unhandled_case(true); + break; + + case 'h': + if (token.string == "h") + { + part.type = template_part::template_type::hour; + break; + } + else if (token.string == "hh") + { + part.type = template_part::template_type::hour_leading_zero; + break; + } + + unhandled_case(true); + break; + + case 's': + if (token.string == "s") + { + part.type = template_part::template_type::second; + break; + } + else if (token.string == "ss") + { + part.type = template_part::template_type::second_leading_zero; + break; + } + + unhandled_case(true); + break; + + case 'A': + section.twelve_hour = true; + + if (token.string == "AM/PM") + { + part.type = template_part::template_type::am_pm; + break; + } + else if (token.string == "A/P") + { + part.type = template_part::template_type::a_p; + break; + } + + unhandled_case(true); + break; + + default: + unhandled_case(true); + break; + } + + section.parts.push_back(part); + part = template_part(); + + break; + } case number_format_token::token_type::end: - codes_.push_back(section); - finalize(); + { + codes_.push_back(section); + finalize(); - return; + return; + } } token = parse_next_token(); @@ -1388,196 +1407,270 @@ std::string number_formatter::format_number(const format_code &format, long doub switch (part.type) { case template_part::template_type::space: - result.push_back(' '); - break; - case template_part::template_type::text: - result.append(part.string); - break; - - case template_part::template_type::fill: - fill = true; - fill_index = result.size(); - fill_character = part.string; - break; - case template_part::template_type::general: - { - if (part.placeholders.type == format_placeholders::placeholders_type::fractional_part - && (format.is_datetime || format.is_timedelta)) { - auto digits = std::min( - static_cast(6), part.placeholders.num_zeros + part.placeholders.num_optionals); - auto denominator = static_cast(std::pow(10.0, digits)); - auto fractional_seconds = dt.microsecond / 1.0E6L * denominator; - fractional_seconds = std::round(fractional_seconds) / denominator; - result.append(fill_placeholders(part.placeholders, fractional_seconds)); + result.push_back(' '); break; } - if (part.placeholders.type == format_placeholders::placeholders_type::fraction_integer) + case template_part::template_type::text: { - improper_fraction = false; + result.append(part.string); + break; } - if (part.placeholders.type == format_placeholders::placeholders_type::fraction_numerator) + case template_part::template_type::fill: { - i += 2; + fill = true; + fill_index = result.size(); + fill_character = part.string; + break; + } - if (number == 0.L) + case template_part::template_type::general: + { + if (part.placeholders.type == format_placeholders::placeholders_type::fractional_part + && (format.is_datetime || format.is_timedelta)) { - result.pop_back(); + auto digits = std::min( + static_cast(6), part.placeholders.num_zeros + part.placeholders.num_optionals); + auto denominator = static_cast(std::pow(10.0, digits)); + auto fractional_seconds = dt.microsecond / 1.0E6L * denominator; + fractional_seconds = std::round(fractional_seconds) / denominator; + result.append(fill_placeholders(part.placeholders, fractional_seconds)); break; } - result.append(fill_fraction_placeholders( - part.placeholders, format.parts[i].placeholders, number, improper_fraction)); - } - else if (part.placeholders.scientific - && part.placeholders.type == format_placeholders::placeholders_type::integer_part) - { - auto integer_part = part.placeholders; - ++i; - auto fractional_part = format.parts[i++].placeholders; - auto exponent_part = format.parts[i++].placeholders; - result.append(fill_scientific_placeholders(integer_part, fractional_part, exponent_part, number)); - } - else - { - result.append(fill_placeholders(part.placeholders, number)); + if (part.placeholders.type == format_placeholders::placeholders_type::fraction_integer) + { + improper_fraction = false; + } + + if (part.placeholders.type == format_placeholders::placeholders_type::fraction_numerator) + { + i += 2; + + if (number == 0.L) + { + result.pop_back(); + break; + } + + result.append(fill_fraction_placeholders( + part.placeholders, format.parts[i].placeholders, number, improper_fraction)); + } + else if (part.placeholders.scientific + && part.placeholders.type == format_placeholders::placeholders_type::integer_part) + { + auto integer_part = part.placeholders; + ++i; + auto fractional_part = format.parts[i++].placeholders; + auto exponent_part = format.parts[i++].placeholders; + result.append(fill_scientific_placeholders(integer_part, fractional_part, exponent_part, number)); + } + else + { + result.append(fill_placeholders(part.placeholders, number)); + } + + break; } - break; - } case template_part::template_type::day_number: - result.append(std::to_string(dt.day)); - break; + { + result.append(std::to_string(dt.day)); + break; + } + case template_part::template_type::day_number_leading_zero: - if (dt.day < 10) { - result.push_back('0'); + if (dt.day < 10) + { + result.push_back('0'); + } + + result.append(std::to_string(dt.day)); + break; } - result.append(std::to_string(dt.day)); - break; case template_part::template_type::month_abbreviation: - result.append(month_names->at(static_cast(dt.month) - 1).substr(0, 3)); - break; + { + result.append(month_names->at(static_cast(dt.month) - 1).substr(0, 3)); + break; + } + case template_part::template_type::month_name: - result.append(month_names->at(static_cast(dt.month) - 1)); - break; + { + result.append(month_names->at(static_cast(dt.month) - 1)); + break; + } + case template_part::template_type::month_number: - result.append(std::to_string(dt.month)); - break; + { + result.append(std::to_string(dt.month)); + break; + } + case template_part::template_type::month_number_leading_zero: - if (dt.month < 10) { - result.push_back('0'); + if (dt.month < 10) + { + result.push_back('0'); + } + + result.append(std::to_string(dt.month)); + break; } - result.append(std::to_string(dt.month)); - break; case template_part::template_type::year_short: - if (dt.year % 1000 < 10) { - result.push_back('0'); + if (dt.year % 1000 < 10) + { + result.push_back('0'); + } + + result.append(std::to_string(dt.year % 1000)); + break; } - result.append(std::to_string(dt.year % 1000)); - break; case template_part::template_type::year_long: - result.append(std::to_string(dt.year)); - break; + { + result.append(std::to_string(dt.year)); + break; + } + case template_part::template_type::hour: - result.append(std::to_string(hour)); - break; + { + result.append(std::to_string(hour)); + break; + } + case template_part::template_type::hour_leading_zero: - if (hour < 10) { - result.push_back('0'); + if (hour < 10) + { + result.push_back('0'); + } + + result.append(std::to_string(hour)); + break; } - result.append(std::to_string(hour)); - break; case template_part::template_type::minute: - result.append(std::to_string(dt.minute)); - break; + { + result.append(std::to_string(dt.minute)); + break; + } + case template_part::template_type::minute_leading_zero: - if (dt.minute < 10) { - result.push_back('0'); + if (dt.minute < 10) + { + result.push_back('0'); + } + + result.append(std::to_string(dt.minute)); + break; } - result.append(std::to_string(dt.minute)); - break; case template_part::template_type::second: - result.append(std::to_string(dt.second + (dt.microsecond > 500000 ? 1 : 0))); - break; + { + result.append(std::to_string(dt.second + (dt.microsecond > 500000 ? 1 : 0))); + break; + } + case template_part::template_type::second_fractional: - result.append(std::to_string(dt.second)); - break; + { + result.append(std::to_string(dt.second)); + break; + } + case template_part::template_type::second_leading_zero: - if ((dt.second + (dt.microsecond > 500000 ? 1 : 0)) < 10) { - result.push_back('0'); + if ((dt.second + (dt.microsecond > 500000 ? 1 : 0)) < 10) + { + result.push_back('0'); + } + + result.append(std::to_string(dt.second + (dt.microsecond > 500000 ? 1 : 0))); + break; } - result.append(std::to_string(dt.second + (dt.microsecond > 500000 ? 1 : 0))); - break; case template_part::template_type::second_leading_zero_fractional: - if (dt.second < 10) { - result.push_back('0'); + if (dt.second < 10) + { + result.push_back('0'); + } + + result.append(std::to_string(dt.second)); + break; } - result.append(std::to_string(dt.second)); - break; case template_part::template_type::am_pm: - if (dt.hour < 12) { - result.append("AM"); - } - else - { - result.append("PM"); - } + if (dt.hour < 12) + { + result.append("AM"); + } + else + { + result.append("PM"); + } - break; + break; + } case template_part::template_type::a_p: - if (dt.hour < 12) { - result.append("A"); - } - else - { - result.append("P"); - } + if (dt.hour < 12) + { + result.append("A"); + } + else + { + result.append("P"); + } - break; + break; + } case template_part::template_type::elapsed_hours: - result.append(std::to_string(24 * static_cast(number) + dt.hour)); - break; + { + result.append(std::to_string(24 * static_cast(number) + dt.hour)); + break; + } case template_part::template_type::elapsed_minutes: - result.append(std::to_string(24 * 60 * static_cast(number) + (60 * dt.hour) + dt.minute)); - break; + { + result.append(std::to_string(24 * 60 * static_cast(number) + + (60 * dt.hour) + dt.minute)); + break; + } case template_part::template_type::elapsed_seconds: - result.append(std::to_string( - 24 * 60 * 60 * static_cast(number) + (60 * 60 * dt.hour) + (60 * dt.minute) + dt.second)); - break; + { + result.append(std::to_string(24 * 60 * 60 * static_cast(number) + + (60 * 60 * dt.hour) + (60 * dt.minute) + dt.second)); + break; + } case template_part::template_type::month_letter: - result.append(month_names->at(static_cast(dt.month) - 1).substr(0, 1)); - break; + { + result.append(month_names->at(static_cast(dt.month) - 1).substr(0, 1)); + break; + } case template_part::template_type::day_abbreviation: - result.append(day_names->at(static_cast(dt.weekday()) - 1).substr(0, 3)); - break; + { + result.append(day_names->at(static_cast(dt.weekday()) - 1).substr(0, 3)); + break; + } case template_part::template_type::day_name: - result.append(day_names->at(static_cast(dt.weekday()) - 1)); - break; + { + result.append(day_names->at(static_cast(dt.weekday()) - 1)); + break; + } } } @@ -1603,75 +1696,19 @@ std::string number_formatter::format_text(const format_code &format, const std:: for (const auto &part : format.parts) { - switch (part.type) + if (part.type == template_part::template_type::text) { - case template_part::template_type::text: result.append(part.string); any_text_part = true; - break; - - case template_part::template_type::general: + } + else if (part.type == template_part::template_type::general) + { if (part.placeholders.type == format_placeholders::placeholders_type::general || part.placeholders.type == format_placeholders::placeholders_type::text) { result.append(text); any_text_part = true; } - - break; - - case template_part::template_type::fill: - break; - case template_part::template_type::space: - break; - case template_part::template_type::month_number: - break; - case template_part::template_type::month_number_leading_zero: - break; - case template_part::template_type::month_abbreviation: - break; - case template_part::template_type::month_name: - break; - case template_part::template_type::month_letter: - break; - case template_part::template_type::day_number: - break; - case template_part::template_type::day_number_leading_zero: - break; - case template_part::template_type::day_abbreviation: - break; - case template_part::template_type::day_name: - break; - case template_part::template_type::year_short: - break; - case template_part::template_type::year_long: - break; - case template_part::template_type::hour: - break; - case template_part::template_type::hour_leading_zero: - break; - case template_part::template_type::minute: - break; - case template_part::template_type::minute_leading_zero: - break; - case template_part::template_type::second: - break; - case template_part::template_type::second_fractional: - break; - case template_part::template_type::second_leading_zero: - break; - case template_part::template_type::second_leading_zero_fractional: - break; - case template_part::template_type::am_pm: - break; - case template_part::template_type::a_p: - break; - case template_part::template_type::elapsed_hours: - break; - case template_part::template_type::elapsed_minutes: - break; - case template_part::template_type::elapsed_seconds: - break; } } diff --git a/source/detail/xlsx_consumer.cpp b/source/detail/xlsx_consumer.cpp index 4f50905a..38d2b42b 100755 --- a/source/detail/xlsx_consumer.cpp +++ b/source/detail/xlsx_consumer.cpp @@ -245,9 +245,9 @@ std::array, 3> parse_header_footer(const std::st tokens.push_back(token); } - const auto parse_section = [&tokens, &result](hf_code code) { + const auto parse_section = [&tokens, &result](hf_code code) + { std::vector end_codes{hf_code::left_section, hf_code::center_section, hf_code::right_section}; - end_codes.erase(std::find(end_codes.begin(), end_codes.end(), code)); std::size_t start_index = 0; @@ -296,141 +296,197 @@ std::array, 3> parse_header_footer(const std::st switch (current_token.code) { case hf_code::text: - break; // already handled above + { + break; // already handled above + } case hf_code::left_section: - break; // used below + { + break; // used below + } case hf_code::center_section: - break; // used below + { + break; // used below + } case hf_code::right_section: - break; // used below + { + break; // used below + } case hf_code::current_page_number: - current_run.first = current_run.first + "&P"; - break; + { + current_run.first = current_run.first + "&P"; + break; + } case hf_code::total_page_number: - current_run.first = current_run.first + "&N"; - break; + { + current_run.first = current_run.first + "&N"; + break; + } case hf_code::font_size: - if (!current_run.second.is_set()) - { - current_run.second = xlnt::font(); - } - current_run.second.get().size(std::stod(current_token.value)); - break; - - case hf_code::text_font_color: - if (current_token.value.size() == 6) { if (!current_run.second.is_set()) { current_run.second = xlnt::font(); } - current_run.second.get().color(xlnt::rgb_color(current_token.value)); + + current_run.second.get().size(std::stod(current_token.value)); + + break; } - break; + case hf_code::text_font_color: + { + if (current_token.value.size() == 6) + { + if (!current_run.second.is_set()) + { + current_run.second = xlnt::font(); + } + + current_run.second.get().color(xlnt::rgb_color(current_token.value)); + } + + break; + } case hf_code::text_strikethrough: - break; + { + break; + } case hf_code::text_superscript: - break; + { + break; + } case hf_code::text_subscript: - break; + { + break; + } case hf_code::date: - current_run.first = current_run.first + "&D"; - break; + { + current_run.first = current_run.first + "&D"; + break; + } case hf_code::time: - current_run.first = current_run.first + "&T"; - break; + { + current_run.first = current_run.first + "&T"; + break; + } case hf_code::picture_as_background: - current_run.first = current_run.first + "&G"; - break; + { + current_run.first = current_run.first + "&G"; + break; + } case hf_code::text_single_underline: - break; + { + break; + } case hf_code::text_double_underline: - break; + { + break; + } case hf_code::workbook_file_path: - current_run.first = current_run.first + "&Z"; - break; + { + current_run.first = current_run.first + "&Z"; + break; + } case hf_code::workbook_file_name: - current_run.first = current_run.first + "&F"; - break; + { + current_run.first = current_run.first + "&F"; + break; + } case hf_code::sheet_tab_name: - current_run.first = current_run.first + "&A"; - break; + { + current_run.first = current_run.first + "&A"; + break; + } case hf_code::add_to_page_number: - break; + { + break; + } case hf_code::subtract_from_page_number: - break; + { + break; + } case hf_code::text_font_name: - { - auto comma_index = current_token.value.find(','); - auto font_name = current_token.value.substr(0, comma_index); - - if (!current_run.second.is_set()) { - current_run.second = xlnt::font(); - } + auto comma_index = current_token.value.find(','); + auto font_name = current_token.value.substr(0, comma_index); - if (font_name != "-") - { - current_run.second.get().name(font_name); - } - - if (comma_index != std::string::npos) - { - auto font_type = current_token.value.substr(comma_index + 1); - - if (font_type == "Bold") + if (!current_run.second.is_set()) { - current_run.second.get().bold(true); + current_run.second = xlnt::font(); } - else if (font_type == "Italic") - { - } - else if (font_type == "BoldItalic") - { - current_run.second.get().bold(true); - } - } - } - break; + if (font_name != "-") + { + current_run.second.get().name(font_name); + } + + if (comma_index != std::string::npos) + { + auto font_type = current_token.value.substr(comma_index + 1); + + if (font_type == "Bold") + { + current_run.second.get().bold(true); + } + else if (font_type == "Italic") + { + // TODO + } + else if (font_type == "BoldItalic") + { + current_run.second.get().bold(true); + } + } + + break; + } case hf_code::bold_font_style: - if (!current_run.second.is_set()) { - current_run.second = xlnt::font(); + if (!current_run.second.is_set()) + { + current_run.second = xlnt::font(); + } + + current_run.second.get().bold(true); + + break; } - current_run.second.get().bold(true); - break; case hf_code::italic_font_style: - break; + { + break; + } case hf_code::outline_style: - break; + { + break; + } case hf_code::shadow_style: - break; + { + break; + } } } diff --git a/source/detail/xlsx_crypto.cpp b/source/detail/xlsx_crypto.cpp index d9e50400..f49a9b31 100644 --- a/source/detail/xlsx_crypto.cpp +++ b/source/detail/xlsx_crypto.cpp @@ -64,6 +64,7 @@ struct value_traits return xlnt::detail::hash_algorithm::ripemd160; else if (hash_algorithm_string == "Whirlpool") return xlnt::detail::hash_algorithm::whirlpool; + default_case(xlnt::detail::hash_algorithm::sha1); } @@ -160,8 +161,9 @@ std::vector crypto_helper::decode_base64(const std::string &encode decoder.Put(reinterpret_cast(encoded.data()), encoded.size()); decoder.MessageEnd(); - std::vector decoded(decoder.MaxRetrievable(), 0); - decoder.Get(decoded.data(), decoded.size()); + const auto bytes_decoded = std::size_t(decoder.MaxRetrievable()); + auto decoded = std::vector(bytes_decoded, 0); + decoder.Get(decoded.data(), bytes_decoded); return decoded; } @@ -172,8 +174,9 @@ std::string crypto_helper::encode_base64(const std::vector &decode encoder.Put(reinterpret_cast(decoded.data()), decoded.size()); encoder.MessageEnd(); - std::vector encoded(encoder.MaxRetrievable(), 0); - encoder.Get(encoded.data(), encoded.size()); + const auto bytes_encoded = std::size_t(encoder.MaxRetrievable()); + auto encoded = std::vector(bytes_encoded, 0); + encoder.Get(encoded.data(), bytes_encoded); return std::string(encoded.begin(), encoded.end()); } diff --git a/source/detail/xlsx_producer.cpp b/source/detail/xlsx_producer.cpp index c276b2d3..6989ba8a 100755 --- a/source/detail/xlsx_producer.cpp +++ b/source/detail/xlsx_producer.cpp @@ -193,7 +193,8 @@ void xlsx_producer::write_content_types() write_end_element(xmlns, "Types"); } -void xlsx_producer::write_property(const std::string &name, const variant &value, const std::string &ns, bool custom, std::size_t pid) +void xlsx_producer::write_property(const std::string &name, const variant &value, + const std::string &ns, bool custom, std::size_t pid) { if (custom) { @@ -208,115 +209,126 @@ void xlsx_producer::write_property(const std::string &name, const variant &value switch (value.value_type()) { case variant::type::null: - break; + { + break; + } case variant::type::boolean: - if (custom) { - write_attribute("fmtid", "{D5CDD505-2E9C-101B-9397-08002B2CF9AE}"); - write_attribute("pid", pid); - write_start_element(constants::ns("vt"), "bool"); + if (custom) + { + write_attribute("fmtid", "{D5CDD505-2E9C-101B-9397-08002B2CF9AE}"); + write_attribute("pid", pid); + write_start_element(constants::ns("vt"), "bool"); + } + + write_characters(value.get() ? "true" : "false"); + + if (custom) + { + write_end_element(constants::ns("vt"), "bool"); + } + + break; } - write_characters(value.get() ? "true" : "false"); - - if (custom) - { - write_end_element(constants::ns("vt"), "bool"); - } - - break; - case variant::type::i4: - if (custom) { - write_attribute("fmtid", "{D5CDD505-2E9C-101B-9397-08002B2CF9AE}"); - write_attribute("pid", pid); - write_start_element(constants::ns("vt"), "i4"); + if (custom) + { + write_attribute("fmtid", "{D5CDD505-2E9C-101B-9397-08002B2CF9AE}"); + write_attribute("pid", pid); + write_start_element(constants::ns("vt"), "i4"); + } + + write_characters(value.get()); + + if (custom) + { + write_end_element(constants::ns("vt"), "i4"); + } + + break; } - write_characters(value.get()); - - if (custom) - { - write_end_element(constants::ns("vt"), "i4"); - } - - break; - case variant::type::lpstr: - if (custom) { - write_attribute("fmtid", "{D5CDD505-2E9C-101B-9397-08002B2CF9AE}"); - write_attribute("pid", pid); - write_start_element(constants::ns("vt"), "lpwstr"); + if (custom) + { + write_attribute("fmtid", "{D5CDD505-2E9C-101B-9397-08002B2CF9AE}"); + write_attribute("pid", pid); + write_start_element(constants::ns("vt"), "lpwstr"); + } + + if (!custom && ns == constants::ns("dcterms") && (name == "created" || name == "modified")) + { + write_attribute(xml::qname(constants::ns("xsi"), "type"), "dcterms:W3CDTF"); + } + + write_characters(value.get()); + + if (custom) + { + write_end_element(constants::ns("vt"), "lpwstr"); + } + + break; } - if (!custom && ns == constants::ns("dcterms") && (name == "created" || name == "modified")) - { - write_attribute(xml::qname(constants::ns("xsi"), "type"), "dcterms:W3CDTF"); - } - - write_characters(value.get()); - - if (custom) - { - write_end_element(constants::ns("vt"), "lpwstr"); - } - - break; - case variant::type::date: - write_attribute(xml::qname(constants::ns("xsi"), "type"), "dcterms:W3CDTF"); - write_characters(value.get().to_iso_string()); - break; + { + write_attribute(xml::qname(constants::ns("xsi"), "type"), "dcterms:W3CDTF"); + write_characters(value.get().to_iso_string()); + + break; + } case variant::type::vector: - { - write_start_element(constants::ns("vt"), "vector"); - - auto vector = value.get>(); - std::unordered_set types; - - for (const auto &element : vector) { - types.insert(element.value_type()); + write_start_element(constants::ns("vt"), "vector"); + + auto vector = value.get>(); + std::unordered_set types; + + for (const auto &element : vector) + { + types.insert(element.value_type()); + } + + const auto is_mixed = types.size() > 1; + const auto vector_type = !is_mixed ? to_string(*types.begin()) : "variant"; + + write_attribute("size", vector.size()); + write_attribute("baseType", vector_type); + + for (std::size_t i = 0; i < vector.size(); ++i) + { + const auto &vector_element = vector.at(i); + + if (is_mixed) + { + write_start_element(constants::ns("vt"), "variant"); + } + + if (vector_element.value_type() == variant::type::lpstr) + { + write_element(constants::ns("vt"), "lpstr", vector_element.get()); + } + else if (vector_element.value_type() == variant::type::i4) + { + write_element(constants::ns("vt"), "i4", vector_element.get()); + } + + if (is_mixed) + { + write_end_element(constants::ns("vt"), "variant"); + } + } + + write_end_element(constants::ns("vt"), "vector"); + + break; } - - const auto is_mixed = types.size() > 1; - const auto vector_type = !is_mixed ? to_string(*types.begin()) : "variant"; - - write_attribute("size", vector.size()); - write_attribute("baseType", vector_type); - - for (std::size_t i = 0; i < vector.size(); ++i) - { - const auto &vector_element = vector.at(i); - - if (is_mixed) - { - write_start_element(constants::ns("vt"), "variant"); - } - - if (vector_element.value_type() == variant::type::lpstr) - { - write_element(constants::ns("vt"), "lpstr", vector_element.get()); - } - else if (vector_element.value_type() == variant::type::i4) - { - write_element(constants::ns("vt"), "i4", vector_element.get()); - } - - if (is_mixed) - { - write_end_element(constants::ns("vt"), "variant"); - } - } - - write_end_element(constants::ns("vt"), "vector"); - } - - break; } if (custom) @@ -2589,6 +2601,7 @@ void xlsx_producer::write_worksheet(const relationship &rel) path archive_path(worksheet_part.parent().append(child_rel.target().path())); auto split_part_path = archive_path.split(); auto part_path_iter = split_part_path.begin(); + while (part_path_iter != split_part_path.end()) { if (*part_path_iter == "..") @@ -2599,85 +2612,19 @@ void xlsx_producer::write_worksheet(const relationship &rel) ++part_path_iter; } + archive_path = std::accumulate(split_part_path.begin(), split_part_path.end(), path(""), [](const path &a, const std::string &b) { return a.append(b); }); begin_part(archive_path); - switch (child_rel.type()) + if (child_rel.type() == relationship_type::comments) { - case relationship_type::comments: write_comments(child_rel, ws, cells_with_comments); - break; - - case relationship_type::vml_drawing: + } + else if (child_rel.type() == relationship_type::vml_drawing) + { write_vml_drawings(child_rel, ws, cells_with_comments); - break; - - case relationship_type::office_document: - break; - case relationship_type::thumbnail: - break; - case relationship_type::calculation_chain: - break; - case relationship_type::extended_properties: - break; - case relationship_type::core_properties: - break; - case relationship_type::worksheet: - break; - case relationship_type::shared_string_table: - break; - case relationship_type::stylesheet: - break; - case relationship_type::theme: - break; - case relationship_type::hyperlink: - break; - case relationship_type::chartsheet: - break; - case relationship_type::unknown: - break; - case relationship_type::custom_properties: - break; - case relationship_type::printer_settings: - break; - case relationship_type::connections: - break; - case relationship_type::custom_property: - break; - case relationship_type::custom_xml_mappings: - break; - case relationship_type::dialogsheet: - break; - case relationship_type::drawings: - break; - case relationship_type::external_workbook_references: - break; - case relationship_type::pivot_table: - break; - case relationship_type::pivot_table_cache_definition: - break; - case relationship_type::pivot_table_cache_records: - break; - case relationship_type::query_table: - break; - case relationship_type::shared_workbook_revision_headers: - break; - case relationship_type::shared_workbook: - break; - case relationship_type::revision_log: - break; - case relationship_type::shared_workbook_user_data: - break; - case relationship_type::single_cell_table_definitions: - break; - case relationship_type::table_definition: - break; - case relationship_type::volatile_dependencies: - break; - case relationship_type::image: - break; } } } diff --git a/source/detail/zstream.cpp b/source/detail/zstream.cpp index 948d8297..fdf2e61e 100644 --- a/source/detail/zstream.cpp +++ b/source/detail/zstream.cpp @@ -266,14 +266,10 @@ public: strm.next_in = reinterpret_cast(in.data()); } - int ret = inflate(&strm, Z_NO_FLUSH); // decompress + const auto ret = inflate(&strm, Z_NO_FLUSH); // decompress - switch (ret) + if (ret == Z_STREAM_ERROR || ret == Z_NEED_DICT || ret == Z_DATA_ERROR || ret == Z_MEM_ERROR) { - case Z_STREAM_ERROR: - case Z_NEED_DICT: - case Z_DATA_ERROR: - case Z_MEM_ERROR: throw xlnt::exception("couldn't inflate ZIP, possibly corrupted"); } diff --git a/source/workbook/workbook.cpp b/source/workbook/workbook.cpp index 8b346759..0e37df8f 100644 --- a/source/workbook/workbook.cpp +++ b/source/workbook/workbook.cpp @@ -233,7 +233,7 @@ std::string content_type(xlnt::relationship_type type) case relationship_type::custom_properties: return "application/vnd.openxmlformats-officedocument.custom-properties+xml"; case relationship_type::custom_property: - return ""; + throw xlnt::unhandled_switch_case(); case relationship_type::custom_xml_mappings: return "application/xml"; case relationship_type::dialogsheet: @@ -245,9 +245,9 @@ std::string content_type(xlnt::relationship_type type) case relationship_type::external_workbook_references: return "application/vnd.openxmlformats-officedocument.spreadsheetml.externalLink+xml"; case relationship_type::hyperlink: - return ""; + throw xlnt::unhandled_switch_case(); case relationship_type::image: - return ""; + throw xlnt::unhandled_switch_case(); case relationship_type::office_document: return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml"; case relationship_type::pivot_table: @@ -265,7 +265,7 @@ std::string content_type(xlnt::relationship_type type) case relationship_type::shared_string_table: return "application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml"; case relationship_type::shared_workbook: - return ""; + throw xlnt::unhandled_switch_case(); case relationship_type::shared_workbook_revision_headers: return "application/vnd.openxmlformats-officedocument.spreadsheetml.revisionHeaders+xml"; case relationship_type::shared_workbook_user_data: