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

Revision 620, 5.8 kB (checked in by wagi, 17 years ago)

- one tokenize function is enough. currently in serialize.h defined, this might change
- saveCache code workover. The cache is now created in ~/.ffado/cache. It is actived since it should break
things. Otherwise it needs fixing.

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