All-in-One programming by Song jingshan
In the past, I thought it would be nice to understand the file. When I realized it, I couldn't even understand it ~
The INI file consists of several sections with names starting from []. Each section is composed of several key = value, and the equal sign can be left or right by several spaces or tables. Each line is equal to or equal; it starts with a comment. There are multiple empty row bins between two sections.
Now that XML is emerging, the INI file is a bit earthy. Convert the INI file to an XML file
Ini
; Configuration of HTTP
[Http]
Domain = www.mysite.com
Port = 8080
Cgihome =/cgi-bin
; Configuration of DB
[Database]
Server = MySQL
User = myname
Passwoed = toopendatabase
XML
<! -- Configuration of HTTP -->
<Http>
<Domain> www.mysite.com </domain>
<Port> 8080 </port>
<Cgihome>/cgi-bin </cgihome>
</HTTP>
<! -- Configuration of DB -->
<Database>
<Server> mysql </Server>
<User> myname </user>
<Passwoed> toopendatabase </passwoed>
</Database>
The following is the source code I wrote. I think the failure is to read the entire code by row:
Code:
- /* Almost every Ni Huan HUan Juan, ML Huan HUan */
- # Include <stdio. h>
- # Include <stdlib. h>
- # Include <string. h>
- # Define M 1024
- # Define n 100
- Const char match_char [] = {';', '[', '= '};
- /* The Conversion Result is "empty" and "empty */
- /* Just a few clicks, just a few clicks, a few clicks, and a few clicks */
- Int match (const char * STR, int * n)
- {
- Int I = 0, Len = sizeof (match_char)/sizeof (match_char [0]);
- Const char * P;
- For (I; I <Len; I ++)
- {
- P = STR;
- * N = 0;
- While (* P! = '/0 ')
- {
- If (match_char [I] = * P)
- Return I;
- P ++;
- (* N) ++;
- }
- }
- Return-1;
- }
- Void semicolon (char * Buf, file * out)
- {
- Fprintf (Out, "<! -- % S -->/N ", BUF + 1 );
- }
- Char * brackets (char * Buf, file * out)
- {
- Int Len = strlen (BUF );
- * (BUF + len-1) = '/0 ';
- Fprintf (Out, "<% S>/N", BUF + 1, BUF + 1 );
- Return BUF + 1;
- }
- Void equal (char * allbuf, file * Out, int N)
- {
- Int I = 0;
- Char halfbuf [m/2];
- For (I; (allbuf [I]! = '') & (Allbuf [I]! = '') & (I <n); I ++)
- {
- Halfbuf [I] = allbuf [I];
- }
- Allbuf = & allbuf [n + 1];
- Halfbuf [I] = '/0 ';
- While (* allbuf = ''| * allbuf = '')
- Allbuf ++;
- Fprintf (Out, "<% S> % S </% S>/N", halfbuf, allbuf, halfbuf );
- }
- Int main (INT argc, char * argv [])
- {
- File * In, * out;
- Char * in_name = "1.ini ";
- Char * out_name = "1.xml ";
- Char Buf [m], name [100] = {0}, * P;
- Int I, n, flag;
- If (in = fopen (in_name, "R") = NULL)
- {
- Printf ("can not open % s/n", in_name );
- Exit (0 );
- }
- If (out = fopen (out_name, "W +") = NULL)
- {
- Printf ("can not open % s/n", out_name );
- Exit (0 );
- }
- While (! Feof (in ))
- {
- Flag = 0;
- Do
- {
- Fgets (BUF, M, in );
- If (flag = 0 & * Buf = '/N ')
- {
- Fprintf (Out, "</% S>/n", name );
- Flag = 1;
- }
- Else if (* Buf = '/N ')
- {
- Fprintf (Out, "/N ");
- }
-
- } While (* Buf = '/N ');
-
- P = strchr (BUF, '/N ');
- If (P! = NULL)
- * P = '/0 ';
- I = match (BUF, & N );
- If (match_char [I]! = '& N! = 0)
- {
- Printf ("error: It's not INI file! /N ");
- Exit (0 );
- }
- Switch (I)
- {
- Case 0:
- Semicolon (BUF, out); // contains multiple rows
- Break;
- Case 1:
- // If (* name! = '/0 ')
- // Fprintf (Out, "</% S>/N", name );
- Strncpy (name, brackets (BUF, out), n); // What is the limit?
- Break;
- Case 2:
- // Fseek (Out, (long) (-(LEN + 2), seek_end); // when else </string> else? Equal (BUF, out, N );
- // Fseek (Out, 0, seek_end); // please wait until then
- Break;
- Default:
- Printf ("error: no such symbol! /N ");
- Exit (0 );
- }
- }
- Fprintf (Out, "</% S>/N", name );
- Fclose (in );
- Fclose (out );
- Printf ("sucess! /N ");
- Return 0;
- }