OSDN Git Service

first
[psychlops/cpp.git] / psychlops / core / data / psychlops_d_misc.cpp
1 /*
2  *  psychlops_io_misc.h
3  *  Psychlops Standard Library (MacOSX)
4  *
5  *  Last Modified 2005/10/18 by Kenchi HOSOKAWA
6  *  (C) 2005 Kenchi HOSOKAWA, Kazushi MARUYA, Takao SATO
7  */
8
9
10 #include "psychlops_d_misc.h"
11
12
13
14 namespace Psychlops {
15
16
17         std::map<const char*, double> Data::number;
18
19
20         Data::DUMMY_ *Data::_tmp;
21         Data_DUMMY2_ *Data::dummy2_;
22         void operator >>(std::istream &a, Data_DUMMY2_ &b){}
23 //      const int Data::prohibitedcnt = 11;
24         char * Data::prohibited[prohibitedcnt] = {"\\","/",";",",",":","*","?","\"","<",">","|"};
25         char * Data::delimiter = "\t";
26         std::ofstream Data::output;
27
28
29         int Data::loadCSVraw(const std::string& filename, std::vector<std::vector<std::string> >& lines)
30         {
31                 //      filename
32                 std::string filenamechacker = File::decodePath(filename);
33                 std::ifstream fin(filenamechacker.c_str());
34
35                 std::string tmp, source("");
36                 while( std::getline(fin,tmp) ) {
37                         source += tmp;
38                         source += '\n';
39                 }
40                 fin.close();
41
42                 return parseCSV(source, lines);
43         }
44
45         // code from  http://stackoverflow.com/questions/1120140/csv-parser-in-c
46         int Data::parseCSV(const std::string& csvSource, std::vector<std::vector<std::string> >& lines)
47     {
48                 int result = 0;
49
50                 bool inQuote = false;
51                 bool lastCharWasAQuote = false;
52                 bool newLine = false;
53                 std::string field;
54                 lines.clear();
55                 std::vector<std::string> line;
56
57                 std::string::const_iterator aChar = csvSource.begin();
58                 while (aChar != csvSource.end())
59                 {
60                         switch (*aChar)
61                         {
62                                 case '"':
63                                         newLine = false;
64                                         if (lastCharWasAQuote == true)
65                                         {
66                                                 lastCharWasAQuote = false;
67                                                 field += *aChar;
68                                         }
69                                         else
70                                         {
71                                                 inQuote = !inQuote;
72                                         }
73                                         break;
74
75                                 case ',':
76                                         newLine = false;
77                                         if (inQuote == true)
78                                         {
79                                                 field += *aChar;
80                                         }
81                                         else
82                                         {
83                                                 line.push_back(field);
84                                                 field.clear();
85                                         }
86                                         break;
87
88                                 case '\n':
89                                 case '\r':
90                                         if (inQuote == true)
91                                         {
92                                                 field += *aChar;
93                                         }
94                                         else
95                                         {
96                                                 if (newLine == false)
97                                                 {
98                                                         line.push_back(field);
99                                                         lines.push_back(line);
100                                                         field.clear();
101                                                         line.clear();
102                                                         newLine = true;
103                                                 }
104                                         }
105                                         break;
106
107                                 default:
108                                         newLine = false;
109                                         field.push_back(*aChar);
110                                         break;
111                         }
112
113                         aChar++;
114                 }
115
116                 if (line.size())
117                 {
118                         if (field.size())
119                                 line.push_back(field);
120
121           lines.push_back(line);
122        }
123
124        return result;
125     }
126
127
128 }       /*      <- namespace Psycholops         */
129