fork of PCE focusing on macplus, supporting DaynaPort SCSI network emulation
1/*****************************************************************************
2 * libini *
3 *****************************************************************************/
4
5/*****************************************************************************
6 * File name: src/libini/write.c *
7 * Created: 2001-08-24 by Hampa Hug <hampa@hampa.ch> *
8 * Copyright: (C) 2001-2010 Hampa Hug <hampa@hampa.ch> *
9 *****************************************************************************/
10
11/*****************************************************************************
12 * This program is free software. You can redistribute it and / or modify it *
13 * under the terms of the GNU General Public License version 2 as published *
14 * by the Free Software Foundation. *
15 * *
16 * This program is distributed in the hope that it will be useful, but *
17 * WITHOUT ANY WARRANTY, without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General *
19 * Public License for more details. *
20 *****************************************************************************/
21
22
23#include <config.h>
24
25#include <stdio.h>
26#include <string.h>
27
28#include <libini/libini.h>
29
30
31static
32int ini_write_indent (FILE *fp, unsigned level)
33{
34 while (level > 0) {
35 fputc ('\t', fp);
36 level -= 1;
37 }
38
39 return (0);
40}
41
42static
43int ini_write_val (FILE *fp, ini_val_t *val)
44{
45 fprintf (fp, "%s = ", val->name);
46
47 switch (val->type) {
48 case INI_VAL_INT:
49 fprintf (fp, "0x%lx", val->val.u32);
50 break;
51
52 case INI_VAL_STR:
53 fprintf (fp, "\"%s\"", val->val.str);
54 break;
55
56 default:
57 return (1);
58 }
59
60 return (0);
61}
62
63static
64int ini_write_section (FILE *fp, ini_sct_t *sct, unsigned indent)
65{
66 ini_val_t *val;
67
68 val = sct->val_head;
69
70 while (val != NULL) {
71 if (ini_write_indent (fp, indent)) {
72 return (1);
73 }
74
75 if (ini_write_val (fp, val)) {
76 return (1);
77 }
78
79 fputs ("\n", fp);
80
81 val = val->next;
82 }
83
84 sct = sct->sub_head;
85
86 while (sct != NULL) {
87 fputs ("\n", fp);
88
89 if (ini_write_indent (fp, indent)) {
90 return (1);
91 }
92
93 fprintf (fp, "%s {\n", sct->name);
94
95 if (ini_write_section (fp, sct, indent + 1)) {
96 return (1);
97 }
98
99 if (ini_write_indent (fp, indent)) {
100 return (1);
101 }
102
103 fputs ("}\n", fp);
104
105 sct = sct->next;
106 }
107
108 return (0);
109}
110
111int ini_write_fp (FILE *fp, ini_sct_t *sct)
112{
113 fputs ("# Generated automatically by libini\n\n", fp);
114
115 if (ini_write_section (fp, sct, 0)) {
116 return (1);
117 }
118
119 return (0);
120}
121
122int ini_write (const char *fname, ini_sct_t *sct)
123{
124 int r;
125 FILE *fp;
126
127 fp = fopen (fname, "wb");
128
129 if (fp == NULL) {
130 return (1);
131 }
132
133 r = ini_write_fp (fp, sct);
134
135 fclose (fp);
136
137 return (r);
138}