src/lib/UnescapingINIHandler.h
branchv_0
changeset 27 fd669e73d39a
parent 26 80e129ec3408
equal deleted inserted replaced
26:80e129ec3408 27:fd669e73d39a
    29 namespace lib {
    29 namespace lib {
    30 
    30 
    31 class UnescapingINIContentHandler : public INIContentHandler {
    31 class UnescapingINIContentHandler : public INIContentHandler {
    32 private:
    32 private:
    33 	INIContentHandler& output;
    33 	INIContentHandler& output;
       
    34 	bool unescapeComments;
    34 
    35 
    35 protected:
    36 protected:
    36 	const char ESC = '\\';
    37 	const char ESC = '\\';
    37 	bool lastEscaphingPhase;
    38 	bool lastEscaphingPhase;
    38 
    39 
    51 	 * @param lastEscaphingPhase instances of UnescapingINIContentHandler might be chained:
    52 	 * @param lastEscaphingPhase instances of UnescapingINIContentHandler might be chained:
    52 	 * unsupported escaping sequences are kept untouched to be processed in further phases;
    53 	 * unsupported escaping sequences are kept untouched to be processed in further phases;
    53 	 * in the last phase, all remaining sequences (including \\) must be recognized and unescaped
    54 	 * in the last phase, all remaining sequences (including \\) must be recognized and unescaped
    54 	 * (otherwise the input is considered invalid and an exception is thrown)
    55 	 * (otherwise the input is considered invalid and an exception is thrown)
    55 	 */
    56 	 */
    56 	UnescapingINIContentHandler(INIContentHandler& output, bool lastEscaphingPhase) : output(output), lastEscaphingPhase(lastEscaphingPhase) {
    57 	UnescapingINIContentHandler(INIContentHandler& output, bool lastEscaphingPhase, bool unescapeComments = false) : output(output), lastEscaphingPhase(lastEscaphingPhase), unescapeComments(unescapeComments) {
    57 	}
    58 	}
    58 
    59 
    59 	void startDocument() override {
    60 	void startDocument() override {
    60 		output.startDocument();
    61 		output.startDocument();
    61 	}
    62 	}
    65 	}
    66 	}
    66 
    67 
    67 	void startSection(const SectionStartEvent& event) override {
    68 	void startSection(const SectionStartEvent& event) override {
    68 		SectionStartEvent e = event;
    69 		SectionStartEvent e = event;
    69 		e.name = unescape(e.name);
    70 		e.name = unescape(e.name);
       
    71 		if (unescapeComments) e.comment = unescape(e.comment);
    70 		output.startSection(e);
    72 		output.startSection(e);
    71 	}
    73 	}
    72 
    74 
    73 	void endSection() override {
    75 	void endSection() override {
    74 		output.endSection();
    76 		output.endSection();
    78 		EntryEvent e = event;
    80 		EntryEvent e = event;
    79 		e.key = unescape(e.key);
    81 		e.key = unescape(e.key);
    80 		e.fullKey = unescape(e.fullKey);
    82 		e.fullKey = unescape(e.fullKey);
    81 		e.subKey = unescape(e.subKey);
    83 		e.subKey = unescape(e.subKey);
    82 		e.value = unescape(e.value);
    84 		e.value = unescape(e.value);
       
    85 		if (unescapeComments) e.comment = unescape(e.comment);
    83 		output.entry(e);
    86 		output.entry(e);
    84 	}
    87 	}
    85 
    88 
    86 	void comment(const CommentEvent& event) override {
    89 	void comment(const CommentEvent& event) override {
    87 		output.comment(event);
    90 		if (unescapeComments) {
       
    91 			CommentEvent e = event;
       
    92 			e.comment = unescape(e.comment);
       
    93 			output.comment(e);
       
    94 		} else {
       
    95 			output.comment(event);
       
    96 		}
    88 	}
    97 	}
    89 
    98 
    90 	void whitespace(const WhitespaceEvent& event) override {
    99 	void whitespace(const WhitespaceEvent& event) override {
    91 		output.whitespace(event);
   100 		output.whitespace(event);
    92 	}
   101 	}