root/trunk/libffado/src/genericavc/avc_vendormodel.cpp

Revision 558, 3.0 kB (checked in by wagi, 16 years ago)

tokinize input (WIP)

Line 
1 /*
2  * Copyright (C) 2007 by Daniel Wagner
3  *
4  * This file is part of FFADO
5  * FFADO = Free Firewire (pro-)audio drivers for linux
6  *
7  * FFADO is based upon FreeBoB.
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License version 2.1, as published by the Free Software Foundation;
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
21  * MA 02110-1301 USA
22  */
23
24 #include "genericavc/avc_vendormodel.h"
25
26 #include <fstream>
27 #include <istream>
28 #include <iostream>
29 #include <iterator>
30
31 using namespace std;
32
33 static void
34 tokenize(const string& str,
35          vector<string>& tokens,
36          const string& delimiters = " ")
37 {
38     // Skip delimiters at beginning.
39     string::size_type lastPos = str.find_first_not_of(delimiters, 0);
40     // Find first "non-delimiter".
41     string::size_type pos     = str.find_first_of(delimiters, lastPos);
42
43     while (string::npos != pos || string::npos != lastPos)
44     {
45         // Found a token, add it to the vector.
46         tokens.push_back(str.substr(lastPos, pos - lastPos));
47         // Skip delimiters.  Note the "not_of"
48         lastPos = str.find_first_not_of(delimiters, pos);
49         // Find next "non-delimiter"
50         pos = str.find_first_of(delimiters, lastPos);
51     }
52 }
53
54 GenericAVC::VendorModel::VendorModel( const char* filename )
55 {
56     ifstream in ( filename );
57
58     if ( !in ) {
59         perror( filename );
60         return;
61     }
62
63     cout << "vendorId\t\tmodelId\t\tvendorName\t\tmodelName" << endl;
64     string line;
65     while ( !getline( in,  line ).eof() ) {
66         string::size_type i = line.find_first_not_of( " \t\n\v" );
67         if ( i != string::npos && line[i] == '#' )
68             continue;
69
70         vector<string> tokens;
71         tokenize( line, tokens, "," );
72
73         for ( vector<string>::iterator it = tokens.begin();
74               it != tokens.end();
75               ++it )
76         {
77             string vendorId = *it++;
78             string modelId = *it++;
79             string vendorName = *it++;
80             string modelName= *it;
81             cout << vendorId << "\t" << modelId << "\t" <<vendorName << "\t" << modelName << endl;
82         }
83     }
84
85     if ( !in.eof() ) {
86         cout << "GenericAVC::VendorModel::VendorModel: error in parsing" << endl;
87     }
88 }
89
90 GenericAVC::VendorModel::~VendorModel()
91 {
92     for ( VendorModelEntryVector::iterator it = m_vendorModelEntries.begin();
93           it != m_vendorModelEntries.end();
94           ++it )
95     {
96         delete *it;
97     }
98 }
99
100 const GenericAVC::VendorModelEntryVector&
101 GenericAVC::VendorModel::getVendorModelEntries() const
102 {
103     return m_vendorModelEntries;
104 }
Note: See TracBrowser for help on using the browser.