00001
00033
00034 #include <errno.h>
00035 #include <stdio.h>
00036 #include <string.h>
00037
00038 #include "yahooTS.h"
00039
00040
00056 const char *yahooTS::getStr_( char *&line,
00057 char *buf,
00058 size_t bufSize ) const
00059 {
00060 const char *rtnPtr = 0;
00061 if (line != 0) {
00062 for (size_t charCnt = 0; charCnt < bufSize-1 && *line != '\0'; charCnt++) {
00063 if (*line == ',') {
00064 line++;
00065 break;
00066 }
00067 else {
00068 buf[charCnt] = *line++;
00069 }
00070 }
00071
00072 buf[charCnt] = '\0';
00073 if (charCnt > 0)
00074 {
00075 rtnPtr = buf;
00076 }
00077 }
00078 return rtnPtr;
00079 }
00080
00081
00082
00101 void yahooTS::parseVals_( char *line,
00102 double *vals,
00103 const size_t n ) const
00104 {
00105 char buf[128];
00106 const char *ptr;
00107
00108
00109 ptr = getStr_( line, buf, sizeof( buf ) );
00110 if (ptr == 0) {
00111 fprintf(stderr, "parseVals: date expected\n" );
00112 return;
00113 }
00114
00115
00116 size_t cnt = 0;
00117 for (dataKind kind = Open;
00118 kind <= Volume && cnt < n;
00119 kind = (dataKind)((size_t)kind + 1)) {
00120
00121 ptr = getStr_( line, buf, sizeof( buf ) );
00122 if (ptr == 0) {
00123 fprintf(stderr, "parseVals: value expected\n");
00124 return;
00125 }
00126
00127 double v;
00128
00129 sscanf( buf, "%lf", &v );
00130 vals[cnt] = v;
00131 cnt++;
00132 }
00133
00134 }
00135
00136
00151 const double yahooTS::getValue_( char *line,
00152 const yahooTS::dataKind kind ) const
00153 {
00154 double retval = 0;
00155
00156 if (kind > badEnum && kind < lastEnum) {
00157 const size_t NUM_VALS = 5;
00158 double vals[ NUM_VALS ];
00159
00160 parseVals_( line, vals, NUM_VALS );
00161
00162 size_t ix = (size_t)kind - 1;
00163 if (ix < NUM_VALS) {
00164 retval = vals[ix];
00165 }
00166 }
00167
00168 return retval;
00169 }
00170
00171
00208 const double *yahooTS::getTS( const char *fileName,
00209 double *a,
00210 size_t &N,
00211 const yahooTS::dataKind kind ) const
00212 {
00213 const double *rtnPtr = 0;
00214 char fullPath[512];
00215 size_t freePath = sizeof( fullPath );
00216 FILE *fptr;
00217
00218 if (path_ != 0) {
00219 strncpy( fullPath, path_, freePath-1 );
00220 freePath = freePath - strlen( fullPath );
00221 }
00222 strncat( fullPath, fileName, freePath-1 );
00223 fptr = fopen( fullPath, "r" );
00224 if (fptr != 0) {
00225 char line[512];
00226 size_t lineSize = sizeof( line );
00227 int ix = N-1;
00228
00229 if (fgets( line, lineSize, fptr ) != 0) {
00230 rtnPtr = a;
00231 while (fgets( line, lineSize, fptr ) != 0) {
00232 if (ix >= 0) {
00233 a[ix] = getValue_( line, kind );
00234 ix--;
00235 }
00236 else {
00237 break;
00238 }
00239 }
00240 }
00241 else {
00242 fprintf(stderr, "getTS: title line expected\n");
00243 }
00244 ix++;
00245 N = N - ix;
00246 }
00247 else {
00248 const char *error = strerror( errno );
00249 fprintf(stderr, "getTS: Error opening %s: %s\n", fullPath, error );
00250 }
00251
00252 return rtnPtr;
00253 }