gui
[lbook_fbreader.git] / fbreader / src / formats / txt / TxtBookReader.cpp
blob1467a00817de7f1789aed82ffd10c8907ad0a839
1 /*
2 * Copyright (C) 2004-2008 Geometer Plus <contact@geometerplus.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA.
20 #include <cctype>
22 #include "TxtBookReader.h"
23 #include "../../bookmodel/BookModel.h"
25 TxtBookReader::TxtBookReader(BookModel &model, const PlainTextFormat &format, const std::string &encoding) : TxtReader(encoding), BookReader(model), myFormat(format) {
28 void TxtBookReader::internalEndParagraph() {
29 if (!myLastLineIsEmpty) {
30 myLineFeedCounter = 0;
32 myLastLineIsEmpty = true;
33 endParagraph();
36 bool TxtBookReader::characterDataHandler(std::string &str) {
37 const char *ptr = str.data();
38 const char *end = ptr + str.length();
39 for (; ptr != end; ++ptr) {
40 if (isspace((unsigned char)*ptr)) {
41 ++mySpaceCounter;
42 } else {
43 myLastLineIsEmpty = false;
44 break;
47 if (ptr != end) {
48 if ((myFormat.breakType() & PlainTextFormat::BREAK_PARAGRAPH_AT_LINE_WITH_INDENT) &&
49 myNewLine && (mySpaceCounter > myFormat.ignoredIndent())) {
50 internalEndParagraph();
51 beginParagraph();
53 addData(str);
54 if (myInsideContentsParagraph) {
55 addContentsData(str);
57 myNewLine = false;
59 return true;
62 bool TxtBookReader::newLineHandler() {
63 if (!myLastLineIsEmpty) {
64 myLineFeedCounter = -1;
66 myLastLineIsEmpty = true;
67 ++myLineFeedCounter;
68 myNewLine = true;
69 mySpaceCounter = 0;
70 bool paragraphBreak =
71 (myFormat.breakType() & PlainTextFormat::BREAK_PARAGRAPH_AT_NEW_LINE) ||
72 ((myFormat.breakType() & PlainTextFormat::BREAK_PARAGRAPH_AT_EMPTY_LINE) && (myLineFeedCounter > 0));
74 if (myFormat.createContentsTable()) {
75 if (!myInsideContentsParagraph && (myLineFeedCounter == myFormat.emptyLinesBeforeNewSection() + 1)) {
76 myInsideContentsParagraph = true;
77 internalEndParagraph();
78 insertEndOfSectionParagraph();
79 beginContentsParagraph();
80 enterTitle();
81 pushKind(SECTION_TITLE);
82 beginParagraph();
83 paragraphBreak = false;
85 if (myInsideContentsParagraph && (myLineFeedCounter == 1)) {
86 exitTitle();
87 endContentsParagraph();
88 popKind();
89 myInsideContentsParagraph = false;
90 paragraphBreak = true;
94 if (paragraphBreak) {
95 internalEndParagraph();
96 beginParagraph();
98 return true;
101 void TxtBookReader::startDocumentHandler() {
102 setMainTextModel();
103 pushKind(REGULAR);
104 beginParagraph();
105 myLineFeedCounter = 0;
106 myInsideContentsParagraph = false;
107 enterTitle();
108 myLastLineIsEmpty = true;
109 myNewLine = true;
110 mySpaceCounter = 0;
113 void TxtBookReader::endDocumentHandler() {
114 internalEndParagraph();