rwhelper.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /* See LICENSE.txt for the full license governing this code. */
  2. /**
  3. * \file rwhelper.c
  4. *
  5. * Source file with some helper functions for working with SDL_RWops.
  6. */
  7. #include <SDL_test.h>
  8. #include "SDL_visualtest_sut_configparser.h"
  9. #include "SDL_visualtest_rwhelper.h"
  10. void
  11. SDLVisualTest_RWHelperResetBuffer(SDLVisualTest_RWHelperBuffer* buffer)
  12. {
  13. if(!buffer)
  14. {
  15. SDLTest_LogError("buffer argument cannot be NULL");
  16. return;
  17. }
  18. buffer->buffer_pos = 0;
  19. buffer->buffer_width = 0;
  20. }
  21. char
  22. SDLVisualTest_RWHelperReadChar(SDL_RWops* rw, SDLVisualTest_RWHelperBuffer* buffer)
  23. {
  24. if(!rw || !buffer)
  25. return 0;
  26. /* if the buffer has been consumed, we fill it up again */
  27. if(buffer->buffer_pos == buffer->buffer_width)
  28. {
  29. buffer->buffer_width = SDL_RWread(rw, buffer->buffer, 1, RWOPS_BUFFER_LEN);
  30. buffer->buffer_pos = 0;
  31. if(buffer->buffer_width == 0)
  32. return 0;
  33. }
  34. buffer->buffer_pos++;
  35. return buffer->buffer[buffer->buffer_pos - 1];
  36. }
  37. /* does not include new lines in the buffer and adds a trailing null character */
  38. char*
  39. SDLVisualTest_RWHelperReadLine(SDL_RWops* rw, char* str, int size,
  40. SDLVisualTest_RWHelperBuffer* buffer,
  41. char comment_char)
  42. {
  43. char ch;
  44. int current_pos, done;
  45. if(!rw)
  46. {
  47. SDLTest_LogError("rw argument cannot be NULL");
  48. return NULL;
  49. }
  50. if(!str)
  51. {
  52. SDLTest_LogError("str argument cannot be NULL");
  53. return NULL;
  54. }
  55. if(!buffer)
  56. {
  57. SDLTest_LogError("buffer argument cannot be NULL");
  58. return NULL;
  59. }
  60. if(size <= 0)
  61. {
  62. SDLTest_LogError("size argument should be positive");
  63. return NULL;
  64. }
  65. done = 0;
  66. while(!done)
  67. {
  68. /* ignore leading whitespace */
  69. for(ch = SDLVisualTest_RWHelperReadChar(rw, buffer); ch && SDL_isspace(ch);
  70. ch = SDLVisualTest_RWHelperReadChar(rw, buffer));
  71. for(current_pos = 0;
  72. ch && ch != '\n' && ch != '\r' && ch != comment_char;
  73. current_pos++)
  74. {
  75. str[current_pos] = ch;
  76. if(current_pos >= size - 2)
  77. {
  78. current_pos++;
  79. break;
  80. }
  81. ch = SDLVisualTest_RWHelperReadChar(rw, buffer);
  82. }
  83. done = 1;
  84. if(ch == comment_char) /* discard all characters until the next line */
  85. {
  86. do
  87. {
  88. ch = SDLVisualTest_RWHelperReadChar(rw, buffer);
  89. }while(ch && ch != '\n' && ch != '\r');
  90. if(current_pos == 0)
  91. done = 0;
  92. }
  93. }
  94. if(current_pos == 0)
  95. return NULL;
  96. str[current_pos] = '\0';
  97. return str;
  98. }
  99. /* Lines with all whitespace are ignored */
  100. int
  101. SDLVisualTest_RWHelperCountNonEmptyLines(SDL_RWops* rw,
  102. SDLVisualTest_RWHelperBuffer* buffer,
  103. char comment_char)
  104. {
  105. int num_lines = 0;
  106. char str[MAX_SUTOPTION_LINE_LENGTH];
  107. if(!rw)
  108. {
  109. SDLTest_LogError("rw argument cannot be NULL");
  110. return -1;
  111. }
  112. if(!buffer)
  113. {
  114. SDLTest_LogError("buffer argument cannot be NULL");
  115. return -1;
  116. }
  117. while(SDLVisualTest_RWHelperReadLine(rw, str, MAX_SUTOPTION_LINE_LENGTH,
  118. buffer, comment_char))
  119. num_lines++;
  120. return num_lines;
  121. }