mptStringBuffer.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * mptStringBuffer.cpp
  3. * -------------------
  4. * Purpose: Various functions for "fixing" char array strings for writing to or
  5. * reading from module files, or for securing char arrays in general.
  6. * Notes : (currently none)
  7. * Authors: OpenMPT Devs
  8. * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
  9. */
  10. #include "stdafx.h"
  11. #include "mptStringBuffer.h"
  12. OPENMPT_NAMESPACE_BEGIN
  13. namespace mpt
  14. {
  15. namespace String
  16. {
  17. namespace detail
  18. {
  19. std::string ReadStringBuffer(String::ReadWriteMode mode, const char *srcBuffer, std::size_t srcSize)
  20. {
  21. std::string dest;
  22. const char *src = srcBuffer;
  23. if(mode == nullTerminated || mode == spacePaddedNull)
  24. {
  25. // We assume that the last character of the source buffer is null.
  26. if(srcSize > 0)
  27. {
  28. srcSize -= 1;
  29. }
  30. }
  31. if(mode == nullTerminated || mode == maybeNullTerminated)
  32. {
  33. // Copy null-terminated string, stopping at null.
  34. dest.assign(src, std::find(src, src + srcSize, '\0'));
  35. } else if(mode == spacePadded || mode == spacePaddedNull)
  36. {
  37. // Copy string over.
  38. dest.assign(src, src + srcSize);
  39. // Convert null characters to spaces.
  40. std::transform(dest.begin(), dest.end(), dest.begin(), [] (char c) -> char { return (c != '\0') ? c : ' '; });
  41. // Trim trailing spaces.
  42. dest = mpt::trim_right(dest, std::string(" "));
  43. }
  44. return dest;
  45. }
  46. void WriteStringBuffer(String::ReadWriteMode mode, char *destBuffer, const std::size_t destSize, const char *srcBuffer, const std::size_t srcSize)
  47. {
  48. MPT_ASSERT(destSize > 0);
  49. const size_t maxSize = std::min(destSize, srcSize);
  50. char *dst = destBuffer;
  51. const char *src = srcBuffer;
  52. // First, copy over null-terminated string.
  53. size_t pos = maxSize;
  54. while(pos > 0)
  55. {
  56. if((*dst = *src) == '\0')
  57. {
  58. break;
  59. }
  60. pos--;
  61. dst++;
  62. src++;
  63. }
  64. if(mode == nullTerminated || mode == maybeNullTerminated)
  65. {
  66. // Fill rest of string with nulls.
  67. std::fill(dst, dst + destSize - maxSize + pos, '\0');
  68. } else if(mode == spacePadded || mode == spacePaddedNull)
  69. {
  70. // Fill the rest of the destination string with spaces.
  71. std::fill(dst, dst + destSize - maxSize + pos, ' ');
  72. }
  73. if(mode == nullTerminated || mode == spacePaddedNull)
  74. {
  75. // Make sure that destination is really null-terminated.
  76. SetNullTerminator(destBuffer, destSize);
  77. }
  78. }
  79. } // namespace detail
  80. } // namespace String
  81. } // namespace mpt
  82. OPENMPT_NAMESPACE_END