OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
Classes | Public Types | Public Member Functions | Static Public Attributes | Protected Member Functions | Protected Attributes | Friends | List of all members
ossimCsvFile Class Reference

#include <ossimCsvFile.h>

Inheritance diagram for ossimCsvFile:
ossimReferenced

Classes

class  Record
 

Public Types

typedef std::vector< ossimStringStringListType
 

Public Member Functions

 ossimCsvFile (const ossimString &separatorList=",")
 
 ~ossimCsvFile ()
 
bool open (const ossimFilename &file, const ossimString &flags="r")
 For now we will only support the read flag and open existing csv files. More...
 
void close ()
 
bool readHeader ()
 
ossimRefPtr< ossimCsvFile::RecordnextRecord ()
 Read one record and returns null if no more records exist or returns a valid pointer if a record exists. More...
 
const StringListTypefieldHeaderList () const
 Returns the header of the CSV file. More...
 
ossim_int32 indexOfField (const ossimString &fieldName) const
 
- Public Member Functions inherited from ossimReferenced
 ossimReferenced ()
 
 ossimReferenced (const ossimReferenced &)
 
ossimReferencedoperator= (const ossimReferenced &)
 
void ref () const
 increment the reference count by one, indicating that this object has another pointer which is referencing it. More...
 
void unref () const
 decrement the reference count by one, indicating that a pointer to this object is referencing it. More...
 
void unref_nodelete () const
 decrement the reference count by one, indicating that a pointer to this object is referencing it. More...
 
int referenceCount () const
 

Static Public Attributes

static ossim_int32 INVALID_INDEX = -1
 

Protected Member Functions

bool readCsvLine (std::istream &inStream, ossimCsvFile::StringListType &tokens) const
 
- Protected Member Functions inherited from ossimReferenced
virtual ~ossimReferenced ()
 

Protected Attributes

StringListType theFieldHeaderList
 
ossimRefPtr< ossimCsvFile::RecordtheRecordBuffer
 
std::istream * theInputStream
 
ossimString theSeparatorList
 
bool theOpenFlag
 

Friends

class Record
 

Detailed Description

Definition at line 12 of file ossimCsvFile.h.

Member Typedef Documentation

◆ StringListType

Definition at line 15 of file ossimCsvFile.h.

Constructor & Destructor Documentation

◆ ossimCsvFile()

ossimCsvFile::ossimCsvFile ( const ossimString separatorList = ",")

Definition at line 102 of file ossimCsvFile.cpp.

103 :theInputStream(0),
104 theSeparatorList(separatorList),
105  theOpenFlag(false)
106 {
107 }
std::istream * theInputStream
Definition: ossimCsvFile.h:100
ossimString theSeparatorList
Definition: ossimCsvFile.h:101

◆ ~ossimCsvFile()

ossimCsvFile::~ossimCsvFile ( )

Definition at line 109 of file ossimCsvFile.cpp.

References close().

110 {
111  close();
112 }

Member Function Documentation

◆ close()

void ossimCsvFile::close ( )

Definition at line 229 of file ossimCsvFile.cpp.

References theFieldHeaderList, theInputStream, theOpenFlag, and theRecordBuffer.

Referenced by open(), and ~ossimCsvFile().

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 }
ossimRefPtr< ossimCsvFile::Record > theRecordBuffer
Definition: ossimCsvFile.h:99
StringListType theFieldHeaderList
Definition: ossimCsvFile.h:98
std::istream * theInputStream
Definition: ossimCsvFile.h:100

◆ fieldHeaderList()

const ossimCsvFile::StringListType & ossimCsvFile::fieldHeaderList ( ) const

Returns the header of the CSV file.

Definition at line 261 of file ossimCsvFile.cpp.

References theFieldHeaderList.

262 {
263  return theFieldHeaderList;
264 }
StringListType theFieldHeaderList
Definition: ossimCsvFile.h:98

◆ indexOfField()

ossim_int32 ossimCsvFile::indexOfField ( const ossimString fieldName) const

Definition at line 266 of file ossimCsvFile.cpp.

References INVALID_INDEX, and theFieldHeaderList.

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 }
StringListType theFieldHeaderList
Definition: ossimCsvFile.h:98
static ossim_int32 INVALID_INDEX
Definition: ossimCsvFile.h:94
unsigned int ossim_uint32
int ossim_int32

◆ nextRecord()

ossimRefPtr< ossimCsvFile::Record > ossimCsvFile::nextRecord ( )

