1
0
Fork 0

Added functions to convert numbers to strings, still not properly tested.

This commit is contained in:
Wael Karram 2022-07-24 18:33:10 +03:00
parent 19a3f27f86
commit d78a69c83f
Signed by: wael
GPG Key ID: 3B356038CCB10808
2 changed files with 371 additions and 0 deletions

302
strings/numtostr.c Normal file
View File

@ -0,0 +1,302 @@
/**
* C_lib Copyright (C) 2022 Wael Karram.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/* This file defines functions to convert numbers to strings. */
/* Include guards + includes. */
#ifndef NUMTOSTR_C
#define NUMTOSTR_C
#include "numtostr.h"
#endif /* NUMTOSTR_C */
/* Function declarations. */
/* This function takes a short and prints it to a string. */
/* Caller should free the string, returns NULL on allocation failure. */
static inline char *stostr(short value) {
/* Variables. */
int length, result;
char *string;
/* Get the length to be allocated. */
length = snprintf(NULL, 0, FORMAT_SHORT, value);
/* Sanity check. */
assert(length > 0);
/* Attempt to allocate the string. */
string = (char*) malloc((++length) * sizeof(char));
if (!string)
return NULL;
/* Print/convert. */
result = snprintf(string, length, FORMAT_SHORT, value);
/* Check. */
if ((string[length - 1] != NULL_CHAR) || (result != length)) {
free(string);
return NULL;
}
/* Return the allocated and converted string. */
return string;
}
/* This function takes an integer and prints it to a string. */
/* Caller should free the string, returns NULL on allocation failure. */
static inline char *itostr(int value) {
/* Variables. */
int length, result;
char *string;
/* Get the length to be allocated. */
length = snprintf(NULL, 0, FORMAT_INT, value);
/* Sanity check. */
assert(length > 0);
/* Attempt to allocate the string. */
string = (char*) malloc((++length) * sizeof(char));
if (!string)
return NULL;
/* Print/convert. */
result = snprintf(string, length, FORMAT_INT, value);
/* Check. */
if ((string[length - 1] != NULL_CHAR) || (result != length)) {
free(string);
return NULL;
}
/* Return the allocated and converted string. */
return string;
}
/* This function takes an unsigned integer and prints it to a string. */
/* Caller should free the string, returns NULL on allocation failure. */
static inline char *uitostr(unsigned int value) {
/* Variables. */
int length, result;
char *string;
/* Get the length to be allocated. */
length = snprintf(NULL, 0, FORMAT_UNSIGNED_INT, value);
/* Sanity check. */
assert(length > 0);
/* Attempt to allocate the string. */
string = (char*) malloc((++length) * sizeof(char));
if (!string)
return NULL;
/* Print/convert. */
result = snprintf(string, length, FORMAT_UNSIGNED_INT, value);
/* Check. */
if ((string[length - 1] != NULL_CHAR) || (result != length)) {
free(string);
return NULL;
}
/* Return the allocated and converted string. */
return string;
}
/* This function takes a double and prints it to a string. */
/* Caller should free the string, returns NULL on allocation failure. */
static inline char *dtostr(double value) {
/* Variables. */
int length, result;
char *string;
/* Get the length to be allocated. */
length = snprintf(NULL, 0, FORMAT_DOUBLE, value);
/* Sanity check. */
assert(length > 0);
/* Attempt to allocate the string. */
string = (char*) malloc((++length) * sizeof(char));
if (!string)
return NULL;
/* Print/convert. */
result = snprintf(string, length, FORMAT_DOUBLE, value);
/* Check. */
if ((string[length - 1] != NULL_CHAR) || (result != length)) {
free(string);
return NULL;
}
/* Return the allocated and converted string. */
return string;
}
/* This function takes a long and prints it to a string. */
/* Caller should free the string, returns NULL on allocation failure. */
static inline char *ltostr(long value) {
/* Variables. */
int length, result;
char *string;
/* Get the length to be allocated. */
length = snprintf(NULL, 0, FORMAT_LONG, value);
/* Sanity check. */
assert(length > 0);
/* Attempt to allocate the string. */
string = (char*) malloc((++length) * sizeof(char));
if (!string)
return NULL;
/* Print/convert. */
result = snprintf(string, length, FORMAT_LONG, value);
/* Check. */
if ((string[length - 1] != NULL_CHAR) || (result != length)) {
free(string);
return NULL;
}
/* Return the allocated and converted string. */
return string;
}
/* This function takes an unsigned long and prints it to a string. */
/* Caller should free the string, returns NULL on allocation failure. */
static inline char *ultostr(unsigned long value) {
/* Variables. */
int length, result;
char *string;
/* Get the length to be allocated. */
length = snprintf(NULL, 0, FORMAT_UNSIGNED_LONG, value);
/* Sanity check. */
assert(length > 0);
/* Attempt to allocate the string. */
string = (char*) malloc((++length) * sizeof(char));
if (!string)
return NULL;
/* Print/convert. */
result = snprintf(string, length, FORMAT_UNSIGNED_LONG, value);
/* Check. */
if ((string[length - 1] != NULL_CHAR) || (result != length)) {
free(string);
return NULL;
}
/* Return the allocated and converted string. */
return string;
}
/* This function takes a long long and prints it to a string. */
/* Caller should free the string, returns NULL on allocation failure. */
static inline char *lltostr(long long value) {
/* Variables. */
int length, result;
char *string;
/* Get the length to be allocated. */
length = snprintf(NULL, 0, FORMAT_LONG_LONG, value);
/* Sanity check. */
assert(length > 0);
/* Attempt to allocate the string. */
string = (char*) malloc((++length) * sizeof(char));
if (!string)
return NULL;
/* Print/convert. */
result = snprintf(string, length, FORMAT_LONG_LONG, value);
/* Check. */
if ((string[length - 1] != NULL_CHAR) || (result != length)) {
free(string);
return NULL;
}
/* Return the allocated and converted string. */
return string;
}
/* This function takes an unsigned long long and prints it to a string. */
/* Caller should free the string, returns NULL on allocation failure. */
static inline char *ulltostr(unsigned long long value) {
/* Variables. */
int length, result;
char *string;
/* Get the length to be allocated. */
length = snprintf(NULL, 0, FORMAT_UNSIGNED_LONG_LONG, value);
/* Sanity check. */
assert(length > 0);
/* Attempt to allocate the string. */
string = (char*) malloc((++length) * sizeof(char));
if (!string)
return NULL;
/* Print/convert. */
result = snprintf(string, length, FORMAT_UNSIGNED_LONG_LONG, value);
/* Check. */
if ((string[length - 1] != NULL_CHAR) || (result != length)) {
free(string);
return NULL;
}
/* Return the allocated and converted string. */
return string;
}
/* This function takes a float and prints it to a string. */
/* Caller should free the string, returns NULL on allocation failure. */
static inline char *ftostr(float value) {
/* Variables. */
int length, result;
char *string;
/* Get the length to be allocated. */
length = snprintf(NULL, 0, FORMAT_FLOAT, value);
/* Sanity check. */
assert(length > 0);
/* Attempt to allocate the string. */
string = (char*) malloc((++length) * sizeof(char));
if (!string)
return NULL;
/* Print/convert. */
result = snprintf(string, length, FORMAT_FLOAT, value);
/* Check. */
if ((string[length - 1] != NULL_CHAR) || (result != length)) {
free(string);
return NULL;
}
/* Return the allocated and converted string. */
return string;
}

