Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Aug 3rd, 2011  |  syntax: C  |  size: 4.24 KB  |  views: 78  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
This paste has a previous version, view the difference. Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /*
  2. *    C Interface: SPString
  3. *      
  4. *    Description: simple String structure and manipulation
  5. *    
  6. *    This class provides a safe and convenient, albeit slower, alternative
  7. *    to C character chains.
  8. *
  9. *    There are two types of strings:
  10. *    1. String is a fully dynamic string, that is be resized automatically
  11. *    at runtime when calling stringcpy and similar functions.
  12. *    One doesn't have to worry about buffer length.
  13. *    2. LString (Local String) has a fixed buffer size.
  14. *    lstringcpy and the like will check that the buffer size is respected
  15. *    and raise an error if it's not the case.
  16. *
  17. *    One can easily create a String from a C string with
  18. *    newString(const char *).
  19. *    String.str always returns a null-terminated C chain, and String.len
  20. *    returns its length.
  21. *    One should always avoid to manipulate the String structure members
  22. *    directly in order to prevent corruption (i.e mainly incorrect sz and
  23. *    len values).
  24. *
  25. *    Most functions mirror standard C functions.
  26. */
  27.  
  28. #ifndef SPSTRING_H
  29. #define SPSTRING_H
  30.  
  31. #include <stddef.h>
  32.  
  33. /* ---- Allocation on the stack (non dynamic)
  34.    The size sz is fixed once and for all at creation and must be the maximum
  35.    size of the buffer holding the string, not the length of the C character
  36.    string passed as parameter at initialization.
  37.  
  38.     Example Usage:
  39.     {
  40.       #define SIZE 40   /* the LString cannot never grow longer than 39 chars */
  41.       const char buff[SIZE] = "Hello";
  42.       LString *hello = localString(buff, SIZE);
  43.       ...
  44.       lstringchcat(hello, " World !");
  45.     } /* automatic deallocation at end of scope */
  46.  
  47.    Warning: The following does not work (because localString does  
  48.             not allocate memory):
  49.     LString *hello = localString("Hello World", SIZE);  
  50. */
  51.  
  52. /* Definition of a local String */
  53. typedef struct {
  54.     size_t sz;  /* max buffer size, fixed, always > len */
  55.     size_t len; /*length (final '\0' excluded)            */
  56.     char *str;  /* null terminated character chain        */
  57. } LString;
  58.  
  59. /* Create a local String */
  60. LString localString ( char *chars, size_t szMax );
  61.  
  62. size_t lstringcpy ( LString *dst, const LString *src );
  63. size_t lstringchcpy ( LString *dst, const char *src );
  64. size_t lstringcat ( LString *dst, const LString *src );
  65. size_t lstringchcat ( LString *dst, const char *src );
  66. LString lstringdup ( const LString *src );
  67.  
  68.  
  69. /* --- Dynamic allocation
  70.    The size is automatically adjusted at any time.
  71.    
  72.    Usage example:
  73.       LString *hello = newString("Hello ");
  74.       ...
  75.       stringchcat(hello, " world !");
  76.       ...
  77.       delString(myString);
  78. */
  79.  
  80. typedef struct{
  81.     size_t sz;  /* max buffer size, dynamic(always > len) */
  82.     size_t len; /* length (final '\0' excluded)          */
  83.     char *str;  /* null terminated character chain       */
  84. } String;
  85.  
  86. /* Allocate a new String object.
  87.  * The source buffer passed as parameter is copied so it must be
  88.  * deallocated manually. */
  89. String * newString(const char *);
  90. void delString(String *);
  91.  
  92. size_t stringcpy(String *dst, const String *src);
  93. size_t stringchcpy(String *dst, const char *src);
  94. size_t stringcat(String *dst, const String *src);
  95. size_t stringchcat(String *dst, const char *src);
  96. String * stringdup(const String *);
  97.  
  98. /* --- The following functions apply both on String and LString */
  99.  
  100. size_t stringlen(const String *);
  101. int stringcmp(const String *st1, const String *st2);
  102. int stringchcmp(const String *st1, const char *st2);
  103. /* Truncation of length to Nth character */
  104. String * stringtrunc(String *string, size_t N);
  105. void stringprintf ( String *dst, const char * format, ... );
  106. /* Verification of internal consistency of a String/LString
  107.    Can be useful in a debugging session */
  108. int stringcheck(const String *string, char *file, int line);
  109.  
  110. /* Use this function to get the error code     */
  111. /* if (get_stringerr() != ST_NO_ERR) --> error */
  112. int get_stringerr();  
  113.  
  114. /* Possible errors */
  115. #define ST_NO_ERR                   -1000
  116. #define ST_OVERWRITE_ERR            -1001
  117. #define ST_ALLOC_ERR                -1002
  118. #define ST_NULLPARAM_ERR            -1003
  119. #define ST_NULLSTR_ERR              -1004
  120. #define ST_INCONSISTENTSZ_ERR       -1005
  121. #define ST_INCONSISTENTLEN_ERR      -1006
  122.  
  123. #endif
clone this paste RAW Paste Data