-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinfo_string.c
More file actions
84 lines (69 loc) · 1.87 KB
/
info_string.c
File metadata and controls
84 lines (69 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include "info_string.h"
#include "info_char.h"
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
info_String info_string_create(size_t cap)
{
return (info_String){
.str = malloc(cap*sizeof(info_char)),
.len = 0,
.cap = cap
};
}
int info_string_realloc(info_String *s, size_t cap)
{
s->str = realloc(s->str, cap);
s->cap=cap;
return !s->str;
}
int info_string_vprintf(info_String *str, const info_char *format, va_list args)
{
if(!format)
return -1;
int res;
va_list arg_tmp;
#ifdef INFO_WIDE
size_t cap = str->cap;
while((res=vswprintf(str->str+str->len, str->cap-str->len, format, args))==-1) {
cap*=2;
if(info_string_realloc(str, cap))
return -1;
va_copy(arg_tmp, args);
}
str->len += res;
/* if(info_string_realloc(&s, s.len+res)) */
/* INTERNAL("Allocation Failed!") */
return res;
#else
va_copy(arg_tmp, args);
int length = vsnprintf(NULL ,0, format, arg_tmp);
va_end(arg_tmp);
if(length<0)
return -1;
info_string_realloc(str, str->len+length+1);
res = vsnprintf(str->str+str->len, length+1, format, args);
str->len += length;
return res;
#endif
}
int info_string_printf(info_String *str, const info_char *format, ...)
{
va_list args;
va_start(args, format);
int res = info_string_vprintf(str, format, args);
va_end(args);
return res;
}
int info_string_puts(info_String *str, const info_char *s, size_t len)
{
if(str->len+len >= str->cap) info_string_realloc(str, str->len+len+1);
memcpy(str->str+str->len*sizeof(info_char), s, len*sizeof(info_char));
str->len+=len;
str->str[str->len] = 0;
return 0;
}
size_t info_string_length(info_String *str)
{
return str->len;
}