r5098
[geany-mirror.git] / tagmanager / conf.c
blob81c6927d5d28f50ff19343c246b64c2ace70fd73
1 /*
3 * Copyright (c) 2000-2001, Darren Hiebert
5 * This source code is released for free distribution under the terms of the
6 * GNU General Public License.
8 * This module contains functions for generating tags for config files.
9 */
12 * INCLUDE FILES
14 #include "general.h" /* must always come first */
16 #include <ctype.h>
18 #include "parse.h"
19 #include "read.h"
20 #include "vstring.h"
23 * DATA DEFINITIONS
25 typedef enum {
26 K_SECTION,
27 K_KEY
28 } confKind;
30 static kindOption ConfKinds [] = {
31 { TRUE, 'n', "namespace", "sections"},
32 { TRUE, 'm', "macro", "keys"}
36 * FUNCTION DEFINITIONS
39 static boolean isIdentifier (int c)
41 /* allow whitespace within keys and sections */
42 return (boolean)(isalnum (c) || isspace (c) || c == '_');
45 static void findConfTags (void)
47 vString *name = vStringNew ();
48 vString *scope = vStringNew ();
49 const unsigned char *line;
51 while ((line = fileReadLine ()) != NULL)
53 const unsigned char* cp = line;
54 boolean possible = TRUE;
56 if (isspace ((int) *cp) || *cp == '#' || (*cp != '\0' && *cp == '/' && *(cp+1) == '/'))
57 continue;
59 /* look for a section */
60 if (*cp != '\0' && *cp == '[')
62 ++cp;
63 while (*cp != '\0' && *cp != ']')
65 vStringPut (name, (int) *cp);
66 ++cp;
68 vStringTerminate (name);
69 makeSimpleTag (name, ConfKinds, K_SECTION);
70 /* remember section name */
71 vStringCopy (scope, name);
72 vStringTerminate (scope);
73 vStringClear (name);
74 continue;
77 while (*cp != '\0')
79 /* We look for any sequence of identifier characters following a white space */
80 if (possible && isIdentifier ((int) *cp))
82 while (isIdentifier ((int) *cp))
84 vStringPut (name, (int) *cp);
85 ++cp;
87 vStringTerminate (name);
88 vStringStripTrailing (name);
89 while (isspace ((int) *cp))
90 ++cp;
91 if (*cp == '=')
93 if (vStringLength (scope) > 0)
94 makeSimpleScopedTag (name, ConfKinds, K_KEY,
95 "section", vStringValue(scope), NULL);
96 else
97 makeSimpleTag (name, ConfKinds, K_KEY);
99 vStringClear (name);
101 else if (isspace ((int) *cp))
102 possible = TRUE;
103 else
104 possible = FALSE;
106 if (*cp != '\0')
107 ++cp;
110 vStringDelete (name);
111 vStringDelete (scope);
114 extern parserDefinition* ConfParser (void)
116 static const char *const patterns [] = { "*.ini", "*.conf", NULL };
117 static const char *const extensions [] = { "conf", NULL };
118 parserDefinition* const def = parserNew ("Conf");
119 def->kinds = ConfKinds;
120 def->kindCount = KIND_COUNT (ConfKinds);
121 def->patterns = patterns;
122 def->extensions = extensions;
123 def->parser = findConfTags;
124 return def;
127 /* vi:set tabstop=8 shiftwidth=4: */