/[jscoverage]/trunk/stream.c
ViewVC logotype

Diff of /trunk/stream.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 100 by siliconforks, Wed May 21 23:50:08 2008 UTC revision 505 by siliconforks, Sat Jan 2 04:55:54 2010 UTC
# Line 1  Line 1 
1  /*  /*
2      stream.c - `Stream' object      stream.c - `Stream' object
3      Copyright (C) 2008 siliconforks.com      Copyright (C) 2008, 2009, 2010 siliconforks.com
4    
5      This program is free software; you can redistribute it and/or modify      This program is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published by      it under the terms of the GNU General Public License as published by
# Line 19  Line 19 
19    
20  #define _GNU_SOURCE  #define _GNU_SOURCE
21    
22    #include <config.h>
23    
24  #include "stream.h"  #include "stream.h"
25    
26  #include <stdarg.h>  #include <stdarg.h>
# Line 39  Line 41 
41  }  }
42    
43  void Stream_write(Stream * stream, const void * p, size_t size) {  void Stream_write(Stream * stream, const void * p, size_t size) {
44    if (SIZE_MAX - size < stream->length) {    size_t stream_length = stream->length;
45      fatal("out of memory");    size_t stream_capacity = stream->capacity;
46    }    if (stream_capacity - stream_length < size) {
47        if (SIZE_MAX - size < stream_length) {
48          fatal("out of memory");
49        }
50    
51    size_t new_length = stream->length + size;      if (SIZE_MAX / 2 < stream_capacity) {
52    size_t new_capacity = stream->capacity;        stream_capacity = SIZE_MAX;
   if (new_capacity < new_length) {  
     if (SIZE_MAX / 2 < new_capacity) {  
       new_capacity = SIZE_MAX;  
53      }      }
54      else {      else {
55        new_capacity *= 2;        stream_capacity *= 2;
56      }      }
57    
58      if (new_capacity < new_length) {      size_t new_length = stream_length + size;
59        new_capacity = new_length;      if (stream_capacity < new_length) {
60          stream_capacity = new_length;
61      }      }
   }  
62    
63    if (new_capacity != stream->capacity) {      stream->data = xrealloc(stream->data, stream_capacity);
64      stream->data = xrealloc(stream->data, new_capacity);      stream->capacity = stream_capacity;
65      stream->capacity = new_capacity;      memcpy(stream->data + stream_length, p, size);
66        stream->length = new_length;
67      }
68      else {
69        memcpy(stream->data + stream_length, p, size);
70        stream->length = stream_length + size;
71    }    }
   
   memcpy(stream->data + stream->length, p, size);  
   stream->length = new_length;  
72  }  }
73    
74  void Stream_write_string(Stream * stream, const char * s) {  void Stream_write_string(Stream * stream, const char * s) {
# Line 72  Line 76 
76  }  }
77    
78  void Stream_write_char(Stream * stream, char c) {  void Stream_write_char(Stream * stream, char c) {
79    Stream_write(stream, &c, 1);    size_t stream_length = stream->length;
80      if (stream_length == stream->capacity) {
81        if (stream->capacity == SIZE_MAX) {
82          fatal("out of memory");
83        }
84    
85        if (SIZE_MAX / 2 < stream->capacity) {
86          stream->capacity = SIZE_MAX;
87        }
88        else {
89          stream->capacity *= 2;
90        }
91    
92        stream->data = xrealloc(stream->data, stream->capacity);
93      }
94      stream->data[stream_length] = c;
95      stream->length = stream_length + 1;
96  }  }
97    
98  void Stream_printf(Stream * stream, const char * format, ...) {  void Stream_printf(Stream * stream, const char * format, ...) {
# Line 103  Line 123 
123    free(stream->data);    free(stream->data);
124    free(stream);    free(stream);
125  }  }
126    
127    void Stream_reset(Stream * stream) {
128      stream->length = 0;
129    }

Legend:
Removed from v.100  
changed lines
  Added in v.505

  ViewVC Help
Powered by ViewVC 1.1.24