LCOV - code coverage report
Current view: directory - ogr/ogrsf_frmts/geojson/jsonc - printbuf.c (source / functions) Found Hit Coverage
Test: gdal_filtered.info Lines: 39 34 87.2 %
Date: 2012-12-26 Functions: 5 5 100.0 %

       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                 : #include "config.h"
      17                 : 
      18                 : #if defined(__GNUC__) && !defined(_GNU_SOURCE)
      19                 : #define _GNU_SOURCE 1 /* seems to be required to bring in vasprintf */
      20                 : #endif
      21                 : #include <stdio.h>
      22                 : #include <stdlib.h>
      23                 : #include <string.h>
      24                 : 
      25                 : #include "cpl_string.h" 
      26                 : 
      27                 : #if HAVE_STDARG_H
      28                 : # include <stdarg.h>
      29                 : #else /* !HAVE_STDARG_H */
      30                 : # error Not enough var arg support!
      31                 : #endif /* HAVE_STDARG_H */
      32                 : 
      33                 : #include "bits.h"
      34                 : #include "debug.h"
      35                 : #include "printbuf.h"
      36                 : 
      37             875 : struct printbuf* printbuf_new(void)
      38                 : {
      39                 :   struct printbuf *p;
      40                 : 
      41             875 :   p = (struct printbuf*)calloc(1, sizeof(struct printbuf));
      42             875 :   if(!p) return NULL;
      43             875 :   p->size = 32;
      44             875 :   p->bpos = 0;
      45             875 :   if((p->buf = (char*)malloc(p->size)) == NULL) {
      46               0 :     free(p);
      47               0 :     return NULL;
      48                 :   }
      49             875 :   return p;
      50                 : }
      51                 : 
      52                 : 
      53            3958 : int printbuf_memappend(struct printbuf *p, const char *buf, int size)
      54                 : {
      55                 :   char *t;
      56            3958 :   if(p->size - p->bpos <= size) {
      57             326 :     int new_size = json_max(p->size * 2, p->bpos + size + 8);
      58                 : #ifdef PRINTBUF_DEBUG
      59                 :     MC_DEBUG("printbuf_memappend: realloc "
      60                 :        "bpos=%d wrsize=%d old_size=%d new_size=%d\n",
      61                 :        p->bpos, size, p->size, new_size);
      62                 : #endif /* PRINTBUF_DEBUG */
      63             326 :     if((t = (char*)realloc(p->buf, new_size)) == NULL) return -1;
      64             326 :     p->size = new_size;
      65             326 :     p->buf = t;
      66                 :   }
      67            3958 :   memcpy(p->buf + p->bpos, buf, size);
      68            3958 :   p->bpos += size;
      69            3958 :   p->buf[p->bpos]= '\0';
      70            3958 :   return size;
      71                 : }
      72                 : 
      73                 : /* Use CPLVASPrintf for portability issues */
      74            2484 : int sprintbuf(struct printbuf *p, const char *msg, ...)
      75                 : {
      76                 :   va_list ap;
      77                 :   char *t;
      78                 :   int size, ret; 
      79                 : 
      80                 :   /* user stack buffer first */
      81            2484 :   va_start(ap, msg);
      82            2484 :   if((size = CPLVASPrintf(&t, msg, ap)) == -1) return -1; 
      83            2484 :   va_end(ap);
      84                 :   
      85            2484 :   if (strcmp(msg, "%f") == 0)
      86                 :   {
      87               0 :       char* pszComma = strchr(t, ',');
      88               0 :       if (pszComma)
      89               0 :           *pszComma = '.';
      90                 :   }
      91                 :   
      92            2484 :   ret = printbuf_memappend(p, t, size);
      93            2484 :   CPLFree(t);
      94            2484 :   return ret;
      95                 : }
      96                 : 
      97           18322 : void printbuf_reset(struct printbuf *p)
      98                 : {
      99           18322 :   p->buf[0] = '\0';
     100           18322 :   p->bpos = 0;
     101           18322 : }
     102                 : 
     103           20869 : void printbuf_free(struct printbuf *p)
     104                 : {
     105           20869 :   if(p) {
     106             875 :     free(p->buf);
     107             875 :     free(p);
     108                 :   }
     109           20869 : }

Generated by: LCOV version 1.7