c_cross_pack/libs/string.h

55 lines
1.3 KiB
C

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
// -------------------------------------
// BASE - String and init of this
// -------------------------------------
typedef char* string;
string strInit(char* s) {
string str = malloc(strlen(s) * sizeof(char));
strcpy(str, s);
return str;
}
// -------------- END ------------------
// -------------------------------------
// FUNCTION - basic operations
// -------------------------------------
void strAdd(string* s, char* str_new) {
*s = realloc(*s, strlen(*s) + strlen(str_new)*sizeof(char));
strcat(*s, str_new);
}
void strSet(string* s, char* str_new) {
*s = realloc(*s, strlen(str_new)*sizeof(char));
strcpy(*s, str_new);
}
void strCopy(string* s1, string* s2) {
strcpy(*s2, *s1);
}
// -------------- END ------------------
// -------------------------------------
// CONVERT - different convert to str
// -------------------------------------
char* int2Str(int* num) {
char* buffer = malloc(12);
sprintf(buffer, "%d", *num);
return buffer;
}
char* float2Str(float* num) {
char* buffer = malloc(12);
sprintf(buffer, "%f", *num);
return buffer;
}
char* double2Str(double* num) {
char* buffer = malloc(12);
sprintf(buffer, "%f", *num);
return buffer;
}
// -------------- END ------------------