OOFEM  2.4
OOFEM.org - Object Oriented Finite Element Solver
tokenizer.C
Go to the documentation of this file.
1 /*
2  *
3  * ##### ##### ###### ###### ### ###
4  * ## ## ## ## ## ## ## ### ##
5  * ## ## ## ## #### #### ## # ##
6  * ## ## ## ## ## ## ## ##
7  * ## ## ## ## ## ## ## ##
8  * ##### ##### ## ###### ## ##
9  *
10  *
11  * OOFEM : Object Oriented Finite Element Code
12  *
13  * Copyright (C) 1993 - 2013 Borek Patzak
14  *
15  *
16  *
17  * Czech Technical University, Faculty of Civil Engineering,
18  * Department of Structural Mechanics, 166 29 Prague, Czech Republic
19  *
20  * This library is free software; you can redistribute it and/or
21  * modify it under the terms of the GNU Lesser General Public
22  * License as published by the Free Software Foundation; either
23  * version 2.1 of the License, or (at your option) any later version.
24  *
25  * This program is distributed in the hope that it will be useful,
26  * but WITHOUT ANY WARRANTY; without even the implied warranty of
27  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
28  * Lesser General Public License for more details.
29  *
30  * You should have received a copy of the GNU Lesser General Public
31  * License along with this library; if not, write to the Free Software
32  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
33  */
34 
35 #include "tokenizer.h"
36 #include "error.h"
37 
38 #include <cctype>
39 #include <list>
40 #include <iterator>
41 
42 namespace oofem {
44  tokens()
45 { }
46 
47 
48 std :: string
49 Tokenizer :: readStringToken(std :: size_t &pos, const std :: string &line)
50 {
51  pos++;
52  std :: string x = this->readToken(pos, line, '"'); // read everything up to terminating '"' (or to the end of the string)
53  if ( line [ pos ] == '"' ) {
54  pos++; // check if terminating '"' was found
55  } else {
56  OOFEM_WARNING("Missing closing separator (\") inserted at end of line");
57  }
58  return x;
59 }
60 
61 
62 std :: string
63 Tokenizer :: readStructToken(std :: size_t &pos, const std :: string &line)
64 {
65  std :: string x = this->readToken(pos, line, '}'); // read everything up to terminating '}' (or to the end of the string)
66  if ( line [ pos ] == '}' ) {
67  pos++; // check if terminating '}' was found
68  } else {
69  OOFEM_WARNING("Missing closing separator (}) inserted at end of line");
70  }
71  return x + '}'; // structs are left with surrounding brackets, unlike strings ""
72 }
73 
74 std :: string
75 Tokenizer :: readSimpleExpressionToken(std :: size_t &pos, const std :: string &line)
76 {
77  pos++;
78  std :: string x = this->readToken(pos, line, '$'); // read everything up to terminating '$' (or to the end of the string)
79  if ( line [ pos ] == '$' ) {
80  pos++; // check if terminating '"' was found
81  } else {
82  OOFEM_WARNING("Missing closing separator (\"$\") inserted at end of line");
83  }
84  return '$' + x + '$'; // simple expressions are left with surrounding '$";
85 }
86 
87 
88 std :: string
89 Tokenizer :: readSimpleToken(std :: size_t &pos, const std :: string &line)
90 {
91  std :: size_t startpos = pos;
92  while ( pos < line.size() && !isspace(line [ pos ]) ) {
93  pos++;
94  }
95  return line.substr(startpos, pos - startpos);
96 }
97 
98 
99 std :: string
100 Tokenizer :: readToken(std :: size_t &pos, const std :: string &line, char sep)
101 {
102  std :: size_t startpos = pos;
103  while ( pos < line.size() && line [ pos ] != sep ) {
104  pos++;
105  }
106  return line.substr(startpos, pos - startpos);
107 }
108 
109 
110 void Tokenizer :: tokenizeLine(const std :: string &currentLine)
111 {
112  std :: list< std :: string >sList;
113  std :: size_t bpos = 0;
114  char c = 0;
115  int nTokens = 0;
116 
117  while ( bpos < currentLine.size() ) {
118  c = currentLine [ bpos ];
119 
120  if ( isspace(c) ) {
121  bpos++;
122  continue;
123  } else if ( c == '"' ) {
124  sList.push_back( this->readStringToken(bpos, currentLine) );
125  } else if ( c == '{' ) {
126  sList.push_back( this->readStructToken(bpos, currentLine) );
127  } else if ( c == '$' ) {
128  sList.push_back( this->readSimpleExpressionToken(bpos, currentLine) );
129  } else {
130  sList.push_back( this->readSimpleToken(bpos, currentLine) );
131  }
132  }
133 
134  // Clear the old stuff and copy list to vector
135  this->tokens.clear();
136  this->tokens.reserve(nTokens);
137  std :: copy( sList.begin(), sList.end(), std :: back_inserter(tokens) );
138 }
139 
141 {
142  // if EOF currentTokens == -1
143  return ( int ) tokens.size();
144 }
145 
146 const char *Tokenizer :: giveToken(int i)
147 {
148  // tokens are numbered from 1
149 
150  if ( i <= ( int ) tokens.size() ) {
151  return tokens [ i - 1 ].c_str();
152  } else {
153  return NULL;
154  }
155 }
156 } // end namespace oofem
std::string readSimpleExpressionToken(std::size_t &pos, const std::string &line)
Reads next simple expression token (section identified by starting with &#39;$&#39; and finishing with &#39;$&#39;)...
Definition: tokenizer.C:75
Tokenizer()
Constructor. Creates tokenizer with given character as separator.
Definition: tokenizer.C:43
std::string readSimpleToken(std::size_t &pos, const std::string &line)
Reads next simple token (stops when whitespace character is reached)
Definition: tokenizer.C:89
std::string readToken(std::size_t &pos, const std::string &line, char sep)
Reads next token (stops when separator is reached)
Definition: tokenizer.C:100
const char * giveToken(int i)
Returns pointer to i-th token.
Definition: tokenizer.C:146
void tokenizeLine(const std::string &line)
Tokenizes given record (string).
Definition: tokenizer.C:110
std::vector< std::string > tokens
Array of tokens.
Definition: tokenizer.h:54
std::string readStructToken(std::size_t &pos, const std::string &line)
Reads next structured token (bounded by &#39;{&#39; &#39;}&#39; pairs, possibly nested).
Definition: tokenizer.C:63
the oofem namespace is to define a context or scope in which all oofem names are defined.
#define OOFEM_WARNING(...)
Definition: error.h:62
int giveNumberOfTokens()
returns the number of tokens.
Definition: tokenizer.C:140
std::string readStringToken(std::size_t &pos, const std::string &line)
Reads next string token (quoted).
Definition: tokenizer.C:49

This page is part of the OOFEM documentation. Copyright (c) 2011 Borek Patzak
Project e-mail: info@oofem.org
Generated at Tue Jan 2 2018 20:07:31 for OOFEM by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2011