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
00045
00046
00047
00048 string::size_type pos = mycommand.rfind("\\", mycommand.length());
00049
00050
00051
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);
00105 if (sLine == "")
00106 continue;
00107
00108 parser << sLine;
00109
00110
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);
00158 if (sLine == "")
00159 continue;
00160
00161 parser << sLine;
00162
00163
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
00200
00201
00202
00203
00204
00205 getline(infile, sLine);
00206 parser << sLine;
00207 parser >> dummy >> numMaturities;
00208
00209
00210 getline(infile, sLine);
00211 parser << sLine;
00212 parser >> dummy >> numStrikes;
00213
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;
00227 for (i = 0; i < numStrikes; i++) {
00228 parser >> p->strikes[i] >> dummy;
00229
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
00242
00243 for (i = 0; i < numStrikes; i++) {
00244 parser >> p->callputprices[maturityIdx][i];
00245 parser >> callorput;
00246
00247 p->iscallputprices[maturityIdx][i] =
00248 (callorput.compare("c") == 0);
00249 }
00250 maturityIdx++;
00251 }
00252
00253 return p;
00254 }