PageRenderTime 31ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/js/lib/Socket.IO-node/support/expresso/deps/jscoverage/stream.c

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
C | 129 lines | 90 code | 20 blank | 19 comment | 16 complexity | 8a73ed02e0840267b565e1dec0446ec3 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  1. /*
  2. stream.c - `Stream' object
  3. Copyright (C) 2008 siliconforks.com
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #define _GNU_SOURCE
  17. #include <config.h>
  18. #include "stream.h"
  19. #include <stdarg.h>
  20. #include <stdint.h>
  21. #include <string.h>
  22. #include "util.h"
  23. Stream * Stream_new(size_t capacity) {
  24. Stream * result = xmalloc(sizeof(Stream));
  25. result->length = 0;
  26. if (capacity == 0) {
  27. capacity = 8192;
  28. }
  29. result->capacity = capacity;
  30. result->data = xmalloc(capacity);
  31. return result;
  32. }
  33. void Stream_write(Stream * stream, const void * p, size_t size) {
  34. size_t stream_length = stream->length;
  35. size_t stream_capacity = stream->capacity;
  36. if (stream_capacity - stream_length < size) {
  37. if (SIZE_MAX - size < stream_length) {
  38. fatal("out of memory");
  39. }
  40. if (SIZE_MAX / 2 < stream_capacity) {
  41. stream_capacity = SIZE_MAX;
  42. }
  43. else {
  44. stream_capacity *= 2;
  45. }
  46. size_t new_length = stream_length + size;
  47. if (stream_capacity < new_length) {
  48. stream_capacity = new_length;
  49. }
  50. stream->data = xrealloc(stream->data, stream_capacity);
  51. stream->capacity = stream_capacity;
  52. memcpy(stream->data + stream_length, p, size);
  53. stream->length = new_length;
  54. }
  55. else {
  56. memcpy(stream->data + stream_length, p, size);
  57. stream->length = stream_length + size;
  58. }
  59. }
  60. void Stream_write_string(Stream * stream, const char * s) {
  61. Stream_write(stream, s, strlen(s));
  62. }
  63. void Stream_write_char(Stream * stream, char c) {
  64. size_t stream_length = stream->length;
  65. if (stream_length == stream->capacity) {
  66. if (stream->capacity == SIZE_MAX) {
  67. fatal("out of memory");
  68. }
  69. if (SIZE_MAX / 2 < stream->capacity) {
  70. stream->capacity = SIZE_MAX;
  71. }
  72. else {
  73. stream->capacity *= 2;
  74. }
  75. stream->data = xrealloc(stream->data, stream->capacity);
  76. }
  77. stream->data[stream_length] = c;
  78. stream->length = stream_length + 1;
  79. }
  80. void Stream_printf(Stream * stream, const char * format, ...) {
  81. va_list a;
  82. va_start(a, format);
  83. char * s = NULL;
  84. /* note that size does not include the NUL character */
  85. int size = vasprintf(&s, format, a);
  86. if (size < 0) {
  87. fatal("out of memory");
  88. }
  89. Stream_write(stream, s, size);
  90. free(s);
  91. va_end(a);
  92. }
  93. void Stream_write_file_contents(Stream * stream, FILE * f) {
  94. char buffer[8192];
  95. size_t bytes_read;
  96. while ((bytes_read = fread(buffer, 1, 8192, f)) > 0) {
  97. Stream_write(stream, buffer, bytes_read);
  98. }
  99. }
  100. void Stream_delete(Stream * stream) {
  101. free(stream->data);
  102. free(stream);
  103. }
  104. void Stream_reset(Stream * stream) {
  105. stream->length = 0;
  106. }