-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring_manip.c
executable file
·52 lines (46 loc) · 1.38 KB
/
string_manip.c
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
#include "main.h"
#include "string_manip.h"
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
char *fitos(int64_t input, char *format) {
int64_t length = snprintf(NULL, 0, format, input);
char *buffer = NEW(char, length + 1);
snprintf(buffer, length + 1, format, input);
return buffer;
}
char *fdtos(double input, char *format) {
int64_t length = snprintf(NULL, 0, format, input);
char *buffer = NEW(char, length + 1);
snprintf(buffer, length + 1, format, input);
return buffer;
}
char *itos(int64_t input) {
return fitos(input, "%d");
}
char *dtos(double input) {
return fdtos(input, "%f");
}
char *create_buffer(char *string) {
char *buffer = calloc(strlen(string) + 1, sizeof(char));
strcpy(buffer, string);
return buffer;
}
char *vstrcat(char *buffer, ...) {
va_list ap;
va_start(ap, buffer);
uint16_t buffer_size = strlen(buffer);
uint16_t used_size = strlen(buffer);
char *string;
while (string = va_arg(ap, char *)) {
int64_t length = strlen(string);
while (buffer_size - used_size < length) {
buffer_size = buffer_size ? buffer_size * 2 : 100;
buffer = realloc(buffer, sizeof(char) * buffer_size + 1);
}
used_size += length;
buffer = strcat(buffer, string);
}
va_end(ap);
return buffer;
}