xref: /haiku/src/add-ons/media/media-add-ons/usb_vision/TunerLocale.cpp (revision b3d94504c208adafebbf2ba62ae7fc4e0b7d68f7)
1 /*
2  * This file is a part of BeOS USBVision driver project.
3  * Copyright (c) 2003 by Siarzuk Zharski <imker@gmx.li>
4  *
5  * This file may be used under the terms of the BSD License
6  *
7  */
8 #include <Path.h>
9 #include <Directory.h>
10 #include <Entry.h>
11 #include <FindDirectory.h>
12 #include <fstream.h>
13 
14 #include "TunerLocale.h"
15 
16 using namespace Locale;
17 
18 const char *kLocalesSettings = "media/usb_vision/Locales";
19 
Channel(string & str,uint32 frequency)20 Channel::Channel(string &str, uint32 frequency):
21                  fName(str), fFrequency(frequency)
22 {
23 }
24 
Name() const25 const string &Channel::Name() const
26 {
27   return fName;
28 }
Frequency()29 const uint32 Channel::Frequency()
30 {
31   return fFrequency;
32 }
33 
TunerLocale(BEntry & entry)34 TunerLocale::TunerLocale(BEntry &entry)
35 {
36   fStatus = B_NO_INIT;
37   if(entry.IsFile()){
38     BPath path;
39     if((fStatus = entry.GetPath(&path)) == B_OK){
40       char name[B_FILE_NAME_LENGTH];
41       if((fStatus = entry.GetName(name)) == B_OK){
42         fName = name;
43         fStatus = LoadLocale(path.Path());
44       }
45     }
46   }
47 }
48 
~TunerLocale()49 TunerLocale::~TunerLocale()
50 {
51 }
52 
InitCheck()53 status_t TunerLocale::InitCheck()
54 {
55   return fStatus;
56 }
57 
LoadLocale(const char * name)58 status_t TunerLocale::LoadLocale(const char *name)
59 {
60   status_t status = B_OK;
61   ifstream ifs(name);
62   while(ifs.good() && !ifs.eof()){
63     string str;
64     getline(ifs, str);
65     //TODO : validity check and parse strings ...
66     str.erase(0, str.find_first_not_of(" \t"));
67     str.erase(str.find_last_not_of(" \t") + 1);
68     switch(str[0]){
69     case ';': continue;
70     case '@':
71       str.erase(0, 1);
72       fName = str;
73       break;
74     default: //TODO parse ...
75       Channel channel(str, 0);
76       fChannels.push_back(channel);
77       break;
78     }
79   }
80   return status;
81 }
82 
LoadLocales(vector<TunerLocale * > & vector)83 status_t TunerLocale::LoadLocales(vector<TunerLocale *> &vector)
84 {
85   status_t status = B_ERROR;
86   BPath path;
87   if((status = find_directory(B_USER_SETTINGS_DIRECTORY, &path)) != B_OK)
88     return status;
89   if((status = path.Append(kLocalesSettings)) != B_OK)
90     return status;
91   BDirectory directory(path.Path());
92   if((status = directory.InitCheck()) != B_OK)
93     return status;
94   vector.clear();
95   BEntry entry;
96   while((status = directory.GetNextEntry(&entry)) == B_OK){
97     if(entry.IsFile()){
98       TunerLocale *locale = new TunerLocale(entry);
99       if((status = locale->InitCheck()) == B_OK){
100         vector.push_back(locale);
101       }
102       else
103         delete locale;
104     }
105   }
106   if(status == B_ENTRY_NOT_FOUND) //we reached the end of entries list ...
107     status = B_OK;
108   return status;
109 }
110 
ReleaseLocales(vector<TunerLocale * > & vector)111 status_t TunerLocale::ReleaseLocales(vector<TunerLocale *> &vector)
112 {
113   for(LocalesIterator locale = vector.begin();
114                locale != vector.end(); locale ++){
115     delete *locale;
116   }
117   return B_OK;
118 }
119 
Name()120 const string &TunerLocale::Name()
121 {
122   return fName;
123 }
124 
GetChannel(int idx)125 const Channel &TunerLocale::GetChannel(int idx)
126 {
127   if(idx < 0) idx = 0;
128   if(idx >= (int)fChannels.size()) idx = (int)(fChannels.size() - 1);
129   return fChannels[idx];
130 }
131 
ChannelsCount()132 uint32 TunerLocale::ChannelsCount()
133 {
134   return fChannels.size();
135 }
136