Debian Codegraph @

projects/Notepad++/raylib_npp_parser/raylib_npp_parser.c

raw symbols SCIP
1 /**********************************************************************************************
2
3 raylib_npp_parser - raylib header parser to generate Notepad++ autocompletion data
4
5 This parser scans raylib.h for functions that start with RLAPI and generates Notepad++
6 autocompletion xml equivalent for function and parameters.
7
8 Converts:
9 RLAPI Color Fade(Color color, float alpha); // Color fade-in or fade-out, alpha goes from 0.0f to 1.0f
10
11 To:
12 <KeyWord name="Fade" func="yes">
13 <Overload retVal="Color" descr="Color fade-in or fade-out, alpha goes from 0.0 to 1.0">
14 <Param name="Color color" />
15 <Param name="float alpha" />
16 </Overload>
17 </KeyWord>
18
19 NOTE: Generated XML text should be copied inside raylib\Notepad++\plugins\APIs\c.xml
20
21 WARNING: Be careful with functions that split parameters into several lines, it breaks the process!
22
23 LICENSE: zlib/libpng
24
25 raylib is licensed under an unmodified zlib/libpng license, which is an OSI-certified,
26 BSD-like license that allows static linking with closed source software:
27
28 Copyright (c) 2018 Ramon Santamaria (@raysan5)
29
30 **********************************************************************************************/
31
32 #include <stdio.h>
33 #include <string.h>
34 #include <stdbool.h>
35 #include <stdlib.h>
36
37 #define MAX_BUFFER_SIZE 512
38
39 int main(int argc, char *argv[])
40 {
41 if (argc > 1)
42 {
43 FILE *rFile = fopen(argv[1], "rt");
44 FILE *rxmlFile = fopen("raylib_npp.xml", "wt");
45
46 if ((rFile == NULL) || (rxmlFile == NULL))
47 {
48 printf("File could not be opened.\n");
49 return 0;
50 }
51
52 char *buffer = (char *)calloc(MAX_BUFFER_SIZE, 1);
53 int count = 0;
54
55 while (!feof(rFile))
56 {
57 // Read one full line
58 fgets(buffer, MAX_BUFFER_SIZE, rFile);
59
60 if (buffer[0] == '/') fprintf(rxmlFile, " <!--%.*s -->\n", strlen(buffer) - 3, buffer + 2);
61 else if (buffer[0] == '\n') fprintf(rxmlFile, "%s", buffer); // Direct copy of code comments
62 else if (strncmp(buffer, "RLAPI", 5) == 0) // raylib function declaration
63 {
64 char funcType[64];
65 char funcTypeAux[64];
66 char funcName[64];
67 char funcDesc[256];
68
69 char params[128];
70 char paramType[16][16];
71 char paramName[16][32];
72
73 int index = 0;
74 char *ptr = NULL;
75
76 sscanf(buffer, "RLAPI %s %[^(]s", funcType, funcName);
77
78 if (strcmp(funcType, "const") == 0)
79 {
80 sscanf(buffer, "RLAPI %s %s %[^(]s", funcType, funcTypeAux, funcName);
81 strcat(funcType, " ");
82 strcat(funcType, funcTypeAux);
83 }
84
85 ptr = strchr(buffer, '/');
86 index = (int)(ptr - buffer);
87
88 sscanf(buffer + index, "%[^\n]s", funcDesc); // Read function comment after declaration
89
90 ptr = strchr(buffer, '(');
91
92 if (ptr != NULL) index = (int)(ptr - buffer);
93 else printf("Character not found!\n");
94
95 sscanf(buffer + (index + 1), "%[^)]s", params); // Read what's inside '(' and ')'
96
97 // Scan params string for number of func params, type and name
98 char *paramPtr[16]; // Allocate 16 pointers for possible parameters
99 int paramsCount = 0;
100 paramPtr[paramsCount] = strtok(params, ",");
101
102 if ((funcName[0] == '*') && (funcName[1] == '*')) fprintf(rxmlFile, " <KeyWord name=\"%s\" func=\"yes\">\n", funcName + 2);
103 else if (funcName[0] == '*') fprintf(rxmlFile, " <KeyWord name=\"%s\" func=\"yes\">\n", funcName + 1);
104 else fprintf(rxmlFile, " <KeyWord name=\"%s\" func=\"yes\">\n", funcName);
105
106 fprintf(rxmlFile, " <Overload retVal=\"%s\" descr=\"%s\">", funcType, funcDesc + 3);
107
108 bool paramsVoid = false;
109
110 char paramConst[8][16];
111
112 while (paramPtr[paramsCount] != NULL)
113 {
114 sscanf(paramPtr[paramsCount], "%s %s\n", paramType[paramsCount], paramName[paramsCount]);
115
116 if (strcmp(paramType[paramsCount], "void") == 0)
117 {
118 paramsVoid = true;
119 break;
120 }
121
122 if ((strcmp(paramType[paramsCount], "const") == 0) || (strcmp(paramType[paramsCount], "unsigned") == 0))
123 {
124 sscanf(paramPtr[paramsCount], "%s %s %s\n", paramConst[paramsCount], paramType[paramsCount], paramName[paramsCount]);
125 fprintf(rxmlFile, "\n <Param name=\"%s %s %s\" />", paramConst[paramsCount], paramType[paramsCount], paramName[paramsCount]);
126 }
127 else if (strcmp(paramType[paramsCount], "...") == 0) fprintf(rxmlFile, "\n <Param name=\"...\" />");
128 else fprintf(rxmlFile, "\n <Param name=\"%s %s\" />", paramType[paramsCount], paramName[paramsCount]);
129
130 paramsCount++;
131 paramPtr[paramsCount] = strtok(NULL, ",");
132 }
133
134 fprintf(rxmlFile, "%s</Overload>\n", paramsVoid ? "" : "\n ");
135 fprintf(rxmlFile, " </KeyWord>\n");
136
137 count++;
138 printf("Function processed %02i: %s\n", count, funcName);
139
140 memset(buffer, 0, MAX_BUFFER_SIZE);
141 }
142 }
143
144 free(buffer);
145 fclose(rFile);
146 fclose(rxmlFile);
147 }
148
149 return 0;
150 }