-: 0:Source:CSV_line.cpp -: 0:Graph:CSV_line.gcno -: 0:Data:CSV_line.gcda -: 0:Runs:1 -: 0:Programs:1 -: 1:// CSV_line.cpp (C) 2008 adolfo@di-mare.com -: 2: -: 3:#ifdef English_dox -: 4:/** \file CSV_line.cpp -: 5: \brief Implementation for \c CSV_line.h. -: 6: \author Adolfo Di Mare -: 7: \date 2008 -: 8:*/ -: 9:#endif -: 10: -: 11:#ifdef Spanish_dox -: 12:/** \file CSV_line.cpp -: 13: \brief Implementación para \c CSV_line.h. -: 14: \author Adolfo Di Mare -: 15: \date 2008 -: 16:*/ -: 17:#endif -: 18: -: 19:#include "CSV_line.h" -: 20: -: 21:#include // basic_istringstream<> -: 22: function _ZN8CSV_line7setDataERKSs called 28 returned 100% blocks executed 71% 28: 23:void CSV_line::setData( const std::string & str ) { 28: 24: std::vector& VEC = (*this); -: 25: // see http://www.horstmann.com/cpp/pitfalls.html -: 26:{{ // test::getNextCSV() 28: 27: VEC.clear(); // std::vector VEC; 28: 28: std::string csv; 28: 29: bool eol_CIN = false; // stop when the end of line is reached 28: 30: std::istringstream ist( str , std::ios::binary ); 119: 31: while ( ! eol_CIN && ! ist.fail() ) { // ! ist.eof() pitfall! 91: 32: eol_CIN = getNextCSV( csv, ist ); 91: 33: VEC.push_back( csv ); -: 34: } 28: 35: return; -: 36: // Using std::ios::binary ensures that no CR+LF chars are discarded -: 37:}} -: 38:} -: 39: function _ZN8CSV_line7setDataEPKcj called 28 returned 100% blocks executed 77% 28: 40:void CSV_line::setData( const char *str , size_t n ) { -: 41: #if 1 28: 42: n = ( n>0 ? n : strlen( str ) ); 28: 43: std::string st; st.assign( str, n ); 28: 44: setData( st ); 28: 45: return; -: 46: #else -: 47: realSetData( m_DATA, str, n ); -: 48: return size(); -: 49: #endif -: 50: -: 51: #if 0 -: 52: // Implementación errónea y vieja (no maneja DQUOTE) -: 53: clear(); // borra el vector de subhileras -: 54: std::string valor; // valor de cada dato extraído de "str" -: 55: std::string::size_type len = str.size(); -: 56: std::string::size_type last, i = 0; // primer valor de "str" -: 57: int n = 0, pos = 0; // posición primera -: 58: while ( i n=0; i=0; DATA[0] = ""; -: 86:// [ ++n h="" ++i ] ==> ++n; DATA[n] = ""; ++i; -: 87:// [ ++i ] ==> ++i; -: 88:// [ h+= ++i ] ==> DATA[n] += str[i]; ++i; -: 89:// [ h+='""' ++i ] ==> DATA[n] = '"' + DATA[n] + '"' + str[i]; ++i; -: 90:// [ END ] ==> return n; -: 91:// -: 92:// | ',' '\n' | " | l | -: 93:// delta() | comma+LF | d-quote | letter | -: 94:// ----------+------------+------------+------------+ -: 95:// ==> 0 | 0 | 1 | 3 | -: 96:// init |++n h="" ++i| ++i | h+= ++i | -: 97:// ----------+------------+------------+------------+ -: 98:// 1 | 1 | 2 | 1 | -: 99:// quoted(1)| h+= ++i | ++i | h+= ++i | -: 100:// ----------+------------+------------+------------+ -: 101:// 2 | 0 | 1 | 3 | -: 102:// inquote(2)|++n h="" ++i| h+= ++i | h+='""' ++i| -: 103:// ----------+------------+------------+------------+ -: 104:// 3 | 0 | 3 | 3 | -: 105:// regular|++n h="" ++i| h+= ++i | h+= ++i | -: 106:// ----------+------------+------------+------------+ -: 107: -: 108:#if 0 // DEPRECATED -: 109: -: 110:/// Función que carga el vector \c DATA[] con los valores CSV que contiene la hilera \c str. -: 111:void realSetData( std::vector & DATA, const char *str , size_t n ) { -: 112: size_t N = ( n>0 ? n : strlen( str ) ); -: 113: size_t i=0; int state=0; n = 0; -: 114: DATA.clear(); DATA.push_back(""); -: 115: // N: cantidad de letras a procesar -: 116: // n: cantidad de columnas extraídas de str[] -: 117: // i: caracter en proceso. -: 118: while( i 0 | 0 | 1 | 3 | -: 139: else { // letter // init |++n h="" ++i| ++i | h+= ++i | -: 140: DATA[n] += str[i]; ++i; // ----------+------------+------------+------------+ -: 141: state = 3; -: 142: } -: 143: } -: 144: break; -: 145: -: 146: case 1: { // quote(1) -: 147: // if ( str[i] == COMMA ) { -: 148: // DATA[n] += str[i]; ++i; -: 149: // // state = 1; -: 150: // } -: 151: /* else */ if ( str[i] == DQUOTE ) {// | ',' '\n' | " | l | -: 152: ++i; // delta() | comma+LF | d-quote | letter | -: 153: state = 2; // ----------+------------+------------+------------+ -: 154: } // 1 | 1 | 2 | 1 | -: 155: // else if ( str[i] == LF ) { // quoted(1)| h+= ++i | ++i | h+= ++i | -: 156: // DATA[n] += str[i]; ++i; // ----------+------------+------------+------------+ -: 157: // // state = 1; -: 158: // } -: 159: else { // letter -: 160: DATA[n] += str[i]; ++i; -: 161: // state = 1; -: 162: } -: 163: } -: 164: break; -: 165: -: 166: case 2: { // inquote(2) -: 167: if ( str[i] == COMMA ) { -: 168: ++n; DATA.push_back(""); ++i; -: 169: state = 0; -: 170: } -: 171: else if ( str[i] == DQUOTE ) { // | ',' '\n' | " | l | -: 172: DATA[n] += str[i]; ++i; // delta() | comma+LF | d-quote | letter | -: 173: state = 1; // ----------+------------+------------+------------+ -: 174: } // 2 | 0 | 1 | 3 | -: 175: // else if ( str[i] == LF ) { // inquote(2)|++n h="" ++i| h+= ++i | h+='""' ++i| -: 176: // // chop( DATA[n], CR ); // ----------+------------+------------+------------+ -: 177: // rebuildDquote( DATA[n] ); -: 178: // DATA[n] = DQUOTE + DATA[n] + DQUOTE + str[i]; ++i; -: 179: // state = 3; -: 180: // } -: 181: else if ( str[i] == CR ) { // handle CR+LF at the end of line -: 182: bool LF_is_last = false; -: 183: if ( i == N-2 ) { -: 184: if ( str[i+1] == LF ) { -: 185: LF_is_last = true; // take out CR+LF -: 186: } -: 187: } -: 188: if ( ! LF_is_last ) { -: 189: rebuildDquote( DATA[n] ); -: 190: DATA[n] = DQUOTE + DATA[n] + str[i]; ++i; -: 191: state = 3; -: 192: } -: 193: } -: 194: else { // letter -: 195: rebuildDquote( DATA[n] ); -: 196: DATA[n] = DQUOTE + DATA[n] + DQUOTE + str[i]; ++i; // assertTrue( """" == "" ); -: 197: state = 3; -: 198: } -: 199: } -: 200: break; -: 201: -: 202: case 3: { // regular -: 203: if ( str[i] == COMMA ) { -: 204: ++n; DATA.push_back(""); ++i; -: 205: state = 0; -: 206: } -: 207: // else if ( str[i] == DQUOTE ) { // | ',' '\n' | " | l | -: 208: // DATA[n] += str[i]; ++i; // delta() | comma+LF | d-quote | letter | -: 209: // // state = 3; // ----------+------------+------------+------------+ -: 210: // } // 3 | 0 | 3 | 3 | -: 211: else if ( str[i] == LF ) { // regular |++n h="" ++i| h+= ++i | h+= ++i | -: 212: if ( i+1 == N ) { // ----------+------------+------------+------------+ -: 213: chop( DATA[n], CR ); -: 214: i = N; // ++i; state = 0; // handle CR+LF at the end of line -: 215: } -: 216: else { -: 217: ++n; DATA.push_back(""); ++i; -: 218: state = 0; -: 219: } -: 220: } -: 221: else { // letter -: 222: DATA[n] += str[i]; ++i; -: 223: // state = 3; -: 224: } -: 225: } -: 226: break; -: 227: } -: 228: } -: 229: return; -: 230:} -: 231: -: 232:#endif -: 233: -: 234:// EOF: CSV_line.cpp