macros.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. This software is subject to the license described in the License.txt file
  3. included with this software distribution. You may not use this file except in compliance
  4. with this license.
  5. Copyright (c) Dynastream Innovations Inc. 2014
  6. All rights reserved.
  7. */
  8. #include "types.h"
  9. #include "macros.h"
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <stdarg.h>
  13. #if defined(_MSC_VER)
  14. //////////////////////////////////////////////////////////////////////////////////
  15. // Public Functions
  16. //////////////////////////////////////////////////////////////////////////////////
  17. int SNPRINTF(char* pcDst_, size_t uNum_, const char* pcFormat_, ...)
  18. {
  19. va_list hList;
  20. int iWritten;
  21. if(uNum_ == 0)
  22. return -1;
  23. va_start(hList, pcFormat_);
  24. iWritten = vsnprintf_s(pcDst_, uNum_, _TRUNCATE, pcFormat_, hList);
  25. va_end(hList);
  26. if(iWritten == -1) //check for truncation
  27. return ((int)(uNum_-1));
  28. return iWritten;
  29. }
  30. BOOL STRNCPY(char* pcDst_, const char* pcSrc_, size_t uNum_)
  31. {
  32. if(strlen(pcSrc_) + 1 > uNum_)
  33. return FALSE;
  34. return (strcpy_s(pcDst_, uNum_, pcSrc_) == 0);
  35. }
  36. BOOL STRNCAT(char* pcDst_, const char* pcSrc_, size_t uNum_)
  37. {
  38. if(strlen(pcDst_) + strlen(pcSrc_) + 1 > uNum_)
  39. return FALSE;
  40. return (strcat_s(pcDst_, uNum_, pcSrc_) == 0);
  41. }
  42. FILE* FOPEN(const char* pcFilename_, const char* pcMode_)
  43. {
  44. FILE* pfFile;
  45. return (fopen_s(&pfFile, pcFilename_, pcMode_) == 0) ? pfFile : NULL;
  46. }
  47. #else
  48. // NOTE: Alternative macro --> #define SNPRINTF(dst, num, fmt, ...) (snprintf(dst, num, fmt, __VA_ARGS__) != -1 ? strlen(dst) : -1)
  49. // C99 standard states that it will return the number of bytes that should have been written. This is not necessarily the case with older libraries.
  50. int SNPRINTF(char* pcDst_, size_t uNum_, const char* pcFormat_, ...)
  51. {
  52. int iReturn;
  53. va_list hList;
  54. if(uNum_ == 0)
  55. return -1;
  56. if(uNum_ == 1)
  57. {
  58. pcDst_[0] = '\0';
  59. return 0;
  60. }
  61. va_start(hList, pcFormat_);
  62. iReturn = vsnprintf(pcDst_, uNum_, pcFormat_, hList);
  63. pcDst_[uNum_-1] = '\0';
  64. va_end(hList);
  65. if(iReturn < 0) //check if an error occured
  66. return iReturn;
  67. if(iReturn >= (int)( uNum_-1) ) //check if output is truncated
  68. return uNum_-1;
  69. return iReturn;
  70. }
  71. BOOL STRNCPY(char* pcDst_, const char* pcSrc_, size_t uNum_)
  72. {
  73. if(uNum_ == 0) //cannot copy a negative amount
  74. return FALSE;
  75. if(strlen(pcSrc_) + 1 > uNum_) //make sure it won't get truncated
  76. return FALSE;
  77. strncpy(pcDst_, pcSrc_, uNum_-1);
  78. pcDst_[uNum_-1] = '\0';
  79. return TRUE;
  80. }
  81. BOOL STRNCAT(char* pcDst_, const char* pcSrc_, size_t uNum_)
  82. {
  83. size_t uDstLen = strlen(pcDst_);
  84. if(uNum_ == 0 || uNum_-1 < uDstLen) //cannot concatenate a negative amount
  85. return FALSE;
  86. if(uDstLen + strlen(pcSrc_) + 1 > uNum_)
  87. return FALSE;
  88. strncat(pcDst_, pcSrc_, (uNum_-1)-uDstLen);
  89. return TRUE;
  90. }
  91. FILE* FOPEN(const char* pcFilename_, const char* pcMode_)
  92. {
  93. return fopen(pcFilename_, pcMode_);
  94. }
  95. #endif