-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathhistTextLoad.cpp
More file actions
143 lines (129 loc) · 3.47 KB
/
histTextLoad.cpp
File metadata and controls
143 lines (129 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// This file parses a comma-delimited text file into a vector of time slices
//
// To do: 1. have the number of fields be variable/user selectable so we do not waste memory
#include <iostream>
#include <fstream>
#include "histTextLoad.h"
#define DEBUG false
#define DELIMITER '\t'
vector<timeSlice> histTextLoad (string fileName)
{
timeSlice currentBar;
vector<timeSlice> returnVector;
string line;
ifstream myfile;
myfile.open(fileName.c_str());
if (myfile.is_open())
{
while (myfile.good())//good is true until EOF hit
{
getline(myfile,line);
currentBar = parseLine(line);
if (currentBar.validBar == false)
{
#if DEBUG
cout<<"bad data line: "<<line<<endl;
#endif
}
else
{
//append this mktBar to the vector
returnVector.push_back(currentBar);
}
}
myfile.close();
#if DEBUG
cerr << "Finished parsing, sucessfullly parsed "<<returnVector.size()<<" time slices.\n";
#endif
return returnVector;
}
else cerr<<"Unable to open the file, man!";
return returnVector;
}
timeSlice parseLine(string inputLine)//should return something
{
timeSlice retBar;
size_t commaLoc = -1;
size_t prevCommaLoc = -1;
string parsedString;
parsedString = splitCSVline(inputLine, &commaLoc, &prevCommaLoc);
if (parsedString.empty())
{
retBar.validBar = false; //set time
return retBar;
}
else //parse minutes from '20110727 13:58:00' full time obj is overkill
{
long int i0, i1, i2, i3;
sscanf(parsedString.c_str(), "%ld %ld:%ld:%ld", &i0, &i1, &i2, &i3);
retBar.date = 10000*i0 + 60*i1 + i2; //date is YYYYMMDD + minutes since midnight ok to sort
}
#if DEBUG
cout << "Date: "<< retBar.date<<"\t";
#endif
parsedString = splitCSVline(inputLine, &commaLoc, &prevCommaLoc);
if (parsedString.empty())
{
retBar.validBar = false;
return retBar;
}
else retBar.open = atof(parsedString.c_str()); //set open
#if DEBUG
cout << "Open: "<<retBar.open <<"\t";
#endif
parsedString = splitCSVline(inputLine, &commaLoc, &prevCommaLoc);
if (parsedString.empty())
{
retBar.validBar = false;
return retBar;
}
else retBar.high = atof(parsedString.c_str()); //set high
#if DEBUG
cout << "High: "<<retBar.high <<"\t";
#endif
parsedString = splitCSVline(inputLine, &commaLoc, &prevCommaLoc);
if (parsedString.empty())
{
retBar.validBar = false;
return retBar;
}
else retBar.low = atof(parsedString.c_str()); //set low
#if DEBUG
cout << "Low: "<<retBar.low <<"\t";
#endif
parsedString = splitCSVline(inputLine, &commaLoc, &prevCommaLoc);
if (parsedString.empty())
{
retBar.validBar = false;
return retBar;
}
else retBar.close = atof(parsedString.c_str()); //set close
#if DEBUG
cout << "Close: "<<retBar.close <<"\t";
#endif
commaLoc = inputLine.find_first_of(',',commaLoc+1); //special case for parsing string
parsedString = inputLine.substr(prevCommaLoc+1);
if (parsedString.empty())
{
retBar.validBar = false;
return retBar;
}
else retBar.volume = atoi(parsedString.c_str()); //set adj close
#if DEBUG
cout << "Volume: " << retBar.volume <<endl;
#endif
retBar.validBar = true;//if we have gotten this far it's a valid bar
return retBar;
}
string splitCSVline(string inputLine, size_t *commaLoc, size_t *prevCommaLoc)
{
string retString;
*commaLoc = inputLine.find_first_of(DELIMITER,*commaLoc+1);
retString = inputLine.substr(*prevCommaLoc+1, *commaLoc-*prevCommaLoc-1);
*prevCommaLoc = *commaLoc;
return retString;
}
bool timeSliceSortPred(timeSlice t1, timeSlice t2)
{
return t1.date < t2.date;
}