ltk

Socket-based GUI for X11 (WIP)
git clone git://lumidify.org/ltk.git (fast, but not encrypted)
git clone https://lumidify.org/git/ltk.git (encrypted, but very slow)
Log | Files | Refs | README | LICENSE

txtbuf.h (2393B)


      1 #ifndef LTK_TXTBUF_H
      2 #define LTK_TXTBUF_H
      3 
      4 #include <stddef.h>
      5 
      6 /*
      7  * txtbuf is really just a string data type that is badly named.
      8  * The stored text is always nul-terminated.
      9  * FIXME: this data type is abused in some places and manually
     10  * created so it isn't nul-terminated
     11  */
     12 
     13 typedef struct {
     14 	size_t len, cap;
     15 	char *text;
     16 } txtbuf;
     17 
     18 /*
     19  * Create an empty txtbuf.
     20  */
     21 txtbuf *txtbuf_new(void);
     22 
     23 /*
     24  * Create a new txtbuf, initializing it with the nul-terminated
     25  * string 'str'. The input string is copied.
     26  */
     27 txtbuf *txtbuf_new_from_char(const char *str);
     28 
     29 /*
     30  * Create a new txtbuf, initializing it with the string 'str'
     31  * of length 'len'. The input string is copied.
     32  */
     33 txtbuf *txtbuf_new_from_char_len(const char *str, size_t len);
     34 
     35 /*
     36  * Replace the stored text in 'buf' with the text generated by
     37  * 'snprintf' when called with the given format string and args.
     38  */
     39 void txtbuf_fmt(txtbuf *buf, const char *fmt, ...);
     40 
     41 /*
     42  * Replace the stored text in 'buf' with 'text'.
     43  */
     44 void txtbuf_set_text(txtbuf *buf, const char *text);
     45 
     46 /*
     47  * Same as txtbuf_set_text, but with explicit length for 'text'.
     48  */
     49 void txtbuf_set_textn(txtbuf *buf, const char *text, size_t len);
     50 
     51 /*
     52  * Append 'text' to the text stored in 'buf'.
     53  */
     54 void txtbuf_append(txtbuf *buf, const char *text);
     55 
     56 /*
     57  * Same as txtbuf_append, but with explicit length for 'text'.
     58  */
     59 void txtbuf_appendn(txtbuf *buf, const char *text, size_t len);
     60 
     61 /*
     62  * Compare the text of two txtbuf's like 'strcmp'.
     63  */
     64 int txtbuf_cmp(txtbuf *buf1, txtbuf *buf2);
     65 
     66 /*
     67  * Convenience function for calling 'txtbuf_cmp' and checking if the
     68  * return value is 0, i.e. the strings are equal.
     69  */
     70 int txtbuf_eql(txtbuf *buf1, txtbuf *buf2);
     71 
     72 /*
     73  * Make sure the txtbuf has space for at least the given size,
     74  * plus '\0' at the end.
     75  */
     76 void txtbuf_resize(txtbuf *buf, size_t sz);
     77 
     78 /*
     79  * Destroy a txtbuf.
     80  */
     81 void txtbuf_destroy(txtbuf *buf);
     82 
     83 /*
     84  * Copy txtbuf 'src' to txtbuf 'dst'.
     85  */
     86 void txtbuf_copy(txtbuf *dst, txtbuf *src);
     87 
     88 /*
     89  * Duplicate txtbuf 'src'.
     90  */
     91 txtbuf *txtbuf_dup(txtbuf *src);
     92 
     93 /*
     94  * Get copy of text stored in 'buf'.
     95  * The returned text belongs to the caller and needs to be freed.
     96  */
     97 char *txtbuf_get_textcopy(txtbuf *buf);
     98 
     99 /*
    100  * Clear the text, but do not reduce the internal capacity
    101  * (for efficiency if it will be filled up again anyways).
    102  */
    103 void txtbuf_clear(txtbuf *buf);
    104 
    105 #endif /* LTK_TXTBUF */