summaryrefslogtreecommitdiffabout
path: root/gconf/gconf-format.c
blob: ea8a8087719ef3061fcb3f37968da2965f7783c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/* gconf - General purpose configuration parser.
   Copyright (C) 2007, 2008, 2009 Sergey Poznyakoff

   This program is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the
   Free Software Foundation; either version 3 of the License, or (at your
   option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License along
   with this program. If not, see <http://www.gnu.org/licenses/>. */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <gconf.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

#if ENABLE_NLS
# include "gettext.h"
#else
# define gettext(s) s
#endif

#define _(s) gettext (s)
#define N_(s) s

const char *
gconf_data_type_string (enum gconf_data_type type)
{
  switch (type)
    {
    case gconf_type_void:
      return "void";
      
    case gconf_type_string:
      return "string";

    case gconf_type_short:
    case gconf_type_ushort:
    case gconf_type_int:
    case gconf_type_uint:
    case gconf_type_long:
    case gconf_type_ulong:
    case gconf_type_size:
/*  case  gconf_type_off:*/
    case gconf_type_uintmax:
    case gconf_type_intmax:
      return "number";

    case gconf_type_time:
      return "time";

    case gconf_type_bool:
      return "boolean";

    case gconf_type_ipv4:
      return "IPv4";

    case gconf_type_cidr:
      return "CIDR";

    case gconf_type_host:
      return "hostname";

    case gconf_type_sockaddr:
      return "sock-addr";

    case gconf_type_section:
      return "section";
    }
  return "UNKNOWN?";
}

static void
format_level (FILE *stream, unsigned level)
{
  while (level--)
    fprintf (stream, "  ");
}

void
gconf_format_docstring (FILE *stream, const char *docstring, unsigned level)
{
  size_t len = strlen (docstring);
  int width = 78 - level * 2;

  if (width < 0)
    {
      width = 78;
      level = 0;
    }

  while (len)
    {
      size_t seglen;
      const char *p;

      for (seglen = 0, p = docstring; p < docstring + width && *p; p++)
	{
	  if (*p == '\n')
	    {
	      seglen = p - docstring;
	      break;
	    }
	  if (isspace (*p))
	    seglen = p - docstring;
	}
      if (seglen == 0 || *p == 0)
	seglen = p - docstring;

      format_level (stream, level);
      fprintf (stream, "# ");
      fwrite (docstring, seglen, 1, stream);
      fputc ('\n', stream);
      len -= seglen;
      docstring += seglen;
      if (*docstring == '\n')
	{
	  docstring++;
	  len--;
	}
      else
	while (*docstring && isspace (*docstring))
	  {
	    docstring++;
	    len--;
	  }
    }
}

void
gconf_format_simple_statement (FILE *stream, struct gconf_keyword *kwp,
			       unsigned level)
{
  const char *argstr;

  if (kwp->docstring)
    gconf_format_docstring (stream, kwp->docstring, level);
  format_level (stream, level);

  if (kwp->argname)
    argstr = kwp->argname;
  else
    argstr = N_("arg");

  if (strchr ("<[", argstr[0]))
    fprintf (stream, "%s %s;\n", kwp->ident, gettext (argstr));
  else if (strchr (argstr, ':'))
    fprintf (stream, "%s <%s>;\n", kwp->ident, gettext (argstr));
  else
    {
      fprintf (stream, "%s <%s: ", kwp->ident, gettext (argstr));
      if (GCONF_IS_LIST (kwp->type))
	fprintf (stream, "list of %s",
		 gettext (gconf_data_type_string (GCONF_TYPE (kwp->type))));
      else
	fprintf (stream, "%s", gettext (gconf_data_type_string (kwp->type)));
      fprintf (stream, ">;\n");
    }
}

void
gconf_format_block_statement (FILE *stream, struct gconf_keyword *kwp,
			      unsigned level)
{
  if (kwp->docstring)
    gconf_format_docstring (stream, kwp->docstring, level);
  format_level (stream, level);
  fprintf (stream, "%s", kwp->ident);
  if (kwp->argname)
    fprintf (stream, " <%s>", gettext (kwp->argname));
  fprintf (stream, " {\n");
  gconf_format_statement_array (stream, kwp->kwd, 0, level + 1);
  format_level (stream, level);
  fprintf (stream, "}\n");
}

void
gconf_format_statement_array (FILE *stream, struct gconf_keyword *kwp,
			      unsigned n,
			      unsigned level)
{
  for (; kwp->ident; kwp++, n++)
    {
      if (n)
	fputc ('\n', stream);
      if (kwp->type == gconf_type_section)
	gconf_format_block_statement (stream, kwp, level);
      else
	gconf_format_simple_statement (stream, kwp, level);
    }
}

Return to:

Send suggestions and report system problems to the System administrator.