Implementations of the C++ Standard Library in... C. Can creatures armed without wings engage in flight?
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

cstring.h 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #ifndef CSTL_CSTRING_H
  2. #define CSTL_CSTRING_H
  3. #include <stdlib.h>
  4. /**
  5. * String OO-like functions: cstring
  6. *
  7. * The general idea is that a heap-allocated c-string can be either NULL, or
  8. * filled with contents.
  9. *
  10. * The strange part of the semantics here, is that it is valid to destroy an
  11. * initialized c-string, and nothing is required to de-initialize a c-string.
  12. *
  13. * A class looking to use cstring, might set all cstring (char*) members to
  14. * NULL, and reserve deinit/destroy for the actual calling of cstring_destroy.
  15. */
  16. /**
  17. * Initialize a c-string to a valid state, or, NULL.
  18. *
  19. * NOTE: The true interface for this function is:
  20. *
  21. * void cstring_init(char* s)
  22. *
  23. * Macros have simply been use to unify the interface.
  24. */
  25. #define cstring_init(s) cstring_init_(&s)
  26. void cstring_init_(char** s);
  27. /**
  28. * Create a new c-string, and fill it with the contents of c-string s.
  29. */
  30. char* cstring_create(const char* s);
  31. /**
  32. * Assign an initialized c-string s1 to a new c-string s2, where s1 is a pointer
  33. * to a c-string. (Yes, this interface is a little screwed.)
  34. *
  35. * This will cause a deallocation, if s1 already has been filled.
  36. * As NULL is a valid state for a char*, this is checked for; if s2 is NULL,
  37. * then s1 will simply be freed and will stay as NULL.
  38. *
  39. * NOTE: The true interface for this function is:
  40. *
  41. * void cstring_asn(char* s1, const char* s2)
  42. */
  43. #define cstring_asn(s1, s2) cstring_asn_(&s1, s2)
  44. void cstring_asn_(char** s1, const char* s2);
  45. /**
  46. * Destroys and releases the memory for a c-string.
  47. */
  48. void cstring_destroy(char* s);
  49. /**
  50. * Utility functions for handling c-strings
  51. */
  52. /**
  53. * Guarantees copying lim-1 chars to the destination string, reserving at least
  54. * one for a null character ('\0'). Strings copied are thus guaranteed to be
  55. * null-terminated, and provides safety by truncating silently.
  56. */
  57. void safestrcpy(char* dest, const char* src, size_t lim);
  58. /**
  59. * Guarantees concatenating as many characters as possible from src to dest.
  60. * This is intended to act as a convenience macro, to mimic safestrcpy's usage.
  61. */
  62. #define safestrcat(dest, src, dest_lim) strncat(dest, src, dest_lim - strlen(dest) - 1)
  63. /**
  64. * fstrcat, as in, "fast" strcat.
  65. * Returns a pointer to the last element, to try to alleviate future O(n) calls.
  66. */
  67. char* fstrcat(char* dest, const char* src);
  68. /**
  69. * sfstrcat, as in, "safe-fast" strcat.
  70. * Guarantees concatenating as many characters as possible from src to dest.
  71. * Returns a pointer to the last element, to try to alleviate future O(n) calls.
  72. */
  73. char* sfstrcat(char* dest, const char* src, size_t lim);
  74. #endif