ARTS  2.2.66
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 <iostream>
19 #include "sourcetext.h"
20 #include "file.h"
21 
22 
23 void SourceText::AppendFile(const String& name)
24 {
25  mSfLine.push_back(mText.nelem());
26  mSfName.push_back(name);
27 
28  read_text_from_file(mText, name);
29 }
30 
31 
33 {
34  if ( mColumn < mText[mLine].nelem()-1 )
35  {
36  ++mColumn;
37  }
38  else
39  {
40  mLineBreak = true;
41  do
42  {
43  if (mLine>=mText.nelem())
44  {
45  throw Eot( "",
46  this->File(),
47  this->Line(),
48  this->Column() );
49  }
50  else if (mLine==mText.nelem()-1)
51  {
52  mColumn++;
53  break;
54  }
55  else
56  {
57  ++mLine;
58  mColumn = 0;
59  }
60  }
61  while ( 1 > mText[mLine].nelem() ); // Skip empty lines.
62  }
63 }
64 
65 
67 {
68  mLineBreak = true;
69  mColumn = 0;
70  do
71  {
72  if (mLine>=mText.nelem()-1)
73  {
74  throw Eot( "",
75  this->File(),
76  this->Line(),
77  this->Column() );
78  }
79  else
80  {
81  ++mLine;
82  }
83  }
84  while ( 1 > mText[mLine].nelem() ); // Skip empty lines.
85 }
86 
87 
89 {
90  Index i = 0;
91  bool stop = false;
92 
93  while ( i<mSfLine.nelem()-1 && !stop )
94  {
95  if (mLine>=mSfLine[i+1]) ++i;
96  else stop = true;
97  }
98 
99  return mSfName[i];
100 }
101 
102 
104 {
105  Index i = 0;
106  bool stop = false;
107 
108  while ( i<mSfLine.nelem()-1 && !stop )
109  {
110  if (mLine>=mSfLine[i+1]) ++i;
111  else stop = true;
112  }
113 
114  return mLine - mSfLine[i] + 1;
115 }
116 
117 
119 {
120  mLine = 0;
121  mColumn = 0;
122 
123  if ( 1 > mText.nelem() )
124  {
125  throw Eot( "Empty text!",
126  this->File(),
127  this->Line(),
128  this->Column() );
129  }
130  else
131  {
132  // Skip empty lines:
133  while ( 1 > mText[mLine].nelem() )
134  {
135  if (mLine>=mText.nelem()-1)
136  {
137  throw Eot( "",
138  this->File(),
139  this->Line(),
140  this->Column() );
141  }
142  else
143  {
144  mLineBreak = true;
145  ++mLine;
146  }
147  }
148  }
149 }
150 
151 
152 std::ostream& operator << (std::ostream& os, const SourceText& text)
153 {
154  for (Index i=0; i<text.mText.nelem();++i)
155  os << i
156  << "(" << text.mText[i].nelem() << ")"
157  << ": " << text.mText[i] << '\n';
158  return(os);
159 }
160 
const String & File()
Return the filename associated with the current position.
Definition: sourcetext.cc:88
INDEX Index
The type to use for all integer numbers and indices.
Definition: matpack.h:35
Index nelem() const
Number of elements.
Definition: array.h:176
This file contains basic functions to handle ASCII files.
void AdvanceChar()
Advance position pointer by one character.
Definition: sourcetext.cc:32
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:252
void AppendFile(const String &name)
Appends contents of file to the source text.
Definition: sourcetext.cc:23
Index mColumn
Column position in the text.
Definition: sourcetext.h:120
Definition: exceptions.h:67
ArrayOfString mSfName
Names associated with.
Definition: sourcetext.h:126
A smart class to hold the text for parsing.
Definition: sourcetext.h:37
void Init()
This sets the pointer to the first existing character in the text.
Definition: sourcetext.cc:118
friend std::ostream & operator<<(std::ostream &os, const SourceText &text)
Output operator for SourceText.
Definition: sourcetext.cc:152
ArrayOfString mText
The text.
Definition: sourcetext.h:114
Index mLine
Line position in the text.
Definition: sourcetext.h:117
bool mLineBreak
Is set to true if the last operation caused a line break.
Definition: sourcetext.h:130
ArrayOfIndex mSfLine
Remember where which source file starts.
Definition: sourcetext.h:123
void AdvanceLine()
Advances position pointer by one line.
Definition: sourcetext.cc:66
Index Column()
Return the current column.
Definition: sourcetext.h:87
Index Line()
Return the line number, but for the file that is associated with the current position.
Definition: sourcetext.cc:103