Compare commits
7 Commits
27046e6bbb
...
d475d0fb6b
Author | SHA1 | Date |
---|---|---|
Wael Karram | d475d0fb6b | |
Wael Karram | fcea70353c | |
Wael Karram | d78a69c83f | |
Wael Karram | 19a3f27f86 | |
Wael Karram | 182655fad8 | |
Wael Karram | 2eac7bd96a | |
Wael Karram | d3633421aa |
|
@ -15,8 +15,6 @@
|
|||
*/
|
||||
|
||||
/* This file contains the function implementations for a double-linked-list. */
|
||||
/* Include guard + includes. */
|
||||
#include <stdlib.h>
|
||||
#ifndef DOUBLE_LINKED_LIST_C
|
||||
#define DOUBLE_LINKED_LIST_C
|
||||
#include "double_linked_list.h"
|
||||
|
|
|
@ -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*) calloc(sizeof(char), (++length));
|
||||
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*) calloc(sizeof(char), (++length));
|
||||
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*) calloc(sizeof(char), (++length));
|
||||
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*) calloc(sizeof(char), (++length));
|
||||
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*) calloc(sizeof(char), (++length));
|
||||
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*) calloc(sizeof(char), (++length));
|
||||
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*) calloc(sizeof(char), (++length));
|
||||
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*) calloc(sizeof(char), (++length));
|
||||
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*) calloc(sizeof(char), (++length));
|
||||
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;
|
||||
}
|
|
@ -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 */
|
|
@ -1,7 +1,7 @@
|
|||
/* Include statements. */
|
||||
#include <stdio.h>
|
||||
#include <assert.h>
|
||||
#include "../linked_lists/single_linked_list.c"
|
||||
#include "../linked_lists/single_linked_list.h"
|
||||
|
||||
/* Define constants. */
|
||||
#define PRINT_INFO_TESTING_START "Starting tests.\n"
|
||||
|
|
|
@ -0,0 +1,53 @@
|
|||
/*
|
||||
* C_lib Copyright (C) 2021 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/>.
|
||||
*/
|
||||
|
||||
/* Include header. */
|
||||
#include "file_utils.h"
|
||||
|
||||
/* This function checks if a file or directory exists at the given path. */
|
||||
static inline int check_file_exists(const char *path) {
|
||||
return (path && access(path, F_OK) == 0);
|
||||
}
|
||||
|
||||
/* This function checks if the path points to a file or directory, assumes existence! */
|
||||
/* Returns 1 on file, 0 on directory, 2 for other; note that -1 indicates internal error! */
|
||||
static inline int check_if_file(const char *path) {
|
||||
/* Sanity check. */
|
||||
assert(check_file_exists(path));
|
||||
|
||||
/* Check type. */
|
||||
struct stat path_stat;
|
||||
/* Check for failure. */
|
||||
if (stat(path, &path_stat) == PASS_FAIL_FILE_UTILS_H)
|
||||
return PASS_FAIL_FILE_UTILS_H;
|
||||
/* Return the result. */
|
||||
if (S_ISREG(path_stat.st_mode))
|
||||
return TRUE_FILE_UTILS_H;
|
||||
else if (S_ISDIR(path_stat.st_mode))
|
||||
return FALSE_FILE_UTILS_H;
|
||||
else
|
||||
return OTHER_FILE_UTILS_H;
|
||||
}
|
||||
|
||||
/* This function checks if the path exists and points to a directory. */
|
||||
static inline int check_if_directory_exists(const char *path) {
|
||||
return check_file_exists(path) && (check_if_file(path) == FALSE_FILE_UTILS_H);
|
||||
}
|
||||
|
||||
/* This function checks if the path exists and points to a file. */
|
||||
static inline int check_if_directory_exists(const char *path) {
|
||||
return check_file_exists(path) && (check_if_file(path) == TRUE_FILE_UTILS_H);
|
||||
}
|
|
@ -0,0 +1,51 @@
|
|||
/*
|
||||
* 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 defines various file utility functions. */
|
||||
#ifndef FILE_UTILS_H
|
||||
#define FILE_UTILS_H
|
||||
|
||||
/* Include unistd.h (for access). */
|
||||
#include <unistd.h>
|
||||
/* Include sys/types and sys/stat to check type. */
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
/* Define constants. */
|
||||
#define PASS_FAIL_FILE_UTILS_H -1
|
||||
#define TRUE_FILE_UTILS_H 1
|
||||
#define FALSE_FILE_UTILS_H 2
|
||||
|
||||
/* Set debug if needed. */
|
||||
#ifdef DEBUG
|
||||
#ifndef NDEBUG
|
||||
#define NDEBUG
|
||||
#endif /* NDEBUG */
|
||||
#endif /* DEBUG */
|
||||
/* Include assert.h to allow code generation. */
|
||||
#include <assert.h>
|
||||
|
||||
/* This function checks if a file or directory exists at the given path. */
|
||||
static inline int check_file_exists(const char *path);
|
||||
/* This function checks if the path points to a file or directory, assumes existence! */
|
||||
static inline int check_if_file(const char *path);
|
||||
/* This function checks if the path exists and points to a directory. */
|
||||
static inline int check_if_directory_exists(const char *path);
|
||||
/* This function checks if the path exists and points to a file. */
|
||||
static inline int check_if_file_exists(const char *path);
|
||||
|
||||
|
||||
#endif /* FILE_UTILS_H */
|
Loading…
Reference in New Issue