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: jdtang@google.com (Jonathan Tang)
16 
17 #include "util.h"
18 
19 #include <assert.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <strings.h>
23 #include <stdarg.h>
24 #include <stdio.h>
25 
26 #include "gumbo.h"
27 #include "parser.h"
28 
29 // TODO(jdtang): This should be elsewhere, but there's no .c file for
30 // SourcePositions and yet the constant needs some linkage, so this is as good
31 // as any.
32 const GumboSourcePosition kGumboEmptySourcePosition = {0, 0, 0};
33 
gumbo_parser_allocate(GumboParser * parser,size_t num_bytes)34 void* gumbo_parser_allocate(GumboParser* parser, size_t num_bytes) {
35   return parser->_options->allocator(parser->_options->userdata, num_bytes);
36 }
37 
gumbo_parser_deallocate(GumboParser * parser,void * ptr)38 void gumbo_parser_deallocate(GumboParser* parser, void* ptr) {
39   parser->_options->deallocator(parser->_options->userdata, ptr);
40 }
41 
gumbo_copy_stringz(GumboParser * parser,const char * str)42 char* gumbo_copy_stringz(GumboParser* parser, const char* str) {
43   char* buffer = gumbo_parser_allocate(parser, strlen(str) + 1);
44   strcpy(buffer, str);
45   return buffer;
46 }
47 
48 // Debug function to trace operation of the parser.  Pass --copts=-DGUMBO_DEBUG
49 // to use.
gumbo_debug(const char * format,...)50 void gumbo_debug(const char* format, ...) {
51 #ifdef GUMBO_DEBUG
52   va_list args;
53   va_start(args, format);
54   vprintf(format, args);
55   va_end(args);
56   fflush(stdout);
57 #endif
58 }
59