asprintf.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /**
  2. * \file asprintf.c
  3. *
  4. * \brief GIS Library - GRASS implementation of asprintf().
  5. *
  6. * Eric G. Miller - Thu, 2 May 2002 17:51:54 -0700
  7. *
  8. * I've got a sort of cheat for asprintf. We can't use vsnprintf for the
  9. * same reason we can't use snprintf ;-) Comments welcome.
  10. *
  11. * We cheat by printing to a tempfile via vfprintf() and then reading it
  12. * back in. Probably not the most efficient way.
  13. *
  14. * <b>WARNING:</b> Temporarily, the G_asprintf macro cannot be used. See
  15. * explanation in gisdefs.h.
  16. *
  17. * (C) 2001-2008 by the GRASS Development Team
  18. *
  19. * This program is free software under the GNU General Public License
  20. * (>=v2). Read the file COPYING that comes with GRASS for details.
  21. *
  22. * \author Eric Miller - egm2 at jps net
  23. *
  24. * \date 2002-2008
  25. */
  26. #define _GNU_SOURCE /* enable asprintf */
  27. #include <grass/config.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <stdarg.h>
  31. #include <unistd.h>
  32. #include <assert.h>
  33. #include <grass/gis.h>
  34. #ifdef __MINGW32__
  35. #include <windows.h>
  36. #endif /* __MINGW32__ */
  37. #ifndef G_asprintf
  38. /**
  39. * \brief Safe replacement for <i>asprintf()</i>.
  40. *
  41. * Allocate a string large enough to hold the new output, including the
  42. * terminating NULL, and returns a pointer to the first parameter. The
  43. * pointer should be passed to <i>G_free()</i> to release the allocated
  44. * storage when it is no longer needed.
  45. *
  46. * \param[out] out
  47. * \param[in] fmt
  48. * \return number of bytes written
  49. */
  50. #ifdef HAVE_ASPRINTF
  51. int G_vasprintf(char **out, const char *fmt, va_list ap)
  52. {
  53. return vasprintf(out, fmt, ap);
  54. }
  55. #else
  56. int G_vasprintf(char **out, const char *fmt, va_list ap)
  57. {
  58. int ret_status = EOF;
  59. char dir_name[2001];
  60. char file_name[2000];
  61. FILE *fp = NULL;
  62. char *work = NULL;
  63. assert(out != NULL && fmt != NULL);
  64. /* Warning: tmpfile() does not work well on Windows (MinGW)
  65. * if user does not have write access on the drive where
  66. * working dir is? */
  67. #ifdef __MINGW32__
  68. /* file_name = G_tempfile(); */
  69. GetTempPath(2000, dir_name);
  70. GetTempFileName(dir_name, "asprintf", 0, file_name);
  71. fp = fopen(file_name, "w+");
  72. #else
  73. fp = tmpfile();
  74. #endif /* __MINGW32__ */
  75. if (fp) {
  76. int count;
  77. count = vfprintf(fp, fmt, ap);
  78. if (count >= 0) {
  79. work = G_calloc(count + 1, sizeof(char));
  80. if (work != NULL) {
  81. rewind(fp);
  82. ret_status = fread(work, sizeof(char), count, fp);
  83. if (ret_status != count) {
  84. ret_status = EOF;
  85. G_free(work);
  86. work = NULL;
  87. }
  88. }
  89. }
  90. fclose(fp);
  91. #ifdef __MINGW32__
  92. unlink(file_name);
  93. #endif /* __MINGW32__ */
  94. }
  95. *out = work;
  96. return ret_status;
  97. }
  98. #endif /* HAVE_ASPRINTF */
  99. int G_asprintf(char **out, const char *fmt, ...)
  100. {
  101. va_list ap;
  102. int count;
  103. va_start(ap, fmt);
  104. count = G_vasprintf(out, fmt, ap);
  105. va_end(ap);
  106. return count;
  107. }
  108. #endif /* G_asprintf */