C applet that converts an INI file to an XML file

Source: Internet
Author: User

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:
  1. /* Almost every Ni Huan HUan Juan, ML Huan HUan */
  2. # Include <stdio. h>
  3. # Include <stdlib. h>
  4. # Include <string. h>
  5. # Define M 1024
  6. # Define n 100
  7. Const char match_char [] = {';', '[', '= '};
  8. /* The Conversion Result is "empty" and "empty */
  9. /* Just a few clicks, just a few clicks, a few clicks, and a few clicks */
  10. Int match (const char * STR, int * n)
  11. {
  12. Int I = 0, Len = sizeof (match_char)/sizeof (match_char [0]);
  13. Const char * P;
  14. For (I; I <Len; I ++)
  15. {
  16. P = STR;
  17. * N = 0;
  18. While (* P! = '/0 ')
  19. {
  20. If (match_char [I] = * P)
  21. Return I;
  22. P ++;
  23. (* N) ++;
  24. }
  25. }
  26. Return-1;
  27. }
  28. Void semicolon (char * Buf, file * out)
  29. {
  30. Fprintf (Out, "<! -- % S -->/N ", BUF + 1 );
  31. }
  32. Char * brackets (char * Buf, file * out)
  33. {
  34. Int Len = strlen (BUF );
  35. * (BUF + len-1) = '/0 ';
  36. Fprintf (Out, "<% S>/N", BUF + 1, BUF + 1 );
  37. Return BUF + 1;
  38. }
  39. Void equal (char * allbuf, file * Out, int N)
  40. {
  41. Int I = 0;
  42. Char halfbuf [m/2];
  43. For (I; (allbuf [I]! = '') & (Allbuf [I]! = '') & (I <n); I ++)
  44. {
  45. Halfbuf [I] = allbuf [I];
  46. }
  47. Allbuf = & allbuf [n + 1];
  48. Halfbuf [I] = '/0 ';
  49. While (* allbuf = ''| * allbuf = '')
  50. Allbuf ++;
  51. Fprintf (Out, "<% S> % S </% S>/N", halfbuf, allbuf, halfbuf );
  52. }
  53. Int main (INT argc, char * argv [])
  54. {
  55. File * In, * out;
  56. Char * in_name = "1.ini ";
  57. Char * out_name = "1.xml ";
  58. Char Buf [m], name [100] = {0}, * P;
  59. Int I, n, flag;
  60. If (in = fopen (in_name, "R") = NULL)
  61. {
  62. Printf ("can not open % s/n", in_name );
  63. Exit (0 );
  64. }
  65. If (out = fopen (out_name, "W +") = NULL)
  66. {
  67. Printf ("can not open % s/n", out_name );
  68. Exit (0 );
  69. }
  70. While (! Feof (in ))
  71. {
  72. Flag = 0;
  73. Do
  74. {
  75. Fgets (BUF, M, in );
  76. If (flag = 0 & * Buf = '/N ')
  77. {
  78. Fprintf (Out, "</% S>/n", name );
  79. Flag = 1;
  80. }
  81. Else if (* Buf = '/N ')
  82. {
  83. Fprintf (Out, "/N ");
  84. }
  85.  
  86. } While (* Buf = '/N ');
  87.  
  88. P = strchr (BUF, '/N ');
  89. If (P! = NULL)
  90. * P = '/0 ';
  91. I = match (BUF, & N );
  92. If (match_char [I]! = '& N! = 0)
  93. {
  94. Printf ("error: It's not INI file! /N ");
  95. Exit (0 );
  96. }
  97. Switch (I)
  98. {
  99. Case 0:
  100. Semicolon (BUF, out); // contains multiple rows
  101. Break;
  102. Case 1:
  103. // If (* name! = '/0 ')
  104. // Fprintf (Out, "</% S>/N", name );
  105. Strncpy (name, brackets (BUF, out), n); // What is the limit?
  106. Break;
  107. Case 2:
  108. // Fseek (Out, (long) (-(LEN + 2), seek_end); // when else </string> else? Equal (BUF, out, N );
  109. // Fseek (Out, 0, seek_end); // please wait until then
  110. Break;
  111. Default:
  112. Printf ("error: no such symbol! /N ");
  113. Exit (0 );
  114. }
  115. }
  116. Fprintf (Out, "</% S>/N", name );
  117. Fclose (in );
  118. Fclose (out );
  119. Printf ("sucess! /N ");
  120. Return 0;
  121. }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.