ARTS  2.4.0(git:4fb77825)
sourcetext.cc
Go to the documentation of this file.
1 /* Copyright (C) 2012 Oliver Lemke <olemke@core-dump.info>
2 
3  This program is free software; you can redistribute it and/or modify it
4  under the terms of the GNU General Public License as published by the
5  Free Software Foundation; either version 2, or (at your option) any
6  later version.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16  USA. */
17 
18 #include "sourcetext.h"
19 #include <iostream>
20 #include "file.h"
21 
22 void SourceText::AppendFile(const String& name) {
23  mSfLine.push_back(mText.nelem());
24  mSfName.push_back(name);
25 
27 }
28 
30  if (mColumn < mText[mLine].nelem() - 1) {
31  ++mColumn;
32  } else {
33  mLineBreak = true;
34  do {
35  if (mLine >= mText.nelem()) {
36  throw Eot("", this->File(), this->Line(), this->Column());
37  } else if (mLine == mText.nelem() - 1) {
38  mColumn++;
39  break;
40  } else {
41  ++mLine;
42  mColumn = 0;
43  }
44  } while (1 > mText[mLine].nelem()); // Skip empty lines.
45  }
46 }
47 
49  mLineBreak = true;
50  mColumn = 0;
51  do {
52  if (mLine >= mText.nelem() - 1) {
53  throw Eot("", this->File(), this->Line(), this->Column());
54  } else {
55  ++mLine;
56  }
57  } while (1 > mText[mLine].nelem()); // Skip empty lines.
58 }
59 
61  Index i = 0;
62  bool stop = false;
63 
64  while (i < mSfLine.nelem() - 1 && !stop) {
65  if (mLine >= mSfLine[i + 1])
66  ++i;
67  else
68  stop = true;
69  }
70 
71  return mSfName[i];
72 }
73 
75  mLine = 0;
76  mColumn = 0;
77 
78  if (1 > mText.nelem()) {
79  throw Eot("Empty text!", this->File(), this->Line(), this->Column());
80  } else {
81  // Skip empty lines:
82  while (1 > mText[mLine].nelem()) {
83  if (mLine >= mText.nelem() - 1) {
84  throw Eot("", this->File(), this->Line(), this->Column());
85  } else {
86  mLineBreak = true;
87  ++mLine;
88  }
89  }
90  }
91 }
92 
94  Index i = 0;
95  bool stop = false;
96 
97  while (i < mSfLine.nelem() - 1 && !stop) {
98  if (line >= mSfLine[i + 1])
99  ++i;
100  else
101  stop = true;
102  }
103 
104  return line - mSfLine[i] + 1;
105 }
106 
107 std::ostream& operator<<(std::ostream& os, const SourceText& text) {
108  for (Index i = 0; i < text.mText.nelem(); ++i)
109  os << i << "(" << text.mText[i].nelem() << ")"
110  << ": " << text.mText[i] << '\n';
111  return (os);
112 }
SourceText::AppendFile
void AppendFile(const String &name)
Appends contents of file to the source text.
Definition: sourcetext.cc:22
SourceText::GetSourceLine
Index GetSourceLine(const Index line)
Return the line number, but for the file that is associated with the given position.
Definition: sourcetext.cc:93
SourceText::mColumn
Index mColumn
Column position in the text.
Definition: sourcetext.h:133
SourceText::Init
void Init()
This sets the pointer to the first existing character in the text.
Definition: sourcetext.cc:74
SourceText::mSfName
ArrayOfString mSfName
Names associated with.
Definition: sourcetext.h:145
SourceText::mText
ArrayOfString mText
The text.
Definition: sourcetext.h:127
SourceText::mLine
Index mLine
Line position in the text.
Definition: sourcetext.h:130
SourceText::mLineBreak
bool mLineBreak
Is set to true if the last operation caused a line break.
Definition: sourcetext.h:149
SourceText::Line
Index Line()
Return the line number, but for the file that is associated with the current position.
Definition: sourcetext.h:76
Absorption::nelem
Index nelem(const Lines &l)
Number of lines.
Definition: absorptionlines.h:1820
SourceText::mSfLine
ArrayOfIndex mSfLine
Remember where which source file starts.
Definition: sourcetext.h:142
SourceText::AdvanceLine
void AdvanceLine()
Advances position pointer by one line.
Definition: sourcetext.cc:48
SourceText::Column
Index Column()
Return the current column.
Definition: sourcetext.h:89
my_basic_string< char >
Eot
Definition: exceptions.h:67
SourceText::File
const String & File()
Return the filename associated with the current position.
Definition: sourcetext.cc:60
SourceText
A smart class to hold the text for parsing.
Definition: sourcetext.h:36
read_text_from_file
void read_text_from_file(ArrayOfString &text, const String &name)
Reads an ASCII file and appends the contents to the String vector text.
Definition: file.cc:225
file.h
This file contains basic functions to handle ASCII files.
SourceText::AdvanceChar
void AdvanceChar()
Advance position pointer by one character.
Definition: sourcetext.cc:29
sourcetext.h
Index
INDEX Index
The type to use for all integer numbers and indices.
Definition: matpack.h:39
operator<<
std::ostream & operator<<(std::ostream &os, const SourceText &text)
Definition: sourcetext.cc:107
Array::nelem
Index nelem() const
Number of elements.
Definition: array.h:195