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.

convertlf.cxx 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /* Copyright 2019 Pierre Ossman <ossman@cendio.se> for Cendio AB
  2. *
  3. * This is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This software 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. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this software; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  16. * USA.
  17. */
  18. #ifdef HAVE_CONFIG_H
  19. #include <config.h>
  20. #endif
  21. #include <stdio.h>
  22. #include <rfb/util.h>
  23. static const char* escape(const char* input)
  24. {
  25. static char output[4096];
  26. const char* in;
  27. char* out;
  28. in = input;
  29. out = output;
  30. do {
  31. if (*in == '\r') {
  32. *out++ = '\\';
  33. *out++ = 'r';
  34. } else if (*in == '\n') {
  35. *out++ = '\\';
  36. *out++ = 'n';
  37. } else {
  38. *out++ = *in;
  39. }
  40. } while (*in++ != '\0');
  41. return output;
  42. }
  43. static void testLF(const char* input, const char* expected)
  44. {
  45. char* output;
  46. printf("convertLF(\"%s\"): ", escape(input));
  47. output = rfb::convertLF(input);
  48. if (strcmp(output, expected) != 0)
  49. printf("FAILED: got \"%s\"", escape(output));
  50. else
  51. printf("OK");
  52. printf("\n");
  53. fflush(stdout);
  54. rfb::strFree(output);
  55. }
  56. static void testCRLF(const char* input, const char* expected)
  57. {
  58. char* output;
  59. printf("convertCRLF(\"%s\"): ", escape(input));
  60. output = rfb::convertCRLF(input);
  61. if (strcmp(output, expected) != 0)
  62. printf("FAILED: got \"%s\"", escape(output));
  63. else
  64. printf("OK");
  65. printf("\n");
  66. fflush(stdout);
  67. rfb::strFree(output);
  68. }
  69. int main(int /*argc*/, char** /*argv*/)
  70. {
  71. testLF("", "");
  72. testLF("no EOL", "no EOL");
  73. testLF("\n", "\n");
  74. testLF("already correct\n", "already correct\n");
  75. testLF("multiple\nlines\n", "multiple\nlines\n");
  76. testLF("empty lines\n\n", "empty lines\n\n");
  77. testLF("\ninitial line", "\ninitial line");
  78. testLF("\r\n", "\n");
  79. testLF("one line\r\n", "one line\n");
  80. testLF("multiple\r\nlines\r\n", "multiple\nlines\n");
  81. testLF("empty lines\r\n\r\n", "empty lines\n\n");
  82. testLF("\r\ninitial line", "\ninitial line");
  83. testLF("mixed\r\nlines\n", "mixed\nlines\n");
  84. testLF("cropped\r", "cropped\n");
  85. testLF("old\rmac\rformat", "old\nmac\nformat");
  86. testCRLF("", "");
  87. testCRLF("no EOL", "no EOL");
  88. testCRLF("\r\n", "\r\n");
  89. testCRLF("already correct\r\n", "already correct\r\n");
  90. testCRLF("multiple\r\nlines\r\n", "multiple\r\nlines\r\n");
  91. testCRLF("empty lines\r\n\r\n", "empty lines\r\n\r\n");
  92. testCRLF("\r\ninitial line", "\r\ninitial line");
  93. testCRLF("\n", "\r\n");
  94. testCRLF("one line\n", "one line\r\n");
  95. testCRLF("multiple\nlines\n", "multiple\r\nlines\r\n");
  96. testCRLF("empty lines\n\n", "empty lines\r\n\r\n");
  97. testCRLF("\ninitial line", "\r\ninitial line");
  98. testCRLF("mixed\r\nlines\n", "mixed\r\nlines\r\n");
  99. testCRLF("cropped\r", "cropped\r\n");
  100. testCRLF("old\rmac\rformat", "old\r\nmac\r\nformat");
  101. return 0;
  102. }