2 * Copyright (C) 12 Jun 2003 Tomas Cermak
4 * This file is part of wmradio program.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 #include "stationnames.h"
27 #define STATION_NAMES_INTERVAL 0.03
29 char *freqtostr(int freq
)
31 static char buffer
[10];
33 sprintf(buffer
, "%i.%.2i\n", freq
/ 100,freq
% 100);
37 int ato100i(char *str
)
44 return atoi(str
) * 100;
48 result
= atoi(str
) * 100;
62 IniVariable
*station_find_by_name(char *name
)
64 return rc_get_variable_ini_variable(SECTION_NAMES
,name
);
67 IniVariable
*station_find_by_freq(int freq
)
70 IniVariable
*variable
;
74 section
= rc_get_section(SECTION_NAMES
);
75 if(!section
) return NULL
;
76 for(item
= section
->variables
; item
; item
= item
->next
) {
77 variable
= item
->data
;
78 cfreq
= ato100i(variable
->value
);
79 if((cfreq
> freq
- STATION_NAMES_INTERVAL
) &&(cfreq
< freq
+ STATION_NAMES_INTERVAL
))
85 void station_add(char *name
, int freq
)
87 rc_set_variable(SECTION_NAMES
,name
,freqtostr(freq
));
90 char *station_get_freq_name(int freq
)
92 IniVariable
*variable
;
94 variable
= station_find_by_freq(freq
);
95 if(!variable
) return NULL
;
96 return variable
->name
;
99 void station_set_freq_name(int freq
, char *name
)
101 rc_set_variable(SECTION_NAMES
,name
,freqtostr(freq
));
104 List
*station_nearest(int freq
)
108 IniVariable
*item_var
;
111 section
= rc_get_section(SECTION_NAMES
);
112 if(! section
) return NULL
;
113 item
= current
= section
-> variables
;
114 if(!item
) return NULL
;
115 item_var
= item
->data
;
116 delta
= abs(freq
- ato100i(item_var
->value
));
118 item_var
= item
->data
;
119 if(abs(freq
- ato100i(item_var
->value
)) < delta
) {
121 delta
= abs(freq
- ato100i(item_var
->value
));
128 int station_next_freq(int freq
)
130 IniVariable
*item_var
;
133 item
= station_nearest(freq
);
134 if(!item
) return freq
;
136 item_var
= item
->next
->data
;
137 return ato100i(item_var
->value
);
139 while(item
->prev
) item
= item
->prev
;
140 item_var
= item
->data
;
141 return ato100i(item_var
->value
);
144 int station_prev_freq(int freq
)
146 IniVariable
*item_var
;
149 item
= station_nearest(freq
);
150 if(!item
) return freq
;
152 item_var
= item
->prev
->data
;
153 return ato100i(item_var
->value
);
155 while(item
->next
) item
= item
->next
;
156 item_var
= item
->data
;
157 return ato100i(item_var
->value
);