wmcliphist: Fix a typo in the Makefile
[dockapps.git] / wmradio / stationnames.c
blob0fdd8af6518a0315ffaa53145eaaca2683a3abea
1 /*
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"
22 #include "rc.h"
23 #include <string.h>
24 #include <stdlib.h>
25 #include <math.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);
34 return buffer;
37 int ato100i(char *str)
39 char *p,*q;
40 int result,tmp;
42 p = strstr(str,".");
43 if(!p) {
44 return atoi(str) * 100;
46 q = p; q++;
47 *p = 0;
48 result = atoi(str) * 100;
49 tmp = atoi(q);
50 *p = '.';
52 switch(strlen(q)) {
53 case 1:
54 result += 10 * tmp;
55 break;
56 case 2:
57 result += tmp;
59 return result;
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)
69 IniSection *section;
70 IniVariable *variable;
71 List *item;
72 int cfreq;
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))
80 return variable;
82 return NULL;
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)
106 IniSection *section;
107 List *item,*current;
108 IniVariable *item_var;
109 int delta;
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));
117 while(item) {
118 item_var = item->data;
119 if(abs(freq - ato100i(item_var->value)) < delta) {
120 current = item;
121 delta = abs(freq - ato100i(item_var->value));
123 item = item->next;
125 return current;
128 int station_next_freq(int freq)
130 IniVariable *item_var;
131 List *item;
133 item = station_nearest(freq);
134 if(!item) return freq;
135 if(item->next) {
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;
147 List *item;
149 item = station_nearest(freq);
150 if(!item) return freq;
151 if(item->prev) {
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);