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

filereader.cpp

Go to the documentation of this file.
00001 #include <fstream>
00002 #include "filereader.h"
00003 #include "csvparser.h"
00004 using namespace std;
00005 
00006 char FileReader::_datadir[128] = "";
00007 
00012 bool FileReader::fileexists(const char *filename) {
00013   bool flag = false;
00014   fstream fin;
00015 
00016   fin.open(filename, ios::in);
00017   flag = fin.is_open();
00018   fin.close();
00019 
00020   return flag;
00021 }
00022 
00027 bool FileReader::fileexists(string filename) {
00028         return FileReader::fileexists(filename.c_str());
00029 }
00030 
00031 bool FileReader::setdatadir(const char *command) {
00037         bool result = false;
00038         string mycommand(command);
00039         string filename("sample.csv");
00040         string dir("");
00041         string datadir("");
00042         string fullfilename("");
00043 
00044         /* possible values for command:
00045            program.exe -> datadir: ..\data
00046            somepath\program.exe -> datadir: somepath\..\data */
00047 
00048         string::size_type pos = mycommand.rfind("\\", mycommand.length());
00049 
00050         /* if \ found in command, extract the dir including the \, if not found
00051            we'll just continue with dir = "" */
00052         if (pos != string::npos) {
00053                 dir = mycommand.substr(0, pos + 1);
00054         }
00055         
00056         datadir = dir + "..\\data\\";
00057         fullfilename = datadir + filename;
00058         if (FileReader::fileexists(fullfilename.c_str())) {
00059                 strcpy(FileReader::_datadir, datadir.c_str());
00060                 result = true;
00061         } else {
00062                 datadir = dir + "data\\";
00063                 fullfilename = datadir + filename;
00064                 if (FileReader::fileexists(fullfilename.c_str())) {
00065                         strcpy(FileReader::_datadir, datadir.c_str());
00066                         result = true;
00067                 }
00068         }
00069 
00070         return result;
00071 }
00072 
00078 valarray<yieldPoint> *
00079 FileReader::buildYieldPointArray(string filename) {
00080   string sLine;
00081   double yieldRate, yieldMaturity;
00082   TypeOfRate yieldType;
00083   
00084   Natural ypArrayIdx = 0;
00085   Natural ypArrayMaxIdx = YC_MAX_NUMBER_POINTS - 1;
00086   yieldPoint *ypArray[YC_MAX_NUMBER_POINTS];
00087   valarray<yieldPoint> *ypValArray;
00088 
00089   CSVParser parser;
00090 
00091   if (!FileReader::fileexists(filename)) {
00092         cout << "could not find file " << filename << endl;
00093         return NULL;
00094   }
00095     
00096   ifstream infile(filename.c_str());
00097   while (!infile.eof() && (ypArrayIdx < ypArrayMaxIdx)) {
00098         if (ypArrayIdx >= ypArrayMaxIdx) {
00099                 cout << "File has more than max entries: " << (ypArrayMaxIdx + 1)
00100                          << " - truncating" << endl;
00101                 break;
00102         }
00103 
00104     getline(infile, sLine); // Get a line
00105     if (sLine == "")
00106       continue;
00107 
00108     parser << sLine; // Feed the line to the parser
00109 
00110     // Now extract the columns from the line
00111     parser >> yieldRate >> yieldMaturity >> yieldType;
00112 
00113         ypArray[ypArrayIdx++] = 
00114                 new yieldPoint(yieldRate, yieldMaturity, yieldType);
00115   }
00116   infile.close();
00117 
00118   ypValArray = new valarray<yieldPoint>(ypArrayIdx);
00119   for (Natural i = 0; i < ypArrayIdx; i++) {
00120           (*ypValArray)[i] = *(ypArray[i]);
00121   }
00122 
00123   return ypValArray;
00124 }
00125 
00131 valarray<CreditSpreadPoint> *
00132 FileReader::buildCreditSpreadPointArray(string filename) {
00133   string sLine;
00134   double spreadRate, spreadMaturity;
00135   CreditSpreadType spreadType;
00136   
00137   Natural ArrayIdx = 0;
00138   Natural ArrayMaxIdx = CC_MAX_NUM_SPREADS - 1;
00139   CreditSpreadPoint *theArray[CC_MAX_NUM_SPREADS];
00140   valarray<CreditSpreadPoint> *theValarray;
00141 
00142   CSVParser parser;
00143 
00144   if (!FileReader::fileexists(filename)) {
00145         cout << "could not find file " << filename << endl;
00146         return NULL;
00147   }
00148     
00149   ifstream infile(filename.c_str());
00150   while (!infile.eof() && (ArrayIdx < ArrayMaxIdx)) {
00151         if (ArrayIdx >= ArrayMaxIdx) {
00152                 cout << "File has more than max entries: " << (ArrayMaxIdx + 1)
00153                          << " - truncating" << endl;
00154                 break;
00155         }
00156 
00157     getline(infile, sLine); // Get a line
00158     if (sLine == "")
00159       continue;
00160 
00161     parser << sLine; // Feed the line to the parser
00162 
00163     // Now extract the columns from the line
00164     parser >> spreadRate >> spreadMaturity >> spreadType;
00165 
00166         theArray[ArrayIdx++] = 
00167                 new CreditSpreadPoint(spreadRate, spreadMaturity, spreadType);
00168   }
00169   infile.close();
00170 
00171   theValarray = new valarray<CreditSpreadPoint>(ArrayIdx);
00172   for (Natural i = 0; i < ArrayIdx; i++) {
00173           (*theValarray)[i] = *(theArray[i]);
00174   }
00175 
00176   return theValarray;
00177 }
00178 
00184 volsurfaceparams *
00185 FileReader::buildVolSurfaceParams(string filename) {
00186         volsurfaceparams *p = new volsurfaceparams();
00187 
00188         CSVParser parser;
00189         string sLine, dummy;
00190         Natural numStrikes, numMaturities;
00191 
00192         if (!FileReader::fileexists(filename)) {
00193                 cout << "could not find file " << filename << endl;
00194                 return NULL;
00195         }
00196 
00197         ifstream infile(filename.c_str());
00198 
00199         // file format is
00200         // maturities,<num of maturities>
00201         // strikes,<num of strikes>
00202         // maturities/strikes,<strike>,type,<strike>,type,...
00203         // <date>            ,<price>,<c|p>,<price>,<c|p>,...
00204 
00205         getline(infile, sLine); // Get a line
00206     parser << sLine; // Feed the line to the parser
00207     parser >> dummy >> numMaturities;
00208         // cout << "maturities = " << numMaturities << endl;
00209 
00210         getline(infile, sLine);
00211         parser << sLine;
00212         parser >> dummy >> numStrikes;
00213         // cout << "strikes = " << numStrikes << endl;
00214 
00215         p->strikes.resize(numStrikes);
00216         p->maturities.resize(numMaturities);
00217         p->callputprices.resize(numMaturities);
00218         p->iscallputprices.resize(numMaturities);
00219         for (Natural i = 0; i < numMaturities; i++) {
00220                 p->callputprices[i].resize(numStrikes);
00221                 p->iscallputprices[i].resize(numStrikes);
00222         }
00223                 
00224         getline(infile, sLine);
00225         parser << sLine;
00226         parser >> dummy; // skip past first column
00227         for (i = 0; i < numStrikes; i++) {
00228                 parser >> p->strikes[i] >> dummy; // every other column is "type"
00229                 // cout << "dummy: " << dummy << ", strike: " << p->strikes[i] << endl;
00230         }
00231 
00232         Natural maturityIdx = 0;
00233         string callorput;
00234 
00235         while (!infile.eof()) {
00236                 getline(infile, sLine);
00237                 if (sLine == "")
00238                         continue;
00239                 parser << sLine;
00240                 parser >> p->maturities[maturityIdx];
00241                 // cout << "maturity: " << p->maturities[maturityIdx].toString() << endl;
00242                 
00243                 for (i = 0; i < numStrikes; i++) {
00244                         parser >> p->callputprices[maturityIdx][i];
00245                         parser >> callorput;
00246                         // cout << "price: " << p->callputprices[maturityIdx][i] << ", " << callorput << endl;
00247                         p->iscallputprices[maturityIdx][i] = 
00248                                 (callorput.compare("c") == 0);
00249                 }
00250                 maturityIdx++;
00251         }
00252 
00253         return p;
00254 }

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