Tools made in assistance of the Metacall Project
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 

154 lignes
3.1 KiB

#include "tstring.h"
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <stdbool.h>
tstring* tstring_create(size_t sz) {
tstring* buffer = malloc(sizeof(tstring)+sz);
memset(buffer, 0, sizeof(tstring)+sz);
buffer->size = sz;
return buffer;
}
tstring* tstring_copy(tstring* src) {
tstring* buffer = tstring_create(src->size);
memcpy(buffer->data, src->data, src->size+1);
return buffer;
}
tstring* cstring_to_tstring(char* src) {
size_t sz = strlen(src);
tstring* buffer = tstring_create(sz);
memcpy(buffer->data, src, sz);
buffer->size = sz;
return buffer;
}
tstring* integer_to_tstring(int64_t val) {
char conversion[20];
bool sign = val < 0;
char* it = conversion;
if(sign) {
val *= -1;
}
if(val == 0) {
tstring* ret = tstring_create(1);
ret->data[0] = '0';
return ret;
}
while(val != 0) {
*(it++) = val%10+'0';
val/=10;
}
tstring* ret = tstring_create((it-conversion) + sign);
char* dest_it = ret->data;
if(sign) {
*(dest_it++) = '-';
}
do {
*(dest_it++) = *(it--);
} while(it != conversion);
return ret;
}
tstring* tstring_concatenate(tstring* lhs, tstring* rhs) {
tstring* ret = tstring_create(lhs->size + rhs->size);
memcpy(ret->data, lhs->data, lhs->size);
memcpy(ret->data + lhs->size, rhs->data, rhs->size);
return ret;
}
tstring* tstring_n_concatenate(size_t count, ...) {
va_list strs;
va_start(strs, count);
size_t tot_sz = 0;
size_t idx;
for(idx = 0; idx < count; idx++) {
tot_sz += va_arg(strs, tstring*)->size;
}
tstring* ret = tstring_create(tot_sz);
va_end(strs);
va_start(strs, count);
char* it = ret->data;
for(idx = 0; idx < count; idx++) {
tstring* curr = va_arg(strs, tstring*);
memcpy(it, curr->data, curr->size);
it+=curr->size;
}
va_end(strs);
ret->size = tot_sz;
return ret;
}
int tstring_n_write(FILE* file, size_t count, ...) {
va_list strs;
va_start(strs, count);
size_t idx;
for(idx = 0; idx < count; idx++) {
tstring* curr = va_arg(strs, tstring*);
if(fwrite(curr->data,1, curr->size, file) != curr->size) {
return ferror(file);
}
}
va_end(strs);
return 0;
}
tstring* tstring_n_compose(const char* count, ...) {
va_list strs;
size_t nb = strlen(count);
va_start(strs, count);
size_t tot_sz = 0;
size_t idx;
for(idx = 0; idx < nb; idx++) {
switch(count[idx]){
case 't':
tot_sz += va_arg(strs, tstring*)->size;
break;
case 'c':
tot_sz += strlen(va_arg(strs, char*));
break;
default:
va_end(strs);
exit(EXIT_FAILURE);
break;
}
}
tstring* ret = tstring_create(tot_sz);
char* it = ret->data;
va_end(strs);
va_start(strs, count);
for(idx = 0; idx < nb; idx++) {
switch(count[idx]){
case 't':
{
tstring* curr = va_arg(strs, tstring*);
memcpy(it, curr->data, curr->size);
it+=curr->size;
}
break;
case 'c':
{
char* curr = va_arg(strs, char*);
size_t sz = strlen(curr);
memcpy(it, curr, sz);
it+=sz;
}
break;
default:
perror("Invalid tstring concatenation");
exit(EXIT_FAILURE);
break;
}
}
va_end(strs);
ret->size = tot_sz;
return ret;
}
void tstring_destroy(tstring* del) {
free(del);
}