69
strings/numtostr.h Normal file
View File

@ -0,0 +1,69 @@
/**
* C_lib Copyright (C) 2022 Wael Karram.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/* This header file defines functions to convert numbers to strings. */
/* Include guards + includes. */
#ifndef NUMTOSTR_H
#define NUMTOSTR_H
/* Include string.h */
#include <string.h>
/* Include stdio.h */
#include <stdio.h>
/* Include assert.h to allow code generation, but only run with debug flag. */
#ifdef DEBUG
#ifndef NDEBUG
#define NDEBUG
#endif /* NDEBUG. */
#endif /* DEBUG. */
#include <assert.h>
/* TODO: CHECK CORRECTNESS. */
/* Constants. */
#define NULL_CHAR '\0'
#define FORMAT_SHORT "%hd"
#define FORMAT_INT "%d"
#define FORMAT_UNSIGNED_INT "%u"
#define FORMAT_DOUBLE "%f"
#define FORMAT_LONG "%l"
#define FORMAT_UNSIGNED_LONG "%l"
#define FORMAT_LONG_LONG "%ll"
#define FORMAT_UNSIGNED_LONG_LONG "%ll"
#define FORMAT_FLOAT "%f"
/* Function declarations. */
/* This function takes a short and prints it to a string. */
static inline char *stostr(short value);
/* This function takes an integer and prints it to a string. */
static inline char *itostr(int value);
/* This function takes an unsigned integer and prints it to a string. */
static inline char *uitostr(unsigned int value);
/* This function takes a double and prints it to a string. */
static inline char *dtostr(double value);
/* This function takes a long and prints it to a string. */
static inline char *ltostr(long value);
/* This function takes an unsigned long and prints it to a string. */
static inline char *ultostr(unsigned long value);
/* This function takes a long long and prints it to a string. */
static inline char *lltostr(long long value);
/* This function takes an unsigned long long and prints it to a string. */
static inline char *ulltostr(long long value);
/* This function takes a float and prints it to a string. */
static inline char *ftostr(float value);
#endif /* NUMTOSTR */