y_mock.inc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #if defined _INC_y_mock
  2. #endinput
  3. #endif
  4. #define _INC_y_mock
  5. #include "..\YSI_Coding\y_hooks"
  6. static stock
  7. YSI_g_sMockFile[2024];
  8. hook OnScriptInit()
  9. {
  10. // Add a file.
  11. strcat(YSI_g_sMockFile, "YSI/test_ini/test1.ini\1" \
  12. "key0 = 42" "\r\n" \
  13. "key1 = 43" "\r\n" \
  14. "key2 = 45" "\r\n" \
  15. "key4 = 46" "\r\n" \
  16. "key3 = 47" "\r\n" \
  17. "" "\r\n" \
  18. "[atag]" "\r\n" \
  19. "" "\r\n" \
  20. "key0 = hello" "\r\n" \
  21. "otherKey = 5.5" "\r\n" \
  22. "myKey = false" "\r\n" \
  23. "" "\r\n" \
  24. "[@@filter-tag2]" "\r\n" \
  25. "" "\r\n" \
  26. "filtered1 = yes" "\r\n" \
  27. "filtered2 = no" "\r\n" \
  28. "\r\n\2");
  29. // Add a file.
  30. strcat(YSI_g_sMockFile, "YSI/test_ini/test2.ini\1" \
  31. "[tag0]" "\r\n" \
  32. "key0 = 101" "\r\n" \
  33. "key1 = 102" "\r\n" \
  34. "" "\r\n" \
  35. "[tag1] : tag0" "\r\n" \
  36. "" "\r\n" \
  37. "key2 = 103" "\r\n" \
  38. "key3 = 104" "\r\n" \
  39. "" "\r\n" \
  40. "[tag2] : tag1" "\r\n" \
  41. "" "\r\n" \
  42. "key4 = 105" "\r\n" \
  43. "key5 = 106" "\r\n" \
  44. "; Overwrite" "\r\n" \
  45. "key0 = 107" "\r\n" \
  46. "\r\n\2");
  47. }
  48. static stock Mock_Compare(const name[], s, e)
  49. {
  50. // Compare the file names, allowing "/" and "\" to match.
  51. new
  52. i = 0;
  53. while (s != e)
  54. {
  55. switch (name[i])
  56. {
  57. case '\0': return false;
  58. case '/', '\\': if (YSI_g_sMockFile[s] != '\\' && YSI_g_sMockFile[s] != '/') return false;
  59. default: if (YSI_g_sMockFile[s] != name[i]) return false;
  60. }
  61. ++i;
  62. ++s;
  63. }
  64. return name[i] == '\0';
  65. }
  66. stock File:Mock_fopen(const name[], filemode:mode = io_readwrite)
  67. {
  68. // This function is not well optimised, but it doesn't need to be. It only
  69. // provides a common and constant interface with which we can profile other
  70. // functions that read files.
  71. assert(mode == io_read);
  72. // Find the named file.
  73. new
  74. pos = 0,
  75. end;
  76. do
  77. {
  78. end = strfind(YSI_g_sMockFile, "\1", false, pos);
  79. //printf("Trying: %s %d %d", name, pos, end);
  80. if (Mock_Compare(name, pos, end)) return File:(end + 1);
  81. pos = strfind(YSI_g_sMockFile, "\2", false, pos) + 1;
  82. }
  83. while (YSI_g_sMockFile[pos]);
  84. return File:0;
  85. }
  86. stock Mock_fread(&File:handle, dest[], len = sizeof (dest))
  87. {
  88. if (YSI_g_sMockFile[_:handle] == '\2') return 0;
  89. new
  90. ret = strfind(YSI_g_sMockFile[_:handle], "\r\n") + 2;
  91. strcpy(dest, YSI_g_sMockFile[_:handle], min(len, ret + 1));
  92. handle += File:ret;
  93. return ret;
  94. }
  95. stock Mock_fseek(&File:handle, position = 0, seek_whence:whence = seek_start)
  96. {
  97. switch (whence)
  98. {
  99. case seek_start:
  100. {
  101. // Loop backwards.
  102. for (new i = _:handle; --i; )
  103. {
  104. if (YSI_g_sMockFile[i] == '\1')
  105. {
  106. handle = File:(position + i + 1);
  107. return 1;
  108. }
  109. }
  110. }
  111. case seek_current:
  112. {
  113. handle += File:position;
  114. return 1;
  115. }
  116. case seek_end:
  117. {
  118. handle = File:(strfind(YSI_g_sMockFile, "\2", false, _:handle) - position);
  119. return 1;
  120. }
  121. }
  122. return 0;
  123. }
  124. stock Mock_fclose(&File:handle)
  125. {
  126. handle = File:0;
  127. return 0;
  128. }
  129. #if defined YSI_MOCK_READER
  130. #if defined _ALS_fopen
  131. #undef fopen
  132. #else
  133. #define _ALS_fopen
  134. #endif
  135. #define fopen Mock_fopen
  136. #if defined _ALS_fread
  137. #undef fread
  138. #else
  139. #define _ALS_fread
  140. #endif
  141. #define fread Mock_fread
  142. #if defined _ALS_fseek
  143. #undef fseek
  144. #else
  145. #define _ALS_fseek
  146. #endif
  147. #define fseek Mock_fseek
  148. #if defined _ALS_fclose
  149. #undef fclose
  150. #else
  151. #define _ALS_fclose
  152. #endif
  153. #define fclose Mock_fclose
  154. #endif