00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "strutl.h"
00021
00022 #include <stdio.h>
00023 #include <stdarg.h>
00024 #include <stdlib.h>
00025 #include <ctype.h>
00026
00027 char * str_printf(const char *fmt, ...)
00028 {
00029
00030 int len;
00031 va_list ap;
00032 int size = 100;
00033 char *tmp, *str = NULL;
00034
00035 str = malloc(size);
00036 while (1)
00037 {
00038
00039 va_start(ap, fmt);
00040 len = vsnprintf(str, size, fmt, ap);
00041 va_end(ap);
00042
00043
00044 if (len > -1 && len < size) {
00045 return str;
00046 }
00047
00048
00049 if (len > -1)
00050 size = len+1;
00051 else
00052 size *= 2;
00053
00054 tmp = realloc(str, size);
00055 if (tmp == NULL) {
00056 return str;
00057 }
00058 str = tmp;
00059 }
00060 }
00061
00062 uint32_t str_to_uint32(const char *s, int n)
00063 {
00064 uint32_t val = 0;
00065
00066 if (n > 4)
00067 n = 4;
00068
00069 if (!s || !*s) {
00070 return (INT64_C(1) << (8*n)) - 1;
00071 }
00072
00073 while (n--) {
00074 val = (val << 8) | *s;
00075 if (*s) {
00076 s++;
00077 }
00078 }
00079
00080 return val;
00081 }
00082
00083 void str_tolower(char *s)
00084 {
00085 while (*s) {
00086 *s = tolower(*s);
00087 s++;
00088 }
00089 }