Read one record and returns null if no more records exist or returns a valid pointer if a record exists.

Definition at line 243 of file ossimCsvFile.cpp.

References readCsvLine(), readHeader(), theFieldHeaderList, theInputStream, theOpenFlag, theRecordBuffer, and ossimCsvFile::Record::values().

244 {
246 
247  if(theFieldHeaderList.empty())
248  {
249  if(!readHeader())
250  {
252  }
253  }
255  {
257  }
258  return theRecordBuffer;
259 }
ossimRefPtr< ossimCsvFile::Record > theRecordBuffer
Definition: ossimCsvFile.h:99
StringListType & values()
Definition: ossimCsvFile.h:36
StringListType theFieldHeaderList
Definition: ossimCsvFile.h:98
std::istream * theInputStream
Definition: ossimCsvFile.h:100
bool readCsvLine(std::istream &inStream, ossimCsvFile::StringListType &tokens) const

◆ open()

bool ossimCsvFile::open ( const ossimFilename file,
const ossimString flags = "r" 
)

For now we will only support the read flag and open existing csv files.

Later we can add support for creating and writing csv files

Definition at line 200 of file ossimCsvFile.cpp.

References ossimString::begin(), ossimString::c_str(), close(), Record, theInputStream, theOpenFlag, and theRecordBuffer.

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 }
ossimRefPtr< ossimCsvFile::Record > theRecordBuffer
Definition: ossimCsvFile.h:99
std::basic_ifstream< char > ifstream
Class for char input file streams.
Definition: ossimIosFwd.h:44
std::istream * theInputStream
Definition: ossimCsvFile.h:100
std::string::iterator begin()
Definition: ossimString.h:420
friend class Record
Definition: ossimCsvFile.h:67
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

◆ readCsvLine()

bool ossimCsvFile::readCsvLine ( std::istream &  inStream,
ossimCsvFile::StringListType tokens 
) const
protected

Definition at line 114 of file ossimCsvFile.cpp.

Referenced by nextRecord(), and readHeader().

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 }
bool contains(char aChar) const
Definition: ossimString.h:58
void push_back(char c)
Equivalent to insert(end(), c).
Definition: ossimString.h:905
ossimString trim(const ossimString &valueToTrim=ossimString(" \\)) const
this will strip lead and trailing character passed in.
ossimString theSeparatorList
Definition: ossimCsvFile.h:101

◆ readHeader()

bool ossimCsvFile::readHeader ( )

Definition at line 218 of file ossimCsvFile.cpp.

References readCsvLine(), theFieldHeaderList, theInputStream, and theOpenFlag.

Referenced by nextRecord().

219 {
220  if(theOpenFlag)
221  {
222  theFieldHeaderList.clear();
224  }
225 
226  return false;
227 }
StringListType theFieldHeaderList
Definition: ossimCsvFile.h:98
std::istream * theInputStream
Definition: ossimCsvFile.h:100
bool readCsvLine(std::istream &inStream, ossimCsvFile::StringListType &tokens) const

Friends And Related Function Documentation

◆ Record

friend class Record
friend

Definition at line 67 of file ossimCsvFile.h.

Referenced by open().

Member Data Documentation

◆ INVALID_INDEX

ossim_int32 ossimCsvFile::INVALID_INDEX = -1
static

Definition at line 94 of file ossimCsvFile.h.

Referenced by indexOfField().

◆ theFieldHeaderList

StringListType ossimCsvFile::theFieldHeaderList
protected

Definition at line 98 of file ossimCsvFile.h.

Referenced by close(), fieldHeaderList(), indexOfField(), nextRecord(), and readHeader().

◆ theInputStream

std::istream* ossimCsvFile::theInputStream
protected

Definition at line 100 of file ossimCsvFile.h.

Referenced by close(), nextRecord(), open(), and readHeader().

◆ theOpenFlag

bool ossimCsvFile::theOpenFlag
protected

Definition at line 102 of file ossimCsvFile.h.

Referenced by close(), nextRecord(), open(), and readHeader().

◆ theRecordBuffer

ossimRefPtr<ossimCsvFile::Record> ossimCsvFile::theRecordBuffer
protected

Definition at line 99 of file ossimCsvFile.h.

Referenced by close(), nextRecord(), and open().

◆ theSeparatorList

ossimString ossimCsvFile::theSeparatorList
protected

Definition at line 101 of file ossimCsvFile.h.


The documentation for this class was generated from the following files: