From 37ec8144edd0cc96ba38b2865ca9df882782c614 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Axel=20D=C3=B6rfler?= Date: Mon, 3 Jan 2005 23:46:08 +0000 Subject: [PATCH] The \'hh command is now also correctly interpreted (it's not a standard command). The Text class no longer likes '\n' and '\r' and will throw those away happily. parse_integer() can now parse integers with different radix. Improved debug output a bit. git-svn-id: file:///srv/svn/repos/haiku/trunk/current@10576 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/add-ons/translators/rtftranslator/RTF.cpp | 50 +++++++++++++------ 1 file changed, 34 insertions(+), 16 deletions(-) diff --git a/src/add-ons/translators/rtftranslator/RTF.cpp b/src/add-ons/translators/rtftranslator/RTF.cpp index 25e9c438a7..b8605700a6 100644 --- a/src/add-ons/translators/rtftranslator/RTF.cpp +++ b/src/add-ons/translators/rtftranslator/RTF.cpp @@ -26,7 +26,7 @@ static const char *kDestinationControlWords[] = { }; static char read_char(BDataIO &stream, bool endOfFileAllowed = false) throw (status_t); -static int32 parse_integer(char first, BDataIO &stream, char &_last) throw (status_t); +static int32 parse_integer(char first, BDataIO &stream, char &_last, int32 base = 10) throw (status_t); using namespace RTF; @@ -49,8 +49,9 @@ read_char(BDataIO &stream, bool endOfFileAllowed) throw (status_t) static int32 -parse_integer(char first, BDataIO &stream, char &_last) throw (status_t) +parse_integer(char first, BDataIO &stream, char &_last, int32 base) throw (status_t) { + const char *kDigits = "0123456789abcdef"; int32 integer = 0; int32 count = 0; @@ -60,10 +61,15 @@ parse_integer(char first, BDataIO &stream, char &_last) throw (status_t) digit = read_char(stream); while (true) { - if (isdigit(digit)) { - integer = integer * 10 + digit - '0'; - count++; - } else { + int32 pos = 0; + for (; pos < base; pos++) { + if (kDigits[pos] == digit) { + integer = integer * base + pos; + count++; + break; + } + } + if (pos == base) { _last = digit; goto out; } @@ -89,7 +95,7 @@ string_array_compare(const char *key, const char **array) static void dump(Element &element, int32 level = 0) { - printf("%03ld:", level); + printf("%03ld (%p):", level, &element); for (int32 i = 0; i < level; i++) printf(" "); @@ -103,8 +109,8 @@ dump(Element &element, int32 level = 0) } else if (RTF::Text *text = dynamic_cast(&element)) { printf(""); puts(text->String()); - } else if (dynamic_cast(&element) != NULL) - puts(""); + } else if (RTF::Group *group = dynamic_cast(&element)) + printf("\n", group->Name()); if (RTF::Group *group = dynamic_cast(&element)) { for (uint32 i = 0; i < group->CountElements(); i++) @@ -536,7 +542,7 @@ Text::Parse(char first, BDataIO &stream, char &last) throw (status_t) size_t position = 0; while (true) { - if (c == '\\' || c == '}' || c == '{' || c == ';') + if (c == '\\' || c == '}' || c == '{' || c == ';' || c == '\n' || c == '\r') break; if (position >= maxSize) { @@ -604,7 +610,7 @@ Command::Parse(char first, BDataIO &stream, char &last) throw (status_t) first = read_char(stream); if (first != '\\') - throw B_BAD_TYPE; + throw (status_t)B_BAD_TYPE; // get name char name[kCommandLength]; @@ -613,7 +619,7 @@ Command::Parse(char first, BDataIO &stream, char &last) throw (status_t) while (isalpha(c = read_char(stream))) { name[length++] = c; if (length >= kCommandLength - 1) - throw B_BAD_TYPE; + throw (status_t)B_BAD_TYPE; } if (length == 0) { @@ -635,12 +641,24 @@ Command::Parse(char first, BDataIO &stream, char &last) throw (status_t) last = c; - if (isdigit(c)) - SetOption(parse_integer(c, stream, last)); + if (fName == "'") { + // hexadecimal + char bytes[2]; + bytes[0] = read_char(stream); + bytes[1] = '\0'; + BMemoryIO memory(bytes, 2); - // a space delimiter is eaten up by the command - if (isspace(last)) + SetOption(parse_integer(c, memory, last, 16)); last = read_char(stream); + } else { + // decimal + if (isdigit(c)) + SetOption(parse_integer(c, stream, last)); + + // a space delimiter is eaten up by the command + if (isspace(last)) + last = read_char(stream); + } }