root/src/json-lib/printbuf.c

Revision 56cf4907b889bb4bebd61ffa8c91be1b2c6be2fa, 3.2 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 3 years ago)

make this stuff compile and run on sparcv8plus and sparcv9

  • Property mode set to 100644
Line 
1 /*
2  * $Id: printbuf.c,v 1.5 2006/01/26 02:16:28 mclark Exp $
3  *
4  * Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd.
5  * Michael Clark <michael@metaparadigm.com>
6  *
7  * This library is free software; you can redistribute it and/or modify
8  * it under the terms of the MIT license. See COPYING for details.
9  *
10  *
11  * Copyright (c) 2008-2009 Yahoo! Inc.  All rights reserved.
12  * The copyrights to the contents of this file are licensed under the MIT License
13  * (http://www.opensource.org/licenses/mit-license.php)
14  */
15
16 #define _GNU_SOURCE
17 #include "noit_config.h"
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22
23 #include <stdarg.h>
24
25 #include "bits.h"
26 #include "debug.h"
27 #include "printbuf.h"
28
29 struct printbuf* printbuf_new(void)
30 {
31   struct printbuf *p;
32
33   p = (struct printbuf*)calloc(1, sizeof(struct printbuf));
34   if(!p) return NULL;
35   p->size = 32;
36   p->bpos = 0;
37   if(!(p->buf = (char*)malloc(p->size))) {
38     free(p);
39     return NULL;
40   }
41   return p;
42 }
43
44
45 int printbuf_memappend(struct printbuf *p, const char *buf, int size)
46 {
47   char *t;
48   if(p->size - p->bpos <= size) {
49     int new_size = json_max(p->size * 2, p->bpos + size + 8);
50 #ifdef PRINTBUF_DEBUG
51     MC_DEBUG("printbuf_memappend: realloc "
52              "bpos=%d wrsize=%d old_size=%d new_size=%d\n",
53              p->bpos, size, p->size, new_size);
54 #endif /* PRINTBUF_DEBUG */
55     if(!(t = (char*)realloc(p->buf, new_size))) return -1;
56     p->size = new_size;
57     p->buf = t;
58   }
59   memcpy(p->buf + p->bpos, buf, size);
60   p->bpos += size;
61   p->buf[p->bpos]= '\0';
62   return size;
63 }
64
65 #ifndef HAVE_VASPRINTF
66 /* CAW: compliant version of vasprintf */
67 static int vasprintf(char **buf, const char *fmt, va_list ap)
68 {
69 #ifndef WIN32
70         static char _T_emptybuffer = '\0';
71 #endif /* !defined(WIN32) */
72         int chars;
73         char *b;
74
75         if(!buf) { return -1; }
76
77 #ifdef WIN32
78         chars = _vscprintf(fmt, ap)+1;
79 #else /* !defined(WIN32) */
80         /* CAW: RAWR! We have to hope to god here that vsnprintf doesn't overwrite
81            our buffer like on some 64bit sun systems.... but hey, its time to move on */
82         chars = vsnprintf(&_T_emptybuffer, 0, fmt, ap)+1;
83         if(chars < 0) { chars *= -1; } /* CAW: old glibc versions have this problem */
84 #endif /* defined(WIN32) */
85
86         b = (char*)malloc(sizeof(char)*chars);
87         if(!b) { return -1; }
88
89         if((chars = vsprintf(b, fmt, ap)) < 0)
90         {
91                 free(b);
92         } else {
93                 *buf = b;
94         }
95
96         return chars;
97 }
98 #endif /* !HAVE_VASPRINTF */
99
100 int sprintbuf(struct printbuf *p, const char *msg, ...)
101 {
102   va_list ap;
103   char *t;
104   int size;
105   char buf[128];
106
107   /* user stack buffer first */
108   va_start(ap, msg);
109   size = vsnprintf(buf, 128, msg, ap);
110   va_end(ap);
111   /* if string is greater than stack buffer, then use dynamic string
112      with vasprintf.  Note: some implementation of vsnprintf return -1
113      if output is truncated whereas some return the number of bytes that
114      would have been written - this code handles both cases. */
115   if(size == -1 || size > 127) {
116     va_start(ap, msg);
117     if((size = vasprintf(&t, msg, ap)) == -1) { va_end(ap); return -1; }
118     va_end(ap);
119     printbuf_memappend(p, t, size);
120     free(t);
121     return size;
122   } else {
123     printbuf_memappend(p, buf, size);
124     return size;
125   }
126 }
127
128 void printbuf_reset(struct printbuf *p)
129 {
130   p->buf[0] = '\0';
131   p->bpos = 0;
132 }
133
134 void printbuf_free(struct printbuf *p)
135 {
136   if(p) {
137     free(p->buf);
138     free(p);
139   }
140 }
Note: See TracBrowser for help on using the browser.