• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            牽著老婆滿街逛

            嚴(yán)以律己,寬以待人. 三思而后行.
            GMail/GTalk: yanglinbo#google.com;
            MSN/Email: tx7do#yahoo.com.cn;
            QQ: 3 0 3 3 9 6 9 2 0 .

            兩個 CSV 解析類

            其一:
            /* Copyright (C) 1999 Lucent Technologies */
            /* Excerpted from 'The Practice of Programming' */
            /* by Brian W. Kernighan and Rob Pike */

            #include 
            <iostream>
            #include 
            <algorithm>
            #include 
            <string>
            #include 
            <vector>

            using namespace std;

            class Csv
            {    // read and parse comma-separated values
                
            // sample input: "LU",86.25,"11/4/1998","2:19PM",+4.0625

            public:
                Csv(istream
            & fin = cin, string sep = ",") : 
                  fin(fin), fieldsep(sep) 
            {}

                  
            int getline(string&);
                  
            string getfield(int n);
                  
            int getnfield() const return nfield; }

            private:
                istream
            & fin;            // input file pointer
                string line;            // input line
                vector<string> field;    // field strings
                int nfield;                // number of fields
                string fieldsep;        // separator characters

                
            int split();
                
            int endofline(char);
                
            int advplain(const string& line, string& fld, int);
                
            int advquoted(const string& line, string& fld, int);
            }
            ;

            // endofline: check for and consume \r, \n, \r\n, or EOF
            int Csv::endofline(char c)
            {
                
            int eol;

                eol 
            = (c=='\r' || c=='\n');
                
            if (c == '\r')
                
            {
                    fin.
            get(c);
                    
            if (!fin.eof() && c != '\n')
                        fin.putback(c);    
            // read too far
                }

                
            return eol;
            }


            // getline: get one line, grow as needed
            int Csv::getline(string& str)
            {    
                
            char c;

                
            for (line = ""; fin.get(c) && !endofline(c); )
                    line 
            += c;
                split();
                str 
            = line;
                
            return !fin.eof();
            }


            // split: split line into fields
            int Csv::split()
            {
                
            string fld;
                
            int i, j;

                nfield 
            = 0;
                
            if (line.length() == 0)
                    
            return 0;
                i 
            = 0;

                
            do {
                    
            if (i < line.length() && line[i] == '"')
                        j 
            = advquoted(line, fld, ++i);    // skip quote
                    else
                        j 
            = advplain(line, fld, i);
                    
            if (nfield >= field.size())
                        field.push_back(fld);
                    
            else
                        field[nfield] 
            = fld;
                    nfield
            ++;
                    i 
            = j + 1;
                }
             while (j < line.length());

                
            return nfield;
            }


            // advquoted: quoted field; return index of next separator
            int Csv::advquoted(const string& s, string& fld, int i)
            {
                
            int j;

                fld 
            = "";
                
            for (j = i; j < s.length(); j++)
                
            {
                    
            if (s[j] == '"' && s[++j] != '"')
                    
            {
                        
            int k = s.find_first_of(fieldsep, j);
                        
            if (k > s.length())    // no separator found
                            k = s.length();
                        
            for (k -= j; k-- > 0; )
                            fld 
            += s[j++];
                        
            break;
                    }

                    fld 
            += s[j];
                }

                
            return j;
            }


            // advplain: unquoted field; return index of next separator
            int Csv::advplain(const string& s, string& fld, int i)
            {
                
            int j;

                j 
            = s.find_first_of(fieldsep, i); // look for separator
                if (j > s.length())               // none found
                    j = s.length();
                fld 
            = string(s, i, j-i);
                
            return j;
            }



            // getfield: return n-th field
            string Csv::getfield(int n)
            {
                
            if (n < 0 || n >= nfield)
                    
            return "";
                
            else
                    
            return field[n];
            }


            // Csvtest main: test Csv class
            int main(void)
            {
                
            string line;
                Csv csv;

                
            while (csv.getline(line) != 0)
                
            {
                    cout 
            << "line = `" << line <<"'\n";
                    
            for (int i = 0; i < csv.getnfield(); i++)
                        cout 
            << "field[" << i << "] = `"
                        
            << csv.getfield(i) << "'\n";
                }

                
            return 0;
            }




            其二:
            來源于:http://www.mayukhbose.com/freebies/c-code.php
            頭文件:
            #ifndef __CSVPARSE_H_2001_06_07__
            #define __CSVPARSE_H_2001_06_07__

            /*
            Copyright (c) 2001, Mayukh Bose
            All rights reserved.

            Redistribution and use in source and binary forms, with or without
            modification, are permitted provided that the following conditions are
            met:

            * Redistributions of source code must retain the above copyright notice,
            this list of conditions and the following disclaimer.  

            * Redistributions in binary form must reproduce the above copyright
            notice, this list of conditions and the following disclaimer in the
            documentation and/or other materials provided with the distribution.

            * Neither the name of Mayukh Bose nor the names of other
            contributors may be used to endorse or promote products derived from
            this software without specific prior written permission.

            THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
            "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
            LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
            A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
            OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
            SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
            LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
            DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
            THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
            (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
            OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
            */


            #include 
            <string>
            using namespace std;

            class CSVParser {
             
            private:
              
            string m_sData;
              
            string::size_type m_nPos;
              
            void SkipSpaces(void);
             
            public:
              CSVParser();
              
            const CSVParser & operator << (const string &sIn);
              
            const CSVParser & operator << (const char *sIn);
              CSVParser 
            & operator >> (int &nOut);
              CSVParser 
            & operator >> (double &nOut);
              CSVParser 
            & operator >> (string &sOut);
            }
            ;

            #endif

            cpp
            /*
            Copyright (c) 2001, Mayukh Bose
            All rights reserved.

            Redistribution and use in source and binary forms, with or without
            modification, are permitted provided that the following conditions are
            met:

            * Redistributions of source code must retain the above copyright notice,
            this list of conditions and the following disclaimer.  

            * Redistributions in binary form must reproduce the above copyright
            notice, this list of conditions and the following disclaimer in the
            documentation and/or other materials provided with the distribution.

            * Neither the name of Mayukh Bose nor the names of other
            contributors may be used to endorse or promote products derived from
            this software without specific prior written permission.

            THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
            "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
            LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
            A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
            OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
            SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
            LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
            DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
            THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
            (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
            OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
            */


            #include 
            <iostream>
            #include 
            <cstdlib>
            #include 
            "csvparser.h"
            using namespace std;


            CSVParser::CSVParser()
            {
              m_sData 
            = "";
              m_nPos 
            = 0;
            }


            void CSVParser::SkipSpaces(void)
            {
              
            while (m_nPos < m_sData.length() && m_sData[m_nPos] == ' ')
                m_nPos
            ++;
            }


            const CSVParser & CSVParser::operator <<(const string & sIn)
            {
              
            this->m_sData = sIn;
              
            this->m_nPos = 0;
              
            return *this;
            }


            const CSVParser & CSVParser::operator <<(const char *sIn)
            {
              
            this->m_sData = sIn;
              
            this->m_nPos = 0;
              
            return *this;
            }


            CSVParser 
            & CSVParser::operator >>(int & nOut)
            {
              
            string sTmp = "";
              SkipSpaces();
              
            while (m_nPos < m_sData.length() && m_sData[m_nPos] != ',')
                sTmp 
            += m_sData[m_nPos++];

              m_nPos
            ++// skip past comma
              nOut = atoi(sTmp.c_str());
              
            return *this;
            }


            CSVParser 
            & CSVParser::operator >>(double & nOut)
            {
              
            string sTmp = "";
              SkipSpaces();
              
            while (m_nPos < m_sData.length() && m_sData[m_nPos] != ',')
                sTmp 
            += m_sData[m_nPos++];

              m_nPos
            ++// skip past comma
              nOut = atof(sTmp.c_str());
              
            return *this;
            }


            CSVParser 
            & CSVParser::operator >>(string & sOut)
            {
              
            bool bQuotes = false;
              sOut 
            = "";
              SkipSpaces();

              
            // Jump past first " if necessary
              if (m_nPos < m_sData.length() && m_sData[m_nPos] == '"'{
                bQuotes 
            = true;
                m_nPos
            ++
              }

              
              
            while (m_nPos < m_sData.length()) {
                
            if (!bQuotes && m_sData[m_nPos] == ',')
                  
            break;
                
            if (bQuotes && m_sData[m_nPos] == '"'{
                  
            if (m_nPos + 1 >= m_sData.length() - 1)
                    
            break;
                  
            if (m_sData[m_nPos+1== ',')
                    
            break;
                }

                sOut 
            += m_sData[m_nPos++];
              }


              
            // Jump past last " if necessary
              if (bQuotes && m_nPos < m_sData.length() && m_sData[m_nPos] == '"')
                m_nPos
            ++

              
            // Jump past , if necessary
              if (m_nPos < m_sData.length() && m_sData[m_nPos] == ',')
                m_nPos
            ++

              
            return *this;
            }



            posted on 2008-06-03 11:26 楊粼波 閱讀(2319) 評論(1)  編輯 收藏 引用

            評論

            # re: 兩個 CSV 解析類 2008-10-16 09:49 傲天

            這片文章不錯,我喜歡,謝謝博主  回復(fù)  更多評論   


            只有注冊用戶登錄后才能發(fā)表評論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            91久久精品电影| A级毛片无码久久精品免费| 亚洲va久久久噜噜噜久久| 久久午夜伦鲁片免费无码| 国产婷婷成人久久Av免费高清| 精品久久人妻av中文字幕| 青青青青久久精品国产| 国产精品久久久久久五月尺| 久久精品国产色蜜蜜麻豆| 久久国产精品久久| 久久天天躁狠狠躁夜夜avapp| 人妻无码中文久久久久专区| 久久se这里只有精品| 日日噜噜夜夜狠狠久久丁香五月| 99热热久久这里只有精品68| 国产A级毛片久久久精品毛片| 亚洲国产天堂久久综合网站| 99精品久久精品一区二区| 国产亚洲精午夜久久久久久| 久久精品中文騷妇女内射| 无码任你躁久久久久久老妇| 久久精品嫩草影院| 影音先锋女人AV鲁色资源网久久| 国内精品久久久久久久久电影网| 色婷婷综合久久久久中文| 久久久久久久综合狠狠综合| 久久国产精品视频| 99国内精品久久久久久久| 国产精品久久久亚洲| 久久精品国产99国产精品亚洲| 久久97久久97精品免视看秋霞| 99久久精品国内| 久久久久亚洲av无码专区导航| 久久人人爽人人爽人人片AV不| 亚洲人成无码久久电影网站| 蜜桃麻豆www久久国产精品| 精品久久久无码中文字幕天天| 国产精品永久久久久久久久久| 好久久免费视频高清| 国产精品99久久久久久人| 久久99精品久久久久久久不卡|