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

Revision 599, 5.7 kB (checked in by wagi, 17 years ago)

Instead of static compiled in vendor/model table use configuration files.

Maybe needs some more cleanup but I wanted to check in this baby before
someone else screws me up with some majors changes in the repos :)

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 #include <cerrno>
31 #include <functional>
32 #include <algorithm>
33
34 using namespace std;
35
36 static void
37 tokenize(const string& str,
38          vector<string>& tokens,
39          const string& delimiters = " ")
40 {
41     // Skip delimiters at beginning.
42     string::size_type lastPos = str.find_first_not_of(delimiters, 0);
43     // Find first "non-delimiter".
44     string::size_type pos     = str.find_first_of(delimiters, lastPos);
45
46     while (string::npos != pos || string::npos != lastPos)
47     {
48         // Found a token, add it to the vector.
49         tokens.push_back(str.substr(lastPos, pos - lastPos));
50         // Skip delimiters.  Note the "not_of"
51         lastPos = str.find_first_not_of(delimiters, pos);
52         // Find next "non-delimiter"
53         pos = str.find_first_of(delimiters, lastPos);
54     }
55 }
56
57 //-------------------------------------------------
58
59 GenericAVC::VendorModelEntry::VendorModelEntry()
60     : vendor_id( 0 )
61     , model_id( 0 )
62 {
63 }
64
65 GenericAVC::VendorModelEntry::VendorModelEntry( const VendorModelEntry& rhs )
66     : vendor_id( rhs.vendor_id )
67     , model_id( rhs.model_id )
68     , vendor_name( rhs.vendor_name )
69     , model_name( rhs.model_name )
70 {
71 }
72
73 GenericAVC::VendorModelEntry&
74 GenericAVC::VendorModelEntry::operator = ( const VendorModelEntry& rhs )
75 {
76     // check for assignment to self
77     if ( this == &rhs ) return *this;
78
79     vendor_id   = rhs.vendor_id;
80     model_id    = rhs.model_id;
81     vendor_name = rhs.vendor_name;
82     model_name  = rhs.model_name;
83
84     return *this;
85 }
86
87 GenericAVC::VendorModelEntry::~VendorModelEntry()
88 {
89 }
90
91 GenericAVC::VendorModel::VendorModel( const char* filename )
92     : m_filename( filename )
93 {
94 }
95
96 GenericAVC::VendorModel::~VendorModel()
97 {
98 }
99
100
101 bool
102 GenericAVC::VendorModel::parse()
103 {
104     ifstream in ( m_filename.c_str() );
105
106     if ( !in ) {
107         perror( m_filename.c_str() );
108         return false;
109     }
110
111     string line;
112     while ( !getline( in,  line ).eof() ) {
113         string::size_type i = line.find_first_not_of( " \t\n\v" );
114         if ( i != string::npos && line[i] == '#' )
115             // this line starts with a '#' -> comment
116             continue;
117
118         vector<string> tokens;
119         tokenize( line, tokens, "," );
120
121         if ( tokens.size() < 4 )
122             // ignore this line, it has not all needed mandatory entries
123             continue;
124
125         VendorModelEntry vme;
126         vector<string>::const_iterator it = tokens.begin();
127         char* tail;
128
129         errno = 0;
130         vme.vendor_id   = strtol( it++->c_str(), &tail, 0 );
131         vme.model_id    = strtol( it++->c_str(), &tail, 0 );
132         vme.vendor_name = *it++;
133         vme.model_name  = *it++;
134
135         if ( errno )
136             // string -> int conversion failed
137             continue;
138
139         vector<string>::const_iterator end = tokens.end();
140         if ( it != end )
141             handleAdditionalEntries( vme, tokens, it, end );
142
143         m_vendorModelEntries.push_back( vme );
144     }
145
146     if ( !in.eof() ) {
147         cerr << "GenericAVC::VendorModel::VendorModel: error in parsing" << endl;
148         return false;
149     }
150
151     return true;
152 }
153
154 bool
155 GenericAVC::VendorModel::printTable() const
156 {
157     // Some debug output
158     cout << "vendorId\t\tmodelId\t\tvendorName\t\t\t\tmodelName" << endl;
159     for ( VendorModelEntryVector::const_iterator it = m_vendorModelEntries.begin();
160           it != m_vendorModelEntries.end();
161           ++it )
162     {
163         cout << it->vendor_id << "\t\t\t"
164              << it->model_id << "\t"
165              << it->vendor_name << "\t"
166              << it->model_name << endl;
167     }
168     return true;
169 }
170
171 bool
172 GenericAVC::VendorModel::handleAdditionalEntries(VendorModelEntry& vme,
173                                                  vector<string>& v,
174                                                  vector<string>::const_iterator& b,
175                                                  vector<string>::const_iterator& e )
176 {
177     return true;
178 }
179
180 class is_same : public unary_function<GenericAVC::VendorModelEntry, bool> {
181 public:
182     is_same( unsigned int vendor_id, unsigned model_id )
183         : m_vendor_id( vendor_id )
184         , m_model_id( model_id )
185     {}
186
187     bool operator () ( const GenericAVC::VendorModelEntry& vme ) const {
188         return vme.vendor_id == m_vendor_id && vme.model_id == m_model_id;
189     }
190
191 private:
192     unsigned int m_vendor_id;
193     unsigned int m_model_id;
194 };
195
196 GenericAVC::VendorModelEntry*
197 GenericAVC::VendorModel::find( unsigned int vendor_id, unsigned model_id )
198 {
199     VendorModelEntryVector::iterator it =
200         find_if ( m_vendorModelEntries.begin(),
201                   m_vendorModelEntries.end(),
202                   is_same( vendor_id, model_id ) );
203     if ( it != m_vendorModelEntries.end() )
204         return &*it;
205
206     return 0;
207 }
208
209 const GenericAVC::VendorModelEntryVector&
210 GenericAVC::VendorModel::getVendorModelEntries() const
211 {
212     return m_vendorModelEntries;
213 }
Note: See TracBrowser for help on using the browser.