-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtokenstuff.h
More file actions
75 lines (75 loc) · 1.41 KB
/
tokenstuff.h
File metadata and controls
75 lines (75 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//(c)Copyright 2003 Thomas Fernandez
//Edited in 2014 by Michael Kossin
//All rights reserved.
#pragma once
#include <fstream>
#include <string>
#include <cstdlib>
#include <cstring>
#include <vector>
using namespace std;
inline string toLower(const string & s)
{
string result=s;
for(int i=0;i<result.length();i++)result[i]=tolower(result[i]);
return result;
}
inline bool getString(ifstream& infile,string& s)
{
char temp[2048];
strcpy(temp,"");
bool endoffile=infile.eof();
while((!endoffile)&&(strlen(temp)==0))
{
infile.getline(temp,2047);
endoffile=infile.eof();
s=temp;
}
return endoffile;
}
inline bool isAlpha(char c)
{
return((c>='a')&&(c<='z'))||((c>='A')&&(c<='Z'));
}
inline void getStart(string s,int& p)
{
while(p<s.length()&&!isAlpha(s[p]))
{
p++;
}
}
inline void getEnd(string s,int& p)
{
while (p<s.length()&&isAlpha(s[p]))
{
p++;
}
}
inline vector<string> tokenize(string x)
{
vector<string> result;
int itoke=0;
int start,end;
start=0;
while (start<x.length())
{
getStart(x,start);
if(start<x.length())
{
end=start+1;
getEnd(x,end);
result.push_back(x.substr(start,end-start));
itoke++;
start=end+1;
}
}
return result;
}
inline bool getStringAndTokens(ifstream& infile,string& line,vector<string>& tokens)
{
bool endoffile=getString(infile,line);
tokens=tokenize(line);
for(int i=0;i<tokens.size();i++)
tokens[i]=toLower(tokens[i]);
return endoffile;
}