ctorrent stat collector
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

154 lines
3.8KB

  1. /***************************************************************************
  2. * Copyright (C) 07/2007 by Olaf Rempel *
  3. * razzor@kopf-tisch.de *
  4. * *
  5. * 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 *
  7. * the Free Software Foundation; version 2 of the License *
  8. * *
  9. * This program is distributed in the hope that it will be useful, *
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  12. * GNU General Public License for more details. *
  13. * *
  14. * You should have received a copy of the GNU General Public License *
  15. * along with this program; if not, write to the *
  16. * Free Software Foundation, Inc., *
  17. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  18. ***************************************************************************/
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <unistd.h>
  22. #include <string.h>
  23. #include <stdarg.h>
  24. #include "linebuffer.h"
  25. #define MIN(a,b) (((a) < (b)) ? (a) : (b))
  26. struct linebuffer * create_linebuffer(int size)
  27. {
  28. struct linebuffer *buf = malloc(sizeof(struct linebuffer));
  29. if (buf == NULL)
  30. return NULL;
  31. buf->size = size;
  32. buf->data = malloc(buf->size);
  33. if (buf->data == NULL) {
  34. free(buf);
  35. return NULL;
  36. }
  37. buf->pos = 0;
  38. return buf;
  39. }
  40. void linebuffer_free(struct linebuffer *buf)
  41. {
  42. free(buf->data);
  43. free(buf);
  44. }
  45. int linebuffer_clear(struct linebuffer *buf)
  46. {
  47. int oldpos = buf->pos;
  48. buf->pos = 0;
  49. return oldpos;
  50. }
  51. int linebuffer_put(struct linebuffer *buf, const char *src, unsigned int size)
  52. {
  53. int len = MIN(buf->size - buf->pos, size);
  54. memcpy(buf->data + buf->pos, src, len);
  55. buf->pos += len;
  56. return len;
  57. }
  58. int linebuffer_readfd(struct linebuffer *buf, int fd)
  59. {
  60. int len = read(fd, buf->data + buf->pos, buf->size - buf->pos);
  61. if (len <= 0)
  62. return -1;
  63. buf->pos += len;
  64. return len;
  65. }
  66. int linebuffer_parsefd(struct linebuffer *buf, int fd)
  67. {
  68. char tmp[32];
  69. int len = read(fd, tmp, sizeof(tmp));
  70. if (len <= 0)
  71. return -1;
  72. int i;
  73. for (i = 0; i < len; i++) {
  74. /* "understand" backspace */
  75. if (tmp[i] == 0x08 && buf->pos > 0) {
  76. buf->pos--;
  77. /* copy */
  78. } else if (tmp[i] >= ' ' || tmp[i] == '\n') {
  79. *(buf->data + buf->pos++) = tmp[i];
  80. }
  81. if (buf->pos > buf->size)
  82. return -1;
  83. }
  84. return 0;
  85. }
  86. int linebuffer_writefd(struct linebuffer *buf, int fd)
  87. {
  88. int len = write(fd, buf->data, buf->pos);
  89. if (len <= 0)
  90. return -1;
  91. /* TODO: now assuming everything is written */
  92. buf->pos = 0;
  93. return len;
  94. }
  95. int linebuffer_vprintf(struct linebuffer *buf, const char *fmt, va_list az)
  96. {
  97. int len = vsnprintf(buf->data + buf->pos, buf->size - buf->pos, fmt, az);
  98. if (len < 0 || len >= (buf->size - buf->pos))
  99. return -1;
  100. buf->pos += len;
  101. return len;
  102. }
  103. int linebuffer_printf(struct linebuffer *buf, const char *fmt, ...)
  104. {
  105. va_list az;
  106. va_start(az, fmt);
  107. int ret = linebuffer_vprintf(buf, fmt, az);
  108. va_end(az);
  109. return ret;
  110. }
  111. char * linebuffer_getline(struct linebuffer *buf, int *len)
  112. {
  113. buf->newline = memchr(buf->data, '\n', buf->pos);
  114. if (buf->newline == NULL)
  115. return NULL;
  116. *(buf->newline) = '\0';
  117. if (len != NULL)
  118. *len = buf->newline - buf->data;
  119. return buf->data;
  120. }
  121. int linebuffer_freeline(struct linebuffer *buf)
  122. {
  123. buf->pos -= (buf->newline - buf->data) +1;
  124. memmove(buf->data, buf->newline +1, buf->pos);
  125. buf->newline = NULL;
  126. return buf->pos;
  127. }