wa_str.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #pragma once
  2. #ifdef WA_STR_EXPORT
  3. #include <bfc/platform/export.h>
  4. #define DLLIMPORT DLLEXPORT
  5. #else
  6. #define DLLIMPORT
  7. #endif
  8. #ifdef __cplusplus
  9. extern "C" {
  10. #endif
  11. /* if you need a temporary ref counted string to pass INTO a function
  12. and have a hardcoded string you want to define on the stack,
  13. you can use this macro.
  14. e.g. wa_str myStr = WA_STR_STATIC("test"); */
  15. #define WA_STR_STATIC(x) ((wa_str)"\0\0\0\0" ## x);
  16. typedef void *wa_str;
  17. /* convert a C string into a ref counted string
  18. if you own the string, try using wa_str_own instead */
  19. DLLIMPORT wa_str wa_strdup(const char *);
  20. /* add a reference to a string
  21. returns the new pointer to use!!!
  22. in some cases (e.g. WA_STR_STATIC strings) the string must be re-malloc'd
  23. so be sure to assign the return value to your string
  24. e.g. wa_str myCopy = wa_str_addref(str); */
  25. DLLIMPORT wa_str wa_str_retain(wa_str str);
  26. /* release a reference to a string */
  27. DLLIMPORT void wa_str_release(wa_str str);
  28. /* gets a C-style string from a ref counted string.
  29. only valid for as long as you hold a reference! */
  30. DLLIMPORT const char *wa_str_get(wa_str str);
  31. /* copies the contents of a ref counted string into the passed buffer
  32. *dest = 0 on empty string */
  33. DLLIMPORT void wa_str_strcpy(wa_str str, char *dest, size_t destlen);
  34. /* allocates a reference counted string large enough to hold the given character count
  35. len MUST include null terminator (e.g. pass 5 to malloc enough for "test")
  36. data is set to where the character data can be written to.
  37. you'll need to null terminate the string you write */
  38. DLLIMPORT wa_str wa_str_malloc(size_t len, char **data);
  39. /* allocates a reference counted string using a C string you already have
  40. frees your string with the supplied free_func when reference count reaches 0 */
  41. DLLIMPORT wa_str wa_str_own(char *ptr, void (*free_func)(char *));
  42. /* a convenient typedef for the above function.
  43. if you want to pass the standard C free() function
  44. use (WA_STR_FREE_FUNC)free */
  45. typedef void (*WA_STR_FREE_FUNC)(char *);
  46. #ifdef __cplusplus
  47. }
  48. #endif