|
| 1 | +UPSCLI_STR_ADD_UNIQUE_TOKEN(3) |
| 2 | +============================== |
| 3 | + |
| 4 | +NAME |
| 5 | +---- |
| 6 | + |
| 7 | +upscli_str_add_unique_token - Add a unique token into a string buffer, |
| 8 | + with optional callbacks as needed by implementation |
| 9 | + |
| 10 | +SYNOPSIS |
| 11 | +-------- |
| 12 | + |
| 13 | +------ |
| 14 | + #include <upsclient.h> |
| 15 | + |
| 16 | + int upscli_str_add_unique_token(char *tgt, size_t tgtsize, const char *token, |
| 17 | + int (*callback_always)(char *, size_t, const char *), |
| 18 | + int (*callback_unique)(char *, size_t, const char *) ); |
| 19 | +------ |
| 20 | + |
| 21 | +DESCRIPTION |
| 22 | +----------- |
| 23 | + |
| 24 | +The *upscli_str_add_unique_token*() function takes the pointer 'tgt' to a |
| 25 | +caller-provided `char *` buffer of size 'tgtsize', and the pointer 'token' |
| 26 | +to a contiguous token that should be added to the end of 'tgt' buffer, if |
| 27 | +it is not there yet. |
| 28 | + |
| 29 | +The 'token' contents are stripped of surrounding space characters, and the |
| 30 | +method recurses to independently process each space-separated token inside |
| 31 | +(if there are any spaces left). |
| 32 | + |
| 33 | +The resulting 'tgt' buffer would eventually collect a string comprised of |
| 34 | +unique 'token' values in order of first mention, separated by single space |
| 35 | +characters (ASCII '0x20'). |
| 36 | + |
| 37 | +Optionally calls 'callback_always' (if not `NULL`) after checking the input |
| 38 | +for spaces (and maybe recursing) and before checking if the token is already |
| 39 | +there, and/or 'callback_unique' (if not `NULL`) after checking for uniqueness |
| 40 | +and just before going to add a newly seen token. |
| 41 | + |
| 42 | +* If such callback returns '0', abort the addition of 'token' and return '-3'. |
| 43 | + |
| 44 | +It is up to the caller to dynamically or statically allocate the 'tgt' buffer |
| 45 | +and *free*() it if needed. As far as this method is concerned, the buffer |
| 46 | +may be recycled (in data processing loops) by setting the initial character |
| 47 | +to `'\0'`, making it an empty string again. |
| 48 | + |
| 49 | +RETURN VALUE |
| 50 | +------------ |
| 51 | + |
| 52 | +The *upscli_str_add_unique_token*() function returns a numeric code: |
| 53 | + |
| 54 | +* '0' if the 'token' value was already there in 'tgt' buffer; |
| 55 | +* '1' if 'token' was added successfully; |
| 56 | +* '-1' if we needed to add the 'token', but it did not fit under the 'tgtsize' |
| 57 | + limit; |
| 58 | +* '-2' if either 'token' or 'tgt' string was `NULL`, or if 'token' was empty; |
| 59 | +* '-3' if the 'token' was rejected by the optional callback method returning '0'. |
| 60 | + |
| 61 | +SEE ALSO |
| 62 | +-------- |
| 63 | + |
| 64 | +linkman:upscli_str_contains_token[3] |
0 commit comments