文章分类

站点统计

  • 分类总数: 13 个
  • 文章总数: 145 篇
  • 评论总数: 47 条
  • 附件总数: 59 个
  • 建站日期: 2008-08-18
  • 访问总数: 472873 人次
  • RSS订阅: 文章|评论

两个CSV 解析类

Admin 于 2008-09-13 02:48:28 发表C/C++

订阅: http://www.kaiyuan8.org/Feed/Article_38.aspx
引用: 点这里获取地址 (UTF-8)
解决tsvncache.exe引起电脑慢的问题 < 两个CSV 解析类 > 使用boost::spirit实现的CSV文件解析类

其一:

  1. /* Copyright (C) 1999 Lucent Technologies */ 
  2. /* Excerpted from 'The Practice of Programming' */ 
  3. /* by Brian W. Kernighan and Rob Pike */ 
  4.  
  5. #include <iostream> 
  6. #include <algorithm> 
  7. #include <string> 
  8. #include <vector> 
  9.  
  10. using namespace std; 
  11.  
  12. class Csv 
  13. {    // read and parse comma-separated values 
  14.     // sample input: "LU",86.25,"11/4/1998","2:19PM",+4.0625 
  15.  
  16. public
  17.     Csv(istream& fin = cin, string sep = ",") :  
  18.       fin(fin), fieldsep(sep) {} 
  19.  
  20.       int getline(string&); 
  21.       string getfield(int n); 
  22.       int getnfield() const { return nfield; } 
  23.  
  24. private
  25.     istream& fin;            // input file pointer 
  26.     string line;            // input line 
  27.     vector<string> field;    // field strings 
  28.     int nfield;                // number of fields 
  29.     string fieldsep;        // separator characters 
  30.  
  31.     int split(); 
  32.     int endofline(char); 
  33.     int advplain(const string& line, string& fld, int); 
  34.     int advquoted(const string& line, string& fld, int); 
  35. }; 
  36.  
  37. // endofline: check for and consume \r, \n, \r\n, or EOF 
  38. int Csv::endofline(char c) 
  39.     int eol; 
  40.  
  41.     eol = (c=='\r' || c=='\n'); 
  42.     if (c == '\r'
  43.     { 
  44.         fin.get(c); 
  45.         if (!fin.eof() && c != '\n'
  46.             fin.putback(c);    // read too far 
  47.     } 
  48.     return eol; 
  49.  
  50. // getline: get one line, grow as needed 
  51. int Csv::getline(string& str) 
  52. {     
  53.     char c; 
  54.  
  55.     for (line = ""; fin.get(c) && !endofline(c); ) 
  56.         line += c; 
  57.     split(); 
  58.     str = line; 
  59.     return !fin.eof(); 
  60.  
  61. // split: split line into fields 
  62. int Csv::split() 
  63.     string fld; 
  64.     int i, j; 
  65.  
  66.     nfield = 0; 
  67.     if (line.length() == 0) 
  68.         return 0; 
  69.     i = 0; 
  70.  
  71.     do { 
  72.         if (i < line.length() && line[i] == '"'
  73.             j = advquoted(line, fld, ++i);    // skip quote 
  74.         else 
  75.             j = advplain(line, fld, i); 
  76.         if (nfield >= field.size()) 
  77.             field.push_back(fld); 
  78.         else 
  79.             field[nfield] = fld; 
  80.         nfield++; 
  81.         i = j + 1; 
  82.     } while (j < line.length()); 
  83.  
  84.     return nfield; 
  85.  
  86. // advquoted: quoted field; return index of next separator 
  87. int Csv::advquoted(const string& s, string& fld, int i) 
  88.     int j; 
  89.  
  90.     fld = ""
  91.     for (j = i; j < s.length(); j++) 
  92.     { 
  93.         if (s[j] == '"' && s[++j] != '"'
  94.         { 
  95.             int k = s.find_first_of(fieldsep, j); 
  96.             if (k > s.length())    // no separator found 
  97.                 k = s.length(); 
  98.             for (k -= j; k-- > 0; ) 
  99.                 fld += s[j++]; 
  100.             break
  101.         } 
  102.         fld += s[j]; 
  103.     } 
  104.     return j; 
  105.  
  106. // advplain: unquoted field; return index of next separator 
  107. int Csv::advplain(const string& s, string& fld, int i) 
  108.     int j; 
  109.  
  110.     j = s.find_first_of(fieldsep, i); // look for separator 
  111.     if (j > s.length())               // none found 
  112.         j = s.length(); 
  113.     fld = string(s, i, j-i); 
  114.     return j; 
  115.  
  116.  
  117. // getfield: return n-th field 
  118. string Csv::getfield(int n) 
  119.     if (n < 0 || n >= nfield) 
  120.         return ""
  121.     else 
  122.         return field[n]; 
  123.  
  124. // Csvtest main: test Csv class 
  125. int main(void
  126.     string line; 
  127.     Csv csv; 
  128.  
  129.     while (csv.getline(line) != 0) 
  130.     { 
  131.         cout << "line = `" << line <<"'\n"
  132.         for (int i = 0; i < csv.getnfield(); i++) 
  133.             cout << "field[" << i << "] = `" 
  134.             << csv.getfield(i) << "'\n"
  135.     } 
  136.     return 0; 

其二:
来源于:http://www.mayukhbose.com/freebies/c-code.php
头文件:

  1. #ifndef __CSVPARSE_H_2001_06_07__ 
  2. #define __CSVPARSE_H_2001_06_07__ 
  3.  
  4. /* 
  5. Copyright (c) 2001, Mayukh Bose 
  6. All rights reserved. 
  7.  
  8. Redistribution and use in source and binary forms, with or without 
  9. modification, are permitted provided that the following conditions are 
  10. met: 
  11.  
  12. * Redistributions of source code must retain the above copyright notice, 
  13. this list of conditions and the following disclaimer.   
  14.  
  15. * Redistributions in binary form must reproduce the above copyright 
  16. notice, this list of conditions and the following disclaimer in the 
  17. documentation and/or other materials provided with the distribution. 
  18.  
  19. * Neither the name of Mayukh Bose nor the names of other 
  20. contributors may be used to endorse or promote products derived from 
  21. this software without specific prior written permission. 
  22.  
  23. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
  24. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
  25. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
  26. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
  27. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
  28. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
  29. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
  30. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
  31. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
  32. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
  33. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
  34. */ 
  35.  
  36. #include <string> 
  37. using namespace std; 
  38.  
  39. class CSVParser { 
  40.  private
  41.   string m_sData; 
  42.   string::size_type m_nPos; 
  43.   void SkipSpaces(void); 
  44.  public
  45.   CSVParser(); 
  46.   const CSVParser & operator << (const string &sIn); 
  47.   const CSVParser & operator << (const char *sIn); 
  48.   CSVParser & operator >> (int &nOut); 
  49.   CSVParser & operator >> (double &nOut); 
  50.   CSVParser & operator >> (string &sOut); 
  51. }; 
  52.  
  53. #endif 

cpp:

  1. /* 
  2. Copyright (c) 2001, Mayukh Bose 
  3. All rights reserved. 
  4.  
  5. Redistribution and use in source and binary forms, with or without 
  6. modification, are permitted provided that the following conditions are 
  7. met: 
  8.  
  9. * Redistributions of source code must retain the above copyright notice, 
  10. this list of conditions and the following disclaimer.   
  11.  
  12. * Redistributions in binary form must reproduce the above copyright 
  13. notice, this list of conditions and the following disclaimer in the 
  14. documentation and/or other materials provided with the distribution. 
  15.  
  16. * Neither the name of Mayukh Bose nor the names of other 
  17. contributors may be used to endorse or promote products derived from 
  18. this software without specific prior written permission. 
  19.  
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
  31. */ 
  32.  
  33. #include <iostream> 
  34. #include <cstdlib> 
  35. #include "csvparser.h" 
  36. using namespace std; 
  37.  
  38.  
  39. CSVParser::CSVParser() 
  40.   m_sData = ""
  41.   m_nPos = 0; 
  42.  
  43. void CSVParser::SkipSpaces(void
  44.   while (m_nPos < m_sData.length() && m_sData[m_nPos] == ' '
  45.     m_nPos++; 
  46.  
  47. const CSVParser & CSVParser::operator <<(const string & sIn) 
  48.   this->m_sData = sIn; 
  49.   this->m_nPos = 0; 
  50.   return *this
  51.  
  52. const CSVParser & CSVParser::operator <<(const char *sIn) 
  53.   this->m_sData = sIn; 
  54.   this->m_nPos = 0; 
  55.   return *this
  56.  
  57. CSVParser & CSVParser::operator >>(int & nOut) 
  58.   string sTmp = ""
  59.   SkipSpaces(); 
  60.   while (m_nPos < m_sData.length() && m_sData[m_nPos] != ','
  61.     sTmp += m_sData[m_nPos++]; 
  62.  
  63.   m_nPos++; // skip past comma 
  64.   nOut = atoi(sTmp.c_str()); 
  65.   return *this
  66.  
  67. CSVParser & CSVParser::operator >>(double & nOut) 
  68.   string sTmp = ""
  69.   SkipSpaces(); 
  70.   while (m_nPos < m_sData.length() && m_sData[m_nPos] != ','
  71.     sTmp += m_sData[m_nPos++]; 
  72.  
  73.   m_nPos++; // skip past comma 
  74.   nOut = atof(sTmp.c_str()); 
  75.   return *this
  76.  
  77. CSVParser & CSVParser::operator >>(string & sOut) 
  78.   bool bQuotes = false
  79.   sOut = ""
  80.   SkipSpaces(); 
  81.  
  82.   // Jump past first " if necessary 
  83.   if (m_nPos < m_sData.length() && m_sData[m_nPos] == '"') { 
  84.     bQuotes = true
  85.     m_nPos++;  
  86.   } 
  87.    
  88.   while (m_nPos < m_sData.length()) { 
  89.     if (!bQuotes && m_sData[m_nPos] == ','
  90.       break
  91.     if (bQuotes && m_sData[m_nPos] == '"') { 
  92.       if (m_nPos + 1 >= m_sData.length() - 1) 
  93.         break
  94.       if (m_sData[m_nPos+1] == ','
  95.         break
  96.     } 
  97.     sOut += m_sData[m_nPos++]; 
  98.   } 
  99.  
  100.   // Jump past last " if necessary 
  101.   if (bQuotes && m_nPos < m_sData.length() && m_sData[m_nPos] == '"'
  102.     m_nPos++;  
  103.  
  104.   // Jump past , if necessary 
  105.   if (m_nPos < m_sData.length() && m_sData[m_nPos] == ','
  106.     m_nPos++;  
  107.  
  108.   return *this

 

被阅900次, 0投一票CSV
  • 看完了要说点啥么?
  • 昵称 (不填说不了话)
  • 信箱地址 (不会被公开,但是不填也说不了话)
  • 网址 (这个不填也成)
Powered by MiniBoke v2.0.0.8 Build 0828

Copyright © 2008 开源吧!. All rights reserved.

粤ICP备07500939号