Advertisement
Guest User

Untitled

a guest
Dec 8th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <limits.h>
  4. #ifndef INSANE_ASPRINTF_H
  5. #define INSANE_ASPRINTF_H
  6.  
  7. #ifndef __cplusplus
  8. #include <stdarg.h>
  9. #else
  10. #include <cstdarg>
  11. extern "C"
  12. {
  13. #endif
  14.  
  15. #define insane_free(ptr) { free(ptr); ptr = 0; }
  16.  
  17. int vasprintf(char **strp, const char *fmt, va_list ap);
  18. int asprintf(char **strp, const char *fmt, ...);
  19.  
  20. #ifdef __cplusplus
  21. }
  22. #endif
  23.  
  24. #endif
  25.  
  26. int vasprintf(char **strp, const char *fmt, va_list ap)
  27. {
  28. int r = -1, size = _vscprintf(fmt, ap);
  29.  
  30. if ((size >= 0) && (size < INT_MAX))
  31. {
  32. *strp = (char *)malloc(size + 1); //+1 for null
  33. if (*strp)
  34. {
  35. r = vsnprintf(*strp, size + 1, fmt, ap); //+1 for null
  36. if ((r < 0) || (r > size))
  37. {
  38. insane_free(*strp);
  39. r = -1;
  40. }
  41. }
  42. }
  43. else { *strp = 0; }
  44.  
  45. return(r);
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement