validator.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * validator/validator.c
  3. *
  4. * Copyright (C) 2022 bzt (bztsrc@gitlab)
  5. *
  6. * Permission is hereby granted, free of charge, to any person
  7. * obtaining a copy of this software and associated documentation
  8. * files (the "Software"), to deal in the Software without
  9. * restriction, including without limitation the rights to use, copy,
  10. * modify, merge, publish, distribute, sublicense, and/or sell copies
  11. * of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be
  15. * included in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  21. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  22. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  24. * DEALINGS IN THE SOFTWARE.
  25. *
  26. * @brief a web based Model 3D validator
  27. * https://gitlab.com/bztsrc/model3d
  28. *
  29. */
  30. #include <stdint.h>
  31. #include <stdarg.h>
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #include <emscripten.h>
  35. int verbose = 0;
  36. char *outbuf = NULL;
  37. void myprintf(char *fmt, ...);
  38. void myerrprintf(void *f, char *fmt, ...);
  39. #define M3D_IMPLEMENTATION
  40. #define M3D_ASCII
  41. #define M3D_LOG(x) do{if(verbose>1)myprintf(" %s\n",x);}while(0)
  42. #define M3D_VERTEXTYPE
  43. #define M3D_VERTEXMAX
  44. #include "../m3d.h"
  45. #define dumplog myprintf
  46. #define dumperr myerrprintf
  47. #include "../m3dconv/dump.h"
  48. /**
  49. * Redirect output to a html element
  50. */
  51. void myprintf(char *fmt, ...)
  52. {
  53. va_list args;
  54. va_start(args, fmt);
  55. vsnprintf(outbuf, BUFSIZ, fmt, args);
  56. EM_ASM({ output($0); }, strlen(outbuf) );
  57. }
  58. void myerrprintf(void *f, char *fmt, ...)
  59. {
  60. (void)f;
  61. va_list args;
  62. va_start(args, fmt);
  63. vsnprintf(outbuf, BUFSIZ, fmt, args);
  64. EM_ASM({ output($0); }, strlen(outbuf) );
  65. }
  66. /**
  67. * Function to run validator
  68. */
  69. void m3d_validate(unsigned char *buff, unsigned int size, int level, int ver, char *out)
  70. {
  71. m3d_t *m3d = NULL;
  72. outbuf = out;
  73. verbose = ver;
  74. if(level == 99) {
  75. if(verbose) myprintf("Parsing model\n");
  76. m3d = m3d_load(buff, NULL, NULL, NULL);
  77. if(verbose) myprintf("\n");
  78. if(m3d) {
  79. dump_cstruct(m3d);
  80. m3d_free(m3d);
  81. } else
  82. myprintf("ERROR: unable to parse as a Model 3D file\n");
  83. } else {
  84. dump_file(buff, size, level);
  85. }
  86. /* let the JS know that there's no more output, so that it can re-enable the enter button */
  87. myprintf("\003");
  88. }