Main Page | Namespace List | Class Hierarchy | Class List | File List | Class Members | File Members

csvparser.cpp

Go to the documentation of this file.
00001 #include <iostream>
00002 #include <cstdlib>
00003 #include "csvparser.h"
00004 #include "../PartB/yieldCurve.h"
00005 #include "../PartF/creditCurve.h"
00006 #include "StringTokenizer.h"
00007 using namespace std;
00008 
00009 /*
00010 Copyright (c) 2001, Mayukh Bose
00011 All rights reserved.
00012 
00013 Redistribution and use in source and binary forms, with or without
00014 modification, are permitted provided that the following conditions are
00015 met:
00016 
00017 * Redistributions of source code must retain the above copyright notice,
00018 this list of conditions and the following disclaimer.  
00019 
00020 * Redistributions in binary form must reproduce the above copyright
00021 notice, this list of conditions and the following disclaimer in the
00022 documentation and/or other materials provided with the distribution.
00023 
00024 * Neither the name of Mayukh Bose nor the names of other
00025 contributors may be used to endorse or promote products derived from
00026 this software without specific prior written permission.
00027 
00028 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00029 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00030 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00031 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00032 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00033 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00034 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00035 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00036 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00037 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00038 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00039 */
00040 
00041 CSVParser::CSVParser()
00042 {
00043   m_sData = "";
00044   m_nPos = 0;
00045 }
00046 
00047 void CSVParser::SkipSpaces(void)
00048 {
00049   while (m_nPos < m_sData.length() && m_sData[m_nPos] == ' ')
00050     m_nPos++;
00051 }
00052 
00053 const CSVParser & CSVParser::operator <<(const string & sIn)
00054 {
00055   this->m_sData = sIn;
00056   this->m_nPos = 0;
00057   return *this;
00058 }
00059 
00060 const CSVParser & CSVParser::operator <<(const char *sIn)
00061 {
00062   this->m_sData = sIn;
00063   this->m_nPos = 0;
00064   return *this;
00065 }
00066 
00067 CSVParser & CSVParser::operator >>(int & nOut)
00068 {
00069   string sTmp = "";
00070   SkipSpaces();
00071   while (m_nPos < m_sData.length() && m_sData[m_nPos] != ',')
00072     sTmp += m_sData[m_nPos++];
00073 
00074   m_nPos++; // skip past comma
00075   nOut = atoi(sTmp.c_str());
00076   return *this;
00077 }
00078 
00079 CSVParser & CSVParser::operator >>(Natural & nOut)
00080 {
00081         return operator>>((int &)nOut);
00082 }
00083 
00084 CSVParser & CSVParser::operator >>(double & nOut)
00085 {
00086   string sTmp = "";
00087   SkipSpaces();
00088   while (m_nPos < m_sData.length() && m_sData[m_nPos] != ',')
00089     sTmp += m_sData[m_nPos++];
00090 
00091   m_nPos++; // skip past comma
00092   nOut = atof(sTmp.c_str());
00093   return *this;
00094 }
00095 
00097 CSVParser & CSVParser::operator >>(TypeOfRate & tOut) 
00098 {
00099   string sTmp = "";
00100   SkipSpaces();
00101   while (m_nPos < m_sData.length() && m_sData[m_nPos] != ',')
00102     sTmp += m_sData[m_nPos++];
00103 
00104   m_nPos++; // skip past comma
00105   tOut = (strcmp(sTmp.c_str(), yieldPoint::TypeAsString(Cash)) == 0 ? Cash : Swap);
00106 
00107   return *this;
00108 }
00109 
00111 CSVParser & CSVParser::operator >>(CreditSpreadType & tOut) 
00112 {
00113   string sTmp = "";
00114   SkipSpaces();
00115   while (m_nPos < m_sData.length() && m_sData[m_nPos] != ',')
00116     sTmp += m_sData[m_nPos++];
00117 
00118   m_nPos++; // skip past comma
00119   tOut = (strcmp(sTmp.c_str(), 
00120           CreditSpreadPoint::TypeAsString(Absolute)) == 0 ? 
00121           Absolute : Relative);
00122 
00123   return *this;
00124 }
00125 
00126 
00127 CSVParser & CSVParser::operator >>(string & sOut)
00128 {
00129   bool bQuotes = false;
00130   sOut = "";
00131   SkipSpaces();
00132 
00133   // Jump past first " if necessary
00134   if (m_nPos < m_sData.length() && m_sData[m_nPos] == '"') {
00135     bQuotes = true;
00136     m_nPos++; 
00137   }
00138   
00139   while (m_nPos < m_sData.length()) {
00140     if (!bQuotes && m_sData[m_nPos] == ',')
00141       break;
00142     if (bQuotes && m_sData[m_nPos] == '"') {
00143       if (m_nPos + 1 >= m_sData.length() - 1)
00144         break;
00145       if (m_sData[m_nPos+1] == ',')
00146         break;
00147     }
00148     sOut += m_sData[m_nPos++];
00149   }
00150 
00151   // Jump past last " if necessary
00152   if (bQuotes && m_nPos < m_sData.length() && m_sData[m_nPos] == '"')
00153     m_nPos++; 
00154 
00155   // Jump past , if necessary
00156   if (m_nPos < m_sData.length() && m_sData[m_nPos] == ',')
00157     m_nPos++; 
00158 
00159   return *this;
00160 }
00161 
00162 CSVParser & CSVParser::operator >> (Date &dOut) 
00163 {
00164   string sTmp = "";
00165   SkipSpaces();
00166   while (m_nPos < m_sData.length() && m_sData[m_nPos] != ',')
00167     sTmp += m_sData[m_nPos++];
00168 
00169   m_nPos++; // skip past comma
00170 
00171   StringTokenizer strtok(sTmp, "-");
00172   Day d = (Day)strtok.nextIntToken();
00173   Month m = (Month)strtok.nextIntToken();
00174   Year y = (Year)strtok.nextIntToken();
00175 
00176   dOut = Date(d, m, y);  
00177  
00178   return *this;
00179 }

Note: Generated nightly - reload for latest version
Generated on Thu Dec 22 23:12:36 2005 for terreneuve by doxygen 1.3.6