OOFEM 3.0
Loading...
Searching...
No Matches
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 - 2025 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
42namespace oofem {
43Tokenizer :: Tokenizer() :
44 tokens()
45{ }
46
47
48std :: string
49Tokenizer :: 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
62std :: string
63Tokenizer :: 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
74std :: string
75Tokenizer :: 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
88std :: string
89Tokenizer :: 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
99std :: string
100Tokenizer :: 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
110void 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
140int Tokenizer :: giveNumberOfTokens()
141{
142 // if EOF currentTokens == -1
143 return ( int ) tokens.size();
144}
145
146const 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 readSimpleToken(std ::size_t &pos, const std ::string &line)
Definition tokenizer.C:89
std::string readStringToken(std ::size_t &pos, const std ::string &line)
Definition tokenizer.C:49
std::string readStructToken(std ::size_t &pos, const std ::string &line)
Definition tokenizer.C:63
std::string readSimpleExpressionToken(std ::size_t &pos, const std ::string &line)
Definition tokenizer.C:75
std ::vector< std ::string > tokens
Array of tokens.
Definition tokenizer.h:54
std::string readToken(std ::size_t &pos, const std ::string &line, char sep)
Definition tokenizer.C:100
#define OOFEM_WARNING(...)
Definition error.h:80

This page is part of the OOFEM-3.0 documentation. Copyright Copyright (C) 1994-2025 Borek Patzak Bořek Patzák
Project e-mail: oofem@fsv.cvut.cz
Generated at for OOFEM by doxygen 1.15.0 written by Dimitri van Heesch, © 1997-2011