PageRenderTime 24ms CodeModel.GetById 12ms app.highlight 10ms RepoModel.GetById 0ms app.codeStats 1ms

/ghost-legacy/csvparser.cpp

http://ghostcb.googlecode.com/
C++ | 122 lines | 76 code | 13 blank | 33 comment | 19 complexity | d3e0e19993cf7e25fa7074e374d721d2 MD5 | raw file
  1/*
  2Copyright (c) 2001, Mayukh Bose
  3All rights reserved.
  4
  5Redistribution and use in source and binary forms, with or without
  6modification, are permitted provided that the following conditions are
  7met:
  8
  9* Redistributions of source code must retain the above copyright notice,
 10this list of conditions and the following disclaimer.  
 11
 12* Redistributions in binary form must reproduce the above copyright
 13notice, this list of conditions and the following disclaimer in the
 14documentation and/or other materials provided with the distribution.
 15
 16* Neither the name of Mayukh Bose nor the names of other
 17contributors may be used to endorse or promote products derived from
 18this software without specific prior written permission.
 19
 20THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 21"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 22LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 23A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 24OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 25SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 26LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 27DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 28THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 29(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 30OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 31*/
 32
 33#include <iostream>
 34#include <cstdlib>
 35#include "csvparser.h"
 36using namespace std;
 37
 38
 39CSVParser::CSVParser()
 40{
 41  m_sData = "";
 42  m_nPos = 0;
 43}
 44
 45void CSVParser::SkipSpaces(void)
 46{
 47  while (m_nPos < m_sData.length() && m_sData[m_nPos] == ' ')
 48    m_nPos++;
 49}
 50
 51const CSVParser & CSVParser::operator <<(const string & sIn)
 52{
 53  this->m_sData = sIn;
 54  this->m_nPos = 0;
 55  return *this;
 56}
 57
 58const CSVParser & CSVParser::operator <<(const char *sIn)
 59{
 60  this->m_sData = sIn;
 61  this->m_nPos = 0;
 62  return *this;
 63}
 64
 65CSVParser & CSVParser::operator >>(int & nOut)
 66{
 67  string sTmp = "";
 68  SkipSpaces();
 69  while (m_nPos < m_sData.length() && m_sData[m_nPos] != ',')
 70    sTmp += m_sData[m_nPos++];
 71
 72  m_nPos++; // skip past comma
 73  nOut = atoi(sTmp.c_str());
 74  return *this;
 75}
 76
 77CSVParser & CSVParser::operator >>(double & nOut)
 78{
 79  string sTmp = "";
 80  SkipSpaces();
 81  while (m_nPos < m_sData.length() && m_sData[m_nPos] != ',')
 82    sTmp += m_sData[m_nPos++];
 83
 84  m_nPos++; // skip past comma
 85  nOut = atof(sTmp.c_str());
 86  return *this;
 87}
 88
 89CSVParser & CSVParser::operator >>(string & sOut)
 90{
 91  bool bQuotes = false;
 92  sOut = "";
 93  SkipSpaces();
 94
 95  // Jump past first " if necessary
 96  if (m_nPos < m_sData.length() && m_sData[m_nPos] == '"') {
 97    bQuotes = true;
 98    m_nPos++; 
 99  }
100  
101  while (m_nPos < m_sData.length()) {
102    if (!bQuotes && m_sData[m_nPos] == ',')
103      break;
104    if (bQuotes && m_sData[m_nPos] == '"') {
105      if (m_nPos + 1 >= m_sData.length() - 1)
106        break;
107      if (m_sData[m_nPos+1] == ',')
108        break;
109    }
110    sOut += m_sData[m_nPos++];
111  }
112
113  // Jump past last " if necessary
114  if (bQuotes && m_nPos < m_sData.length() && m_sData[m_nPos] == '"')
115    m_nPos++; 
116
117  // Jump past , if necessary
118  if (m_nPos < m_sData.length() && m_sData[m_nPos] == ',')
119    m_nPos++; 
120
121  return *this;
122}