| 1 | /* lenstring.h --- interface to lenstring type. |
|---|
| 2 | Jim Blandy <jimb@gnu.ai.mit.edu> --- September 1994 */ |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | /* In order to process data which may contain null ('\0') characters, |
|---|
| 6 | it's often helpful to use a representation of strings which stores |
|---|
| 7 | the length separately from the data. Like this: */ |
|---|
| 8 | typedef struct |
|---|
| 9 | { |
|---|
| 10 | char *text; |
|---|
| 11 | int len; |
|---|
| 12 | } |
|---|
| 13 | lenstring; |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | /* Write STRING to STREAM. |
|---|
| 17 | Return value same as for stdio's fwrite. */ |
|---|
| 18 | extern size_t write_lenstring (lenstring *STRING, FILE *STREAM); |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | /* Write STRING to STREAM, left-justified in a field of FIELD spaces. |
|---|
| 22 | If STRING is longer than FIELD, truncate it. */ |
|---|
| 23 | extern void display_clipped_lenstring (lenstring *string, |
|---|
| 24 | int field, |
|---|
| 25 | FILE *stream); |
|---|
| 26 | |
|---|
| 27 | /* Read text from SOURCE until we find DELIMITER, or hit EOF. |
|---|
| 28 | Set *STRING to the text we read; the delimiting string or EOF is |
|---|
| 29 | not included in STRING. |
|---|
| 30 | |
|---|
| 31 | If the text was terminated by EOF, and is empty (i.e. its length is |
|---|
| 32 | zero), set STRING->text to zero and return EOF. In this case, the |
|---|
| 33 | caller should not free STRING->text. |
|---|
| 34 | |
|---|
| 35 | Otherwise, the text is stored in memory obtained via malloc, and |
|---|
| 36 | should be freed by the caller. |
|---|
| 37 | |
|---|
| 38 | If the string was terminated by DELIMITER, return 0. |
|---|
| 39 | If the string was non-empty and terminated by EOF, return 1. |
|---|
| 40 | If an error occurred reading the string, print an error message |
|---|
| 41 | and exit. */ |
|---|
| 42 | extern int read_delimited_lenstring (lenstring *STRING, |
|---|
| 43 | char *DELIMITER, |
|---|
| 44 | FILE *STREAM); |
|---|
| 45 | |
|---|
| 46 | /* Search STRING for an occurrence of SUBSTRING starting not before |
|---|
| 47 | START, and return its starting position, or -1 if SUBSTRING is not |
|---|
| 48 | a substring of STRING. */ |
|---|
| 49 | extern size_t search_lenstring (lenstring *STRING, |
|---|
| 50 | const char *SUBSTRING, |
|---|
| 51 | size_t START); |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | /* Strip newlines from IN, leaving the result in OUT. |
|---|
| 55 | OUT points to the same text as IN. */ |
|---|
| 56 | extern void strip_newlines (lenstring *OUT, lenstring *IN); |
|---|
| 57 | |
|---|
| 58 | |
|---|
| 59 | /* Append STR2 to the end of STR1. Assume STR1 has sufficient space |
|---|
| 60 | allocated. */ |
|---|
| 61 | extern void append_lenstring (lenstring *STR1, lenstring *STR2); |
|---|
| 62 | |
|---|
| 63 | /* Append character C to the end of STR. Assume STR has sufficient |
|---|
| 64 | space allocated. */ |
|---|
| 65 | #define APPEND_CHAR(str, c) ((str)->text[(str)->len++] = (c)) |
|---|