OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimCsvFile.cpp
Go to the documentation of this file.
2 #include <iostream>
3 #include <iterator>
4 static const ossim_uint32 DEFAULT_BUFFER_SIZE = 1024;
5 
7 static std::istream& csvSkipWhiteSpace(std::istream& in)
8 {
9  int c = in.peek();
10  while(!in.fail()&& ( (c == ' ')||(c == '\t')||(c == '\n')||(c == '\r') ) )
11  {
12  in.ignore(1);
13  c = in.peek();
14  }
15 
16  return in;
17 }
18 
20 :theCsvFile(csvFile)
21 {
22 }
23 
25  ossimString& value)const
26 {
27  bool result = false;
28  if(theCsvFile)
29  {
30  ossim_int32 idx = theCsvFile->indexOfField(fieldName);
31  if((idx > 0)&&(idx < (ossim_int32)theValues.size()))
32  {
33  value = theValues[idx];
34  result = true;
35  }
36  }
37 
38  return result;
39 }
40 
42  ossimString& value)const
43 {
44  bool result = false;
45  if(idx < theValues.size())
46  {
47  value = theValues[idx];
48  result = true;
49  }
50 
51  return result;
52 }
53 
55 {
56  if(theCsvFile)
57  {
58  ossim_int32 idx = theCsvFile->indexOfField(fieldName);
59  if((idx >= 0)&&(idx < (ossim_int32)theValues.size()))
60  {
61  return theValues[idx];
62  }
63  }
64 
65  return theDummyValue;
66 }
67 
69 {
70  if(theCsvFile)
71  {
72  ossim_int32 idx = theCsvFile->indexOfField(fieldName);
73  if((idx >= 0)&&(idx < (ossim_int32)theValues.size()))
74  {
75  return theValues[idx];
76  }
77  }
78 
79  return theDummyValue;
80 }
81 
83 {
84  if(idx < theValues.size())
85  {
86  return theValues[idx];
87  }
88 
89  return theDummyValue;
90 }
91 
93 {
94  if(idx < theValues.size())
95  {
96  return theValues[idx];
97  }
98 
99  return theDummyValue;
100 }
101 
103 :theInputStream(0),
104 theSeparatorList(separatorList),
105  theOpenFlag(false)
106 {
107 }
108 
110 {
111  close();
112 }
113 
115  ossimCsvFile::StringListType& tokens)const
116 {
117  tokens.clear();
118  bool done = false;
119  char c;
120  const char quote = '\"';
121  bool inQuotedString = false;
122  bool inDoubleQuote = false;
123  ossimString currentToken;
124  inStream >> csvSkipWhiteSpace;
125  while(!done&&inStream.get(c)&&inStream.good())
126  {
127  if(c > 0x7e )
128  {
129  return false;
130  }
131  if((c!='\n')&&
132  (c!='\r'))
133  {
134  // check to see if we are quoted and check to see if double quoted
135  if(c == quote)
136  {
137  // check if at a double quote
138  if(inStream.peek() == quote)
139  {
140  currentToken += quote;
141  inStream.ignore(1);
142  if(!inDoubleQuote)
143  {
144  inDoubleQuote = true;
145  }
146  else
147  {
148  inDoubleQuote = false;
149  }
150  }
151  else
152  {
153  if(!inQuotedString)
154  {
155  inQuotedString = true;
156  }
157  else
158  {
159  inQuotedString = false;
160  }
161  }
162  }
163  // if we are at a separator then check to see if we are inside a quoted string
164  else if(theSeparatorList.contains(c))
165  {
166  // ignore token separator if quoted
167  if(inQuotedString||inDoubleQuote)
168  {
169  currentToken += c;
170  }
171  else
172  {
173  // push the current token.
174  currentToken = currentToken.trim();
175  tokens.push_back(currentToken);
176  currentToken = "";
177  inStream >> csvSkipWhiteSpace;
178  }
179  }
180  else
181  {
182  currentToken += c;
183  }
184  }
185  else if(!inQuotedString||inDoubleQuote)
186  {
187  currentToken = currentToken.trim();
188  tokens.push_back(currentToken);
189  done = true;
190  }
191  else
192  {
193  currentToken += c;
194  }
195  }
196 
197  return (inStream.good()&&(tokens.size()>0));
198 }
199 
200 bool ossimCsvFile::open(const ossimFilename& file, const ossimString& flags)
201 {
202  close();
203 
204  if((*flags.begin()) == 'r')
205  {
206  theInputStream = new std::ifstream(file.c_str(), std::ios::in|std::ios::binary);
207  theOpenFlag = true;
209  }
210  else
211  {
212  return theOpenFlag;
213  }
214 
215  return theOpenFlag;
216 }
217 
219 {
220  if(theOpenFlag)
221  {
222  theFieldHeaderList.clear();
224  }
225 
226  return false;
227 }
228 
230 {
231  if(theOpenFlag)
232  {
233  theFieldHeaderList.clear();
234  if(theInputStream)
235  {
236  delete theInputStream;
237  theInputStream = 0;
238  }
239  theOpenFlag = false;
240  theRecordBuffer = 0;
241  }
242 }
244 {
246 
247  if(theFieldHeaderList.empty())
248  {
249  if(!readHeader())
250  {
252  }
253  }
255  {
257  }
258  return theRecordBuffer;
259 }
260 
262 {
263  return theFieldHeaderList;
264 }
265 
267 {
269  ossim_uint32 idx = 0;
270  for(;idx < theFieldHeaderList.size(); ++idx)
271  {
272  if(theFieldHeaderList[idx] == fieldName)
273  {
274  result = (ossim_int32)idx;
275  break;
276  }
277  }
278 
279  return result;
280 }
ossimRefPtr< ossimCsvFile::Record > nextRecord()
Read one record and returns null if no more records exist or returns a valid pointer if a record exis...
ossimRefPtr< ossimCsvFile::Record > theRecordBuffer
Definition: ossimCsvFile.h:99
StringListType & values()
Definition: ossimCsvFile.h:36
bool open(const ossimFilename &file, const ossimString &flags="r")
For now we will only support the read flag and open existing csv files.
StringListType theFieldHeaderList
Definition: ossimCsvFile.h:98
std::basic_ifstream< char > ifstream
Class for char input file streams.
Definition: ossimIosFwd.h:44
std::istream * theInputStream
Definition: ossimCsvFile.h:100
static ossim_int32 INVALID_INDEX
Definition: ossimCsvFile.h:94
bool contains(char aChar) const
Definition: ossimString.h:58
void push_back(char c)
Equivalent to insert(end(), c).
Definition: ossimString.h:905
bool readCsvLine(std::istream &inStream, ossimCsvFile::StringListType &tokens) const
ossimString & operator[](const ossimString &fieldName)
Allows one to access and write to the field.
std::string::iterator begin()
Definition: ossimString.h:420
unsigned int ossim_uint32
ossimString trim(const ossimString &valueToTrim=ossimString(" \\)) const
this will strip lead and trailing character passed in.
ossim_int32 indexOfField(const ossimString &fieldName) const
Record(ossimCsvFile *csvFile)
friend class Record
Definition: ossimCsvFile.h:67
ossimString theSeparatorList
Definition: ossimCsvFile.h:101
std::basic_istream< char > istream
Base class for char input streams.
Definition: ossimIosFwd.h:20
ossimCsvFile(const ossimString &separatorList=",")
const StringListType & fieldHeaderList() const
Returns the header of the CSV file.
const char * c_str() const
Returns a pointer to a null-terminated array of characters representing the string&#39;s contents...
Definition: ossimString.h:396
bool valueAt(const ossimString &fieldName, ossimString &value) const
Allows access to a field as read only.
std::vector< ossimString > StringListType
Definition: ossimCsvFile.h:15
int ossim_int32