Make parser includes closer to uctags and sync parser license header
[geany-mirror.git] / ctags / parsers / diff.c
blob0e820239e2a62cf465f2972e86013aa27dd77ffb
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 version 2 or (at your option) any later version.
8 * This module contains functions for generating tags for diff files (based on Sh parser).
9 */
12 * INCLUDE FILES
14 #include "general.h" /* must always come first */
16 #include <ctype.h>
17 #include <string.h>
19 #include "parse.h"
20 #include "routines.h"
21 #include "read.h"
22 #include "vstring.h"
25 * DATA DEFINITIONS
27 typedef enum {
28 K_FUNCTION
29 } diffKind;
31 static kindOption DiffKinds [] = {
32 { TRUE, 'f', "function", "functions"}
35 enum {
36 DIFF_DELIM_MINUS = 0,
37 DIFF_DELIM_PLUS
40 static const char *DiffDelims[2] = {
41 "--- ",
42 "+++ "
46 * FUNCTION DEFINITIONS
49 static const unsigned char *stripAbsolute (const unsigned char *filename)
51 const unsigned char *tmp;
53 /* strip any absolute path */
54 if (*filename == '/' || *filename == '\\')
56 boolean skipSlash = TRUE;
58 tmp = (const unsigned char*) strrchr ((const char*) filename, '/');
59 if (tmp == NULL)
60 { /* if no / is contained try \ in case of a Windows filename */
61 tmp = (const unsigned char*) strrchr ((const char*) filename, '\\');
62 if (tmp == NULL)
63 { /* last fallback, probably the filename doesn't contain a path, so take it */
64 tmp = filename;
65 skipSlash = FALSE;
69 /* skip the leading slash or backslash */
70 if (skipSlash)
71 tmp++;
73 else
74 tmp = filename;
76 return tmp;
79 static void findDiffTags (void)
81 vString *filename = vStringNew ();
82 const unsigned char *line, *tmp;
83 int delim = DIFF_DELIM_MINUS;
85 while ((line = fileReadLine ()) != NULL)
87 const unsigned char* cp = line;
89 if (strncmp ((const char*) cp, DiffDelims[delim], 4u) == 0)
91 cp += 4;
92 if (isspace ((int) *cp)) continue;
93 /* when original filename is /dev/null use the new one instead */
94 if (delim == DIFF_DELIM_MINUS &&
95 strncmp ((const char*) cp, "/dev/null", 9u) == 0 &&
96 (cp[9] == 0 || isspace (cp[9])))
98 delim = DIFF_DELIM_PLUS;
99 continue;
102 tmp = stripAbsolute (cp);
104 if (tmp != NULL)
106 while (! isspace(*tmp) && *tmp != '\0')
108 vStringPut(filename, *tmp);
109 tmp++;
112 vStringTerminate(filename);
113 makeSimpleTag (filename, DiffKinds, K_FUNCTION);
114 vStringClear (filename);
117 /* restore default delim */
118 delim = DIFF_DELIM_MINUS;
121 vStringDelete (filename);
124 extern parserDefinition* DiffParser (void)
126 static const char *const patterns [] = { "*.diff", "*.patch", NULL };
127 static const char *const extensions [] = { "diff", NULL };
128 parserDefinition* const def = parserNew ("Diff");
129 def->kinds = DiffKinds;
130 def->kindCount = KIND_COUNT (DiffKinds);
131 def->patterns = patterns;
132 def->extensions = extensions;
133 def->parser = findDiffTags;
134 return def;
137 /* vi:set tabstop=8 shiftwidth=4: */