00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #ifndef __FIMCP_COMMON_READERSSV__
00036 #define __FIMCP_COMMON_READERSSV__
00037
00038 #include <stdio.h>
00039 #include <stdlib.h>
00040 #include <iostream>
00041 #include <vector>
00042 #include <string>
00043
00044 using namespace std;
00045
00046
00047
00048
00049 template <class Type>
00050 class Reader_SSV {
00051 public:
00052 vector <vector<Type> > data;
00053
00054 Reader_SSV () { }
00055 ~Reader_SSV () { }
00056 virtual void read ( string filename );
00057
00058 inline bool is_space ( char pos ) {
00059 return pos == ' ' || pos == '\n' || pos == '\t' || pos == ',' || pos == '\r';
00060 }
00061
00062
00063 inline int parse_int(char &c, FILE *in);
00064 };
00065
00066 template <class Type>
00067 void Reader_SSV<Type>::read ( string filename ) {
00068 FILE *in = fopen(filename.c_str(),"rt");
00069 if (!in) {
00070 std::cerr << "\tError: file "<<filename<<" does not exist !"<<std::endl;
00071 exit(1);
00072 }
00073
00074 do {
00075 char c = getc(in);
00076 if (c != '@' && c != '%' && c != ' ' && c != '\n' && !feof(in)) {
00077
00078 vector<Type> temp;
00079 do {
00080 Type x = this->parse_int(c, in);
00081 temp.push_back(x);
00082 } while (c != '\n');
00083 data.push_back(temp);
00084 } else {
00085
00086 while (c != '\n' && !feof(in))
00087 c = getc(in);
00088 }
00089 } while (!feof(in));
00090 fclose(in);
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101 }
00102
00103 template <class Type>
00104 int Reader_SSV<Type>::parse_int(char &c, FILE *in) {
00105 int val = 0;
00106 int cval;
00107 do {
00108 cval = int(c)-int('0');
00109 if (cval < 0 || cval > 9) {
00110 fprintf(stderr, "\tError: unexpected input, non-digit '%c'=%i found !\n", c, cval);
00111 exit(1);
00112 }
00113 val *= 10;
00114 val += cval;
00115 c = getc(in);
00116 } while (!is_space(c));
00117
00118 while (c != '\n' && is_space(c))
00119 c = getc(in);
00120 return val;
00121 }
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131 #endif