raylib_npp_parser.c 5.9 KB

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