2 ***************************************************************************
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 ***************************************************************************
22 Implement getkeystring / writekeystring like windows API.
26 -------- ---------- ----------------------------------------------
27 Paul Wu 01-22-2003 created
35 #include "configapi.h"
37 int isfound(char *ptr
, char *ini_buffer
)
41 else if(ptr
>ini_buffer
)
43 while(ptr
> ini_buffer
)
49 else if( (*ptr
== ' ') || (*ptr
== '\t'))
59 char *find_header(char *ini_buffer
, char *header_name
)
61 char temp_buf
[MAX_CHARS
];
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
));
79 int get_parameter(char *str
, int strsize
, char *key
, char *offset
)
81 char temp_buf1
[MAX_CHARS
];
82 char temp_buf2
[MAX_CHARS
];
89 strcpy(temp_buf1
, NEWLINE
);
90 strcat(temp_buf1
, key
);
91 strcat(temp_buf1
, "=");
93 if((start_ptr
=strstr(offset
, temp_buf1
))==NULL
)
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
)
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
)
122 strcpy(temp_buf2
, start_ptr
+1);
127 if( (*ptr
== ' ') || (*ptr
== '\t') )
133 memset(str
, 0x00, strsize
);
134 strncpy(str
, ptr
, len
>= strsize
? strsize
: len
);
138 char *find_parameter(char *key
, char *offset
, int *length
)
140 char temp_buf1
[MAX_CHARS
];
141 char temp_buf2
[MAX_CHARS
];
148 strcpy(temp_buf1
, NEWLINE
);
149 strcat(temp_buf1
, key
);
150 strcat(temp_buf1
, "=");
151 strcpy(temp_buf2
, NEWLINE
);
152 strcat(temp_buf2
, "[");
155 end_ptr
=strstr(offset
+1, temp_buf2
);
156 start_ptr
=strstr(offset
, temp_buf1
);
160 ptr
= strstr(offset
, temp_buf2
);
162 *length
= strlen(offset
)-1;
164 *length
= ptr
- offset
;
167 else if((end_ptr
== NULL
) || (start_ptr
< end_ptr
) )
169 ptr
= strstr(start_ptr
+1, NEWLINE
);
171 *length
= strlen(start_ptr
)+strlen(NEWLINE
);
173 *length
= ptr
- start_ptr
;
174 return (start_ptr
+strlen(NEWLINE
));
177 *length
= end_ptr
-offset
-strlen(NEWLINE
);
186 int writekeystring(char *section
, char *key
, char *str
, char *path
, char *filename
)
194 char temp_buf
[MAX_CHARS
];
195 char *ini_buffer
; /* storage area for .INI file */
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
);
207 //printf("%s\n", cfgfile);
208 if((handle
= fopen(cfgfile
, "wb")) != NULL
)
210 fprintf(handle
, "[%s]\n%s=%s\n", section
, key
, str
);
216 //printf("open failed\n");
220 if ((fseek(handle
, 0, SEEK_END
))!=0)
222 filesize
=ftell(handle
);
227 fprintf(handle
, "[%s]\n%s=%s\n", section
, key
, str
);
232 if (filesize
>65534) //File to big
235 if ((ini_buffer
=(char *)malloc(filesize
+ 1))==NULL
)
236 return (FALSE
); //out of memory
238 fread(ini_buffer
, filesize
, 1, handle
);
240 if((handle
= fopen(cfgfile
, "wb")) == NULL
)
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
);
251 if((ptr
=find_parameter(key
, offset
, &length
)) == NULL
)
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
);
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
);
277 int getkeystring(char *section
, char *key
, char *dest
, int strsize
, char *path
, char *filename
)
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 */
294 if ((fseek(handle
, 0, SEEK_END
))!=0)
302 if ((ini_buffer
=(char *)malloc(len
+ 1))==NULL
)
305 fread(ini_buffer
, len
, 1, handle
);
307 ini_buffer
[len
]='\0';
308 if((offset
=find_header(ini_buffer
, section
)) == NULL
)
310 if(get_parameter(dest
, strsize
, key
, offset
) == 0)
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);
325 printf("not found\n");
327 if(writekeystring("RT2460", "Profile2", "ok", "/etc/cfg", "sample.ini"))
328 printf("=== End ===\n");