util.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // Copyright 2010 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //
  15. // Author: [email protected] (Jonathan Tang)
  16. #include "util.h"
  17. #include <assert.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <strings.h>
  21. #include <stdarg.h>
  22. #include <stdio.h>
  23. #include "gumbo.h"
  24. #include "parser.h"
  25. // TODO(jdtang): This should be elsewhere, but there's no .c file for
  26. // SourcePositions and yet the constant needs some linkage, so this is as good
  27. // as any.
  28. const GumboSourcePosition kGumboEmptySourcePosition = {0, 0, 0};
  29. void* gumbo_parser_allocate(GumboParser* parser, size_t num_bytes) {
  30. return parser->_options->allocator(parser->_options->userdata, num_bytes);
  31. }
  32. void gumbo_parser_deallocate(GumboParser* parser, void* ptr) {
  33. parser->_options->deallocator(parser->_options->userdata, ptr);
  34. }
  35. char* gumbo_copy_stringz(GumboParser* parser, const char* str) {
  36. char* buffer = gumbo_parser_allocate(parser, strlen(str) + 1);
  37. strcpy(buffer, str);
  38. return buffer;
  39. }
  40. // Debug function to trace operation of the parser. Pass --copts=-DGUMBO_DEBUG
  41. // to use.
  42. void gumbo_debug(const char* format, ...) {
  43. #ifdef GUMBO_DEBUG
  44. va_list args;
  45. va_start(args, format);
  46. vprintf(format, args);
  47. va_end(args);
  48. fflush(stdout);
  49. #endif
  50. }