text stringlengths 0 357 |
|---|
buf = malloc(char_cnt_without_zero+2 /* +1 would be enough, however, protect against off-by-one-errors */); |
if (buf==NULL) { |
va_end(argp_copy); |
dc_strbuilder_cat(strbuilder, "ErrMem"); |
return; |
} |
vsnprintf(buf, char_cnt_without_zero+1, format, argp_copy); |
va_end(argp_copy); |
dc_strbuilder_cat(strbuilder, buf); |
free(buf); |
} |
/** |
* Set the string to a lenght of 0. This does not free the buffer; |
* if you want to free the buffer, you have to call free() on dc_strbuilder_t::buf. |
* |
* @param strbuilder The object to initialze. Must be initialized with |
* dc_strbuilder_init(). |
* @return None. |
*/ |
void dc_strbuilder_empty(dc_strbuilder_t* strbuilder) |
{ |
strbuilder->buf[0] = 0; |
strbuilder->free = strbuilder->allocated - 1 /*the nullbyte! */; |
strbuilder->eos = strbuilder->buf; |
} |
``` |
Filename: dc_strencode.c |
```c |
#include <ctype.h> |
#include <libetpan/libetpan.h> |
#include "dc_context.h" |
#include "dc_strencode.h" |
/******************************************************************************* |
* URL encoding and decoding, RFC 3986 |
******************************************************************************/ |
static char int_2_uppercase_hex(char code) |
{ |
static const char hex[] = "0123456789ABCDEF"; |
return hex[code & 15]; |
} |
static char hex_2_int(char ch) |
{ |
return isdigit(ch) ? ch - '0' : tolower(ch) - 'a' + 10; |
} |
/** |
* Url-encodes a string. |
* All characters but A-Z, a-z, 0-9 and -_. are encoded by a percent sign followed by two hexadecimal digits. |
* |
* The space in encoded as `+` - this is correct for parts in the url _after_ the `?` and saves some bytes when used in QR codes. |
* (in the URL _before_ the `?` or elsewhere, the space should be encoded as `%20`) |
* |
* Belongs to RFC 3986: https://tools.ietf.org/html/rfc3986#section-2 |
* |
* Example: The string `Björn Petersen` will be encoded as `"Bj%C3%B6rn+Petersen`. |
* |
* @param to_encode Null-terminated UTF-8 string to encode. |
* @return Returns a null-terminated url-encoded strings. The result must be free()'d when no longer needed. |
* On memory allocation errors the program halts. |
* On other errors, an empty string is returned. |
*/ |
char* dc_urlencode(const char *to_encode) |
{ |
const char *pstr = to_encode; |
if (to_encode==NULL) { |
return dc_strdup(""); |
} |
char *buf = malloc(strlen(to_encode) * 3 + 1), *pbuf = buf; |
if (buf==NULL) { |
exit(46); |
} |
while (*pstr) |
{ |
if (isalnum(*pstr) || *pstr=='-' || *pstr=='_' || *pstr=='.' || *pstr=='~') { |
*pbuf++ = *pstr; |
} |
else if (*pstr==' ') { |
*pbuf++ = '+'; |
} |
else { |
*pbuf++ = '%', *pbuf++ = int_2_uppercase_hex(*pstr >> 4), *pbuf++ = int_2_uppercase_hex(*pstr & 15); |
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.