MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / net / wireless / rtlink / Utility / configapi.cpp
blob0d1e35910fc35bf81c79f7e988b53cf5a432b00c
1 /*
2 ***************************************************************************
3 * Ralink Tech Inc.
4 * 4F, No. 2 Technology 5th Rd.
5 * Science-based Industrial Park
6 * Hsin-chu, Taiwan, R.O.C.
8 * (c) Copyright 2002, Ralink Technology, Inc.
10 * All rights reserved. Ralink's source code is an unpublished work and the
11 * use of a copyright notice does not imply otherwise. This source code
12 * contains confidential trade secret material of Ralink Tech. Any attemp
13 * or participation in deciphering, decoding, reverse engineering or in any
14 * way altering the source code is stricitly prohibited, unless the prior
15 * written consent of Ralink Technology, Inc. is obtained.
16 ***************************************************************************
18 Module Name:
19 configapi.cpp
21 Abstract:
22 Implement getkeystring / writekeystring like windows API.
24 Revision History:
25 Who When What
26 -------- ---------- ----------------------------------------------
27 Paul Wu 01-22-2003 created
31 #include <string.h>
32 #include <stdio.h>
33 #include <stdlib.h>
35 #include "configapi.h"
37 int isfound(char *ptr, char *ini_buffer)
39 if(ptr == ini_buffer)
40 return (1);
41 else if(ptr>ini_buffer)
43 while(ptr > ini_buffer)
45 ptr--;
47 if(*ptr == 0x0a)
48 return (1);
49 else if( (*ptr == ' ') || (*ptr == '\t'))
50 continue;
51 else
52 return (0);
54 return (1);
56 return (0);
59 char *find_header(char *ini_buffer, char *header_name)
61 char temp_buf[MAX_CHARS];
62 char *ptr;
64 strcpy(temp_buf, "["); /* and the opening bracket [ */
65 strcat(temp_buf, header_name);
66 strcat(temp_buf, "]");
68 if((ptr = strstr(ini_buffer, temp_buf)) != NULL)
70 if(isfound(ptr, ini_buffer))
71 return (ptr+strlen(NEWLINE));
72 else
73 return NULL;
75 else
76 return NULL;
79 int get_parameter(char *str, int strsize, char *key, char *offset)
81 char temp_buf1[MAX_CHARS];
82 char temp_buf2[MAX_CHARS];
83 char *start_ptr;
84 char *end_ptr;
85 char *ptr;
86 char *too_far_ptr;
87 int len;
89 strcpy(temp_buf1, NEWLINE);
90 strcat(temp_buf1, key);
91 strcat(temp_buf1, "=");
93 if((start_ptr=strstr(offset, temp_buf1))==NULL)
95 *str=0x00;
96 return (0);
98 start_ptr+=strlen(NEWLINE);
100 if((too_far_ptr=strstr(offset+1, "["))==NULL)
101 too_far_ptr=offset+strlen(offset);
103 if((end_ptr=strstr(start_ptr, NEWLINE))==NULL)
104 end_ptr=start_ptr+strlen(start_ptr);
106 if (too_far_ptr<start_ptr)
108 *str=0x00;
109 return(0);
112 memcpy(temp_buf2, start_ptr, end_ptr-start_ptr);
113 temp_buf2[end_ptr-start_ptr]='\0';
114 len = strlen(temp_buf2);
115 strcpy(temp_buf1, temp_buf2);
116 if((start_ptr=strstr(temp_buf1, "=")) == NULL)
118 *str=0x00;
119 return (0);
122 strcpy(temp_buf2, start_ptr+1);
124 ptr = temp_buf2;
125 while(*ptr != 0x00)
127 if( (*ptr == ' ') || (*ptr == '\t') )
128 ptr++;
129 else
130 break;
132 len = strlen(ptr);
133 memset(str, 0x00, strsize);
134 strncpy(str, ptr, len >= strsize ? strsize: len);
135 return (1);
138 char *find_parameter(char *key, char *offset, int *length)
140 char temp_buf1[MAX_CHARS];
141 char temp_buf2[MAX_CHARS];
142 char *start_ptr;
143 char *end_ptr;
144 char *ptr;
145 //char *too_far_ptr;
146 //int len;
148 strcpy(temp_buf1, NEWLINE);
149 strcat(temp_buf1, key);
150 strcat(temp_buf1, "=");
151 strcpy(temp_buf2, NEWLINE);
152 strcat(temp_buf2, "[");
153 *length = 0;
155 end_ptr=strstr(offset+1, temp_buf2);
156 start_ptr=strstr(offset, temp_buf1);
157 if(start_ptr==NULL)
158 { //not found key;
160 ptr = strstr(offset, temp_buf2);
161 if(ptr == NULL)
162 *length = strlen(offset)-1;
163 else
164 *length = ptr - offset;
165 return NULL;
167 else if((end_ptr == NULL) || (start_ptr < end_ptr) )
169 ptr = strstr(start_ptr+1, NEWLINE);
170 if(ptr == NULL)
171 *length = strlen(start_ptr)+strlen(NEWLINE);
172 else
173 *length = ptr - start_ptr;
174 return (start_ptr+strlen(NEWLINE));
176 else
177 *length = end_ptr-offset-strlen(NEWLINE);
179 return NULL;
183 TRUE - Success
184 FALSE - Fail
186 int writekeystring(char *section, char *key, char *str, char *path, char *filename)
188 char *cfgfile;
189 char *offset;
190 int length;
191 int templen;
192 long filesize;
193 FILE *handle;
194 char temp_buf[MAX_CHARS];
195 char *ini_buffer; /* storage area for .INI file */
196 char *ptr;
197 //char *rest=" ";
199 if ((cfgfile=(char *)malloc(strlen(path)+strlen(filename)+2))==NULL)
200 return (FALSE); //out of memory
202 sprintf(cfgfile, "%s/%s", path, filename);
203 if ((handle=fopen(cfgfile, "r+b"))==NULL) /* Binary mode, no CR/LF */
205 sprintf(temp_buf, "mkdir -p %s", path);
206 system(temp_buf);
207 //printf("%s\n", cfgfile);
208 if((handle = fopen(cfgfile, "wb")) != NULL)
210 fprintf(handle, "[%s]\n%s=%s\n", section, key, str);
211 fclose(handle);
212 return (TRUE);
214 else
216 //printf("open failed\n");
217 return (FALSE);
220 if ((fseek(handle, 0, SEEK_END))!=0)
221 return (FALSE);
222 filesize=ftell(handle);
223 rewind(handle);
225 if(filesize == 0)
227 fprintf(handle, "[%s]\n%s=%s\n", section, key, str);
228 fclose(handle);
229 return (TRUE);
232 if (filesize >65534) //File to big
233 return (FALSE);
235 if ((ini_buffer=(char *)malloc(filesize + 1))==NULL)
236 return (FALSE); //out of memory
238 fread(ini_buffer, filesize, 1, handle);
239 fclose(handle);
240 if((handle = fopen(cfgfile, "wb")) == NULL)
241 return (FALSE);
243 ini_buffer[filesize]='\0';
244 if((offset=find_header(ini_buffer, section)) == NULL)
245 {//not found section.
246 fprintf(handle, "%s", ini_buffer);
247 fprintf(handle, "\n[%s]\n%s=%s\n", section, key, str);
248 fclose(handle);
249 return (TRUE);
251 if((ptr=find_parameter(key, offset, &length)) == NULL)
252 { //not found key;
253 rewind(handle);
254 templen = offset - ini_buffer;
255 fwrite(ini_buffer, templen+length, 1, handle);
256 fprintf(handle, "\n%s=%s%s", key, str, ini_buffer+templen+length);
257 fclose(handle);
258 return (TRUE);
260 else
262 rewind(handle);
263 templen = ptr - ini_buffer;
264 fwrite(ini_buffer, templen, 1, handle);
265 fprintf(handle, "%s=%s\n%s", key, str, ini_buffer+templen+length);
266 fclose(handle);
267 return (TRUE);
270 return (FALSE);
274 TRUE - Success
275 FALSE - Fail
277 int getkeystring(char *section, char *key, char *dest, int strsize, char *path, char *filename)
279 char *cfgfile;
280 char *offset;
281 //int length;
282 long len;
283 FILE *handle;
284 //char temp_buf[MAX_CHARS];
285 char *ini_buffer; /* storage area for .INI file */
287 if ((cfgfile=(char *)malloc(strlen(path)+strlen(filename)+2))==NULL)
288 return (FALSE); //out of memory
290 sprintf(cfgfile, "%s/%s", path, filename);
292 if ((handle=fopen(cfgfile, "rb"))==NULL) /* Binary mode, no CR/LF */
293 return (FALSE);
294 if ((fseek(handle, 0, SEEK_END))!=0)
295 return (FALSE);
296 len=ftell(handle);
297 rewind(handle);
299 if (len >65534)
300 return (FALSE);
302 if ((ini_buffer=(char *)malloc(len + 1))==NULL)
303 return (FALSE);
305 fread(ini_buffer, len, 1, handle);
306 fclose(handle);
307 ini_buffer[len]='\0';
308 if((offset=find_header(ini_buffer, section)) == NULL)
309 return (FALSE);
310 if(get_parameter(dest, strsize, key, offset) == 0)
311 return (FALSE);
313 return (TRUE);
317 int main(void)
319 char dest[MAX_CHARS];
320 //int getkeystring(char *section, char *key, char *str, int strsize, char *filename)
322 if(getkeystring("RT2460", "Profile2", dest, MAX_CHARS, "/etc/cfg", "sample.ini"))
323 printf("dest=%s\n", dest);
324 else
325 printf("not found\n");
327 if(writekeystring("RT2460", "Profile2", "ok", "/etc/cfg", "sample.ini"))
328 printf("=== End ===\n");
329 return 0;