diff options
author | Stef <stef@ws.local> | 2004-07-23 22:13:48 +0000 |
---|---|---|
committer | Stef <stef@ws.local> | 2004-07-23 22:13:48 +0000 |
commit | 13a4073194d1bf698c9dc0db21b8ff57fdceb635 (patch) | |
tree | 6bb6ad76047ad3878da907b5772bfc24b1ccca22 | |
parent | cea60367e7cc92bdfa8b7a8d1d56d3535652412b (diff) |
- Remove unneeded RtfContext structure
-rw-r--r-- | src/rtfparser.cpp | 165 | ||||
-rw-r--r-- | src/rtfparser.h | 24 |
2 files changed, 89 insertions, 100 deletions
diff --git a/src/rtfparser.cpp b/src/rtfparser.cpp index 738ab50..c136e95 100644 --- a/src/rtfparser.cpp +++ b/src/rtfparser.cpp @@ -83,15 +83,11 @@ bool RtfParser::parse(string fileName) bool RtfParser::parse(FILE* file) { int ch = 0; + bool isData = false; - // The group depth m_depth = 0; m_parseErrors = ""; - - RtfContext cx; - cx.isData = false; - cx.file = file; - cx.data = L""; + m_file = file; if(m_handler) m_handler->startDocument(this); @@ -102,67 +98,63 @@ bool RtfParser::parse(FILE* file) if(ch == EOF) goto done; - // TODO: Do we need this ? - if(!cx.isData) + switch(ch) { - switch(ch) - { - // Starting a control word - case '\\': - if(!parseControlWord(cx)) - goto done; - break; + // Starting a control word + case '\\': + if(!parseControlWord()) + goto done; + break; - // Starting an RTF group - case '{': - { - // Send all previous data - flushData(cx); - - // Handle any unicode destinations properly - m_uniEatStack.push(m_uniEatStack.top()); + // Starting an RTF group + case '{': + { + // Send all previous data + flushData(); - if(m_handler) - m_handler->groupStart(); + // Handle any unicode destinations properly + m_uniEatStack.push(m_uniEatStack.top()); - m_depth++; - } - break; + if(m_handler) + m_handler->groupStart(); - case '}': - { - // Send all previous data - flushData(cx); + m_depth++; + } + break; - if(m_handler) - m_handler->groupEnd(); + case '}': + { + // Send all previous data + flushData(); - // Handle any unicode destinations properly - if(!m_uniEatStack.empty()) - m_uniEatStack.pop(); + if(m_handler) + m_handler->groupEnd(); - m_depth--; - } - break; + // Handle any unicode destinations properly + if(!m_uniEatStack.empty()) + m_uniEatStack.pop(); - default: - cx.isData = true; - break; + m_depth--; } + break; + + default: + isData = true; + break; } - if(cx.isData) + if(isData) { // We translate tabs into the appropriate control word if(ch == '\t') - sendControlWord(cx, "tab", 0, -1); + sendControlWord("tab", 0, -1); // line endings aren't used else if(!strchr("\r\n", ch)) - sendData(cx, ch); + sendData(ch); - cx.isData = false; + isData = false; } } @@ -175,6 +167,9 @@ done: if(m_handler) m_handler->endDocument(); + m_file = NULL; + m_dataBuffer.resize(0); + // If any parse errors return failure return m_parseErrors.empty(); } @@ -184,28 +179,28 @@ done: * HANDLER CALLS */ -void RtfParser::flushData(RtfContext& cx) +void RtfParser::flushData() { - if(!cx.data.empty()) + if(!m_dataBuffer.empty()) { if(m_handler) - m_handler->charData(cx.data); + m_handler->charData(m_dataBuffer); - cx.data.resize(0); + m_dataBuffer.resize(0); } } -void RtfParser::sendData(RtfContext& cx, wchar_t ch) +void RtfParser::sendData(wchar_t ch) { // Skip unicode chars we've been asked to if(m_uniEat > 0) m_uniEat--; else - cx.data.append(1, ch); + m_dataBuffer.append(1, ch); } -void RtfParser::sendData(RtfContext& cx, wstring data) +void RtfParser::sendData(wstring data) { // Skip any unicode chars we've been asked to if(m_uniEat > 0) @@ -214,18 +209,18 @@ void RtfParser::sendData(RtfContext& cx, wstring data) if(len > m_uniEat) len = m_uniEat; - cx.data.append(data.substr(len)); + m_dataBuffer.append(data.substr(len)); m_uniEat -= len; } else { - cx.data.append(data); + m_dataBuffer.append(data); } } -void RtfParser::sendControlWord(RtfContext& cx, string cw, int flags, int param) +void RtfParser::sendControlWord(string cw, int flags, int param) { - flushData(cx); + flushData(); if(m_handler) m_handler->controlWord(cw, flags, param); @@ -236,14 +231,14 @@ void RtfParser::sendControlWord(RtfContext& cx, string cw, int flags, int param) * PARSE HELPERS */ -bool RtfParser::parseHexChar(RtfContext& cx, int num) +bool RtfParser::parseHexChar(int num) { string data; // Get num chars and put them in the string for(int i = 0; i < num; i++) { - char ch = fgetc(cx.file); + char ch = fgetc(m_file); if(ch == -1) return false; @@ -266,7 +261,7 @@ bool RtfParser::parseHexChar(RtfContext& cx, int num) char* end = NULL; int val = strtol(data.c_str(), &end, 16); if(end == data.c_str() + data.size() && m_parseHex) - sendData(cx, val); + sendData(val); else m_parseErrors.append("invalid hex char: " + data + "\n"); } @@ -275,13 +270,13 @@ bool RtfParser::parseHexChar(RtfContext& cx, int num) // Otherwise just send as a hex control word else { - sendControlWord(cx, data, RtfHandler::kIsEncoded, -1); + sendControlWord(data, RtfHandler::kIsEncoded, -1); } return true; } -bool RtfParser::parseControlWord(RtfContext& cx) +bool RtfParser::parseControlWord() { bool isAsterisk = false; string controlword; @@ -289,7 +284,7 @@ bool RtfParser::parseControlWord(RtfContext& cx) while(1) { - int ch = fgetc(cx.file); + int ch = fgetc(m_file); if(ch == WEOF) return false; @@ -312,7 +307,7 @@ bool RtfParser::parseControlWord(RtfContext& cx) // hex spelled out character else if(empty && ch == '\'') { - parseHexChar(cx, 2); + parseHexChar(2); break; } @@ -321,43 +316,43 @@ bool RtfParser::parseControlWord(RtfContext& cx) { isAsterisk = true; - ch = fgetc(cx.file); + ch = fgetc(m_file); while(strchr("\r\n", ch)) - ch = fgetc(cx.file); + ch = fgetc(m_file); if(ch != '\\') - ungetc(ch, cx.file); + ungetc(ch, m_file); } // Escaped backslash else if(empty && ch == '\\') { - sendData(cx, L'\\'); + sendData(L'\\'); break; } // Escaped braces else if(empty && ch == '{') { - sendData(cx, L'{'); + sendData(L'{'); } else if(empty && ch == '}') { - sendData(cx, L'}'); + sendData(L'}'); } // Non breaking space else if(empty && ch == '~') { - sendData(cx, 0x00A0); + sendData(0x00A0); break; } // Optional hyphen else if(empty && ch == '-') { - sendData(cx, 0x00AD); + sendData(0x00AD); break; } @@ -374,7 +369,7 @@ bool RtfParser::parseControlWord(RtfContext& cx) // Anything else (including a backslash ends a control word) else { - ungetc(ch, cx.file); + ungetc(ch, m_file); break; } } @@ -395,29 +390,29 @@ bool RtfParser::parseControlWord(RtfContext& cx) // Here we check for common characters if(controlword == "emdash") - sendData(cx, 0x2014); + sendData(0x2014); else if(controlword == "endash") - sendData(cx, 0x2013); + sendData(0x2013); else if(controlword == "emspace") - sendData(cx, 0x2003); + sendData(0x2003); else if(controlword == "enspace") - sendData(cx, 0x2002); + sendData(0x2002); else if(controlword == "bullet") - sendData(cx, 0x2022); + sendData(0x2022); else if(controlword == "lquote") - sendData(cx, 0x2018); + sendData(0x2018); else if(controlword == "rquote") - sendData(cx, 0x2019); + sendData(0x2019); else if(controlword == "ldblquote") - sendData(cx, 0x201C); + sendData(0x201C); else if(controlword == "rdblquote") - sendData(cx, 0x201D); + sendData(0x201D); // Unicode values get sent through else if(m_parseUnicode && flags & RtfHandler::kHasParam && controlword == "u" ) { - sendData(cx, numPar); + sendData(numPar); m_uniEat = m_uniEatStack.top(); } @@ -438,7 +433,7 @@ bool RtfParser::parseControlWord(RtfContext& cx) else { if(m_handler) - sendControlWord(cx, controlword, flags, numPar); + sendControlWord(controlword, flags, numPar); } } diff --git a/src/rtfparser.h b/src/rtfparser.h index e1636fc..9509a35 100644 --- a/src/rtfparser.h +++ b/src/rtfparser.h @@ -86,26 +86,20 @@ protected: StackInt m_uniEatStack; int m_uniEat; -private: - - // TODO: Why aren't these just members? + FILE* m_file; // The file we're currently parsing + wstring m_dataBuffer; // The block of data we're caching to send - struct RtfContext - { - FILE* file; // The current file being parsed - wstring data; // Any data stored up ready to be sent to handler - bool isData; // TODO: Do we need this? - }; +private: // Parse helpers - bool parseControlWord(RtfContext& cx); - bool parseHexChar(RtfContext& cx, int num); + bool parseControlWord(); + bool parseHexChar(int num); // Convenience functions for calling the handler - void sendControlWord(RtfContext& cx, string cw, int flags, int param); - void sendData(RtfContext& cx, wchar_t ch); - void sendData(RtfContext& cx, wstring data); - void flushData(RtfContext& cx); + void sendControlWord(string cw, int flags, int param); + void sendData(wchar_t ch); + void sendData(wstring data); + void flushData(); }; /* |