2
2
* libetm-0.4 / str_mem.c - Copyright (C) Emmanuel Thomas-Maurin 2008-2012
3
3
* <manutm007@gmail.com>
5
* - some strings and memory management functions -
5
* - A few strings and memory management functions -
7
7
* This program is free software: you can redistribute it and/or modify
8
8
* it under the terms of the GNU General Public License as published by
47
* concanate n bytes max of src to dest then add '\0' at the end of dest
48
* strings may not be identical and should not overlap
47
* Concanate n bytes max of src to dest then add '\0' at end of dest.
48
* Strings may not be identical and should not overlap.
50
50
char *str_n_cat(char *dest, const char *src, size_t length)
85
* append l_str_src (can be NULL) to l_str_dest (re-allocate memory as necessary)
86
* l_str_dest must have been created by l_str_new - strings may overlap
85
* Append l_str_src (can be NULL) to l_str_dest (re-allocate memory as necessary).
86
* l_str_dest must have been created by l_str_new - strings may overlap.
88
88
char *l_str_cat(char *l_str_dest, const char *l_str_src)
125
* wrappers for malloc(), realloc(), calloc() and free() which check returned value
125
* Wrappers for malloc(), realloc(), calloc() and free() which check returned value.
127
127
void *malloc2(size_t size)
184
* return size in readable format (KiB, MiB, GiB, TiB)
185
* *** allow up to 16 simultaneous calls ***
186
* convention: we assume 2.5 MiB = 2 MiB + [0.5 x 1024 = 512] KiB, not 500 KiB
187
* otherwise, there is no way to express a value in the range 1000 - 1023
184
* Return size in readable format (KiB, MiB, GiB, TiB).
185
* *** Allow up to 16 simultaneous calls ***
186
* Convention: we assume 2.5 MiB = 2 MiB + [0.5 x 1024 = 512] KiB, not 500 KiB.
187
* Otherwise, there is no way to express a value in the range 1000 - 1023,
188
188
* so x.y MiB = x MiB + 0.y MiB, that is: not y x 100 KiB but y x 102.4 KiB
189
189
* (isn't this a bit confusing?)
191
191
const char *readable_size(double size_bytes)
193
/* we use an array of 16 strings to store the returned string in order
193
/* We use an array of 16 strings to store the returned string in order
194
194
* to allow 16 simultaneous calls (should be enough in most cases?)
195
* otherwise, sth like:
195
* Otherwise, sth like:
196
196
* printf("size1 = %s / size2 = %s\n", readable_size(size1), readable_size(size2))
197
197
* would produce unpredictable / false results (like readable sizes are
198
* equal whereas sizes are different
198
* equal whereas sizes are different).
200
200
static char lib_static_str[16][128];
201
201
unsigned long size_KiB, size_MiB, size_GiB, size_TiB;
231
* itoa() is not ansi c so this one could be useful
232
* *** allow up to 16 simultaneous calls ***
231
* itoa() is not ansi c so this one could be useful.
232
* *** Allow up to 16 simultaneous calls ***
234
234
const char *itoa2(long int n)
236
/* array of 16 strings (like for readable_size() - see above) */
236
/* Array of 16 strings (like for readable_size() - see above) */
237
237
static char lib_static_str[16][128];
238
238
static int count = -1;
276
* very basic xor sym encryption for small strings (up to 511 chars)
277
* *** key must be ascii / if 2 chars match (in str and key) the string is cut ***
278
* *** allow up to 16 simultaneous calls ***
279
* works with ascii strings, probably not otherwise so don't try with
276
* Very basic xor sym encryption for small strings (up to 511 chars).
277
* *** Key must be ascii / if 2 chars match (in str and key) the string is cut ***
278
* *** Allow up to 16 simultaneous calls ***
279
* Works with ascii strings, probably not otherwise so don't try with
280
280
* exotic things...
282
282
const char *str_crypt(const char *str, const char *key)
284
/* array of 16 strings (like for readable_size() - see above) */
284
/* Array of 16 strings (like for readable_size() - see above) */
285
285
static char str2[16][512];
286
286
static int count = -1;
318
* generate a random string up to 1023 chars long
318
* Generate a random string up to 1023 chars long.
319
319
* mode = a -> alpha / d -> digits / b -> both
321
321
const char *rnd_str(char mode, int length)