Squashed 'lib/advanced_C_standard_liberary/' content from commit 7f81bd3
git-subtree-dir: lib/advanced_C_standard_liberary git-subtree-split: 7f81bd361e4b3ad0ce89d7052cb448cee5d32a21
This commit is contained in:
68
.gitignore
vendored
Normal file
68
.gitignore
vendored
Normal file
@ -0,0 +1,68 @@
|
||||
# ---> C
|
||||
# Prerequisites
|
||||
*.d
|
||||
|
||||
# Object files
|
||||
*.o
|
||||
*.ko
|
||||
*.obj
|
||||
*.elf
|
||||
|
||||
# Linker output
|
||||
*.ilk
|
||||
*.map
|
||||
*.exp
|
||||
|
||||
# Precompiled Headers
|
||||
*.gch
|
||||
*.pch
|
||||
|
||||
# Libraries
|
||||
*.lib
|
||||
*.a
|
||||
*.la
|
||||
*.lo
|
||||
|
||||
# Shared objects (inc. Windows DLLs)
|
||||
*.dll
|
||||
*.so
|
||||
*.so.*
|
||||
*.dylib
|
||||
|
||||
# Executables
|
||||
*.exe
|
||||
*.out
|
||||
*.app
|
||||
*.i*86
|
||||
*.x86_64
|
||||
*.hex
|
||||
|
||||
# Debug files
|
||||
*.dSYM/
|
||||
*.su
|
||||
*.idb
|
||||
*.pdb
|
||||
|
||||
# Kernel Module Compile Results
|
||||
*.mod*
|
||||
*.cmd
|
||||
.tmp_versions/
|
||||
modules.order
|
||||
Module.symvers
|
||||
Mkfile.old
|
||||
dkms.conf
|
||||
|
||||
# ---> CMake
|
||||
CMakeLists.txt.user
|
||||
CMakeCache.txt
|
||||
CMakeFiles
|
||||
CMakeScripts
|
||||
Testing
|
||||
Makefile
|
||||
cmake_install.cmake
|
||||
install_manifest.txt
|
||||
compile_commands.json
|
||||
CTestTestfile.cmake
|
||||
_deps
|
||||
# ---> Extra
|
||||
vgcore.*
|
||||
11
CMakeLists.txt
Normal file
11
CMakeLists.txt
Normal file
@ -0,0 +1,11 @@
|
||||
project(advanced_C_standard_library C)
|
||||
set_property(GLOBAL PROPERTY C_STANDARD)
|
||||
cmake_minimum_required(VERSION 3.9)
|
||||
include_directories("include")
|
||||
file(GLOB SOURCES "src/*.c")
|
||||
add_executable(test.out ${SOURCES})
|
||||
find_program(CCACHE_PROGRAM ccache)
|
||||
if(CCACHE_PROGRAM)
|
||||
set(CMAKE_C_COMPILER_LAUNCHER "${CCACHE_PROGRAM}")
|
||||
endif()
|
||||
set( CMAKE_EXPORT_COMPILE_COMMANDS ON )
|
||||
19
LICENSE
Normal file
19
LICENSE
Normal file
@ -0,0 +1,19 @@
|
||||
MIT License Copyright (c) 2020 Marcel Zinkel
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is furnished
|
||||
to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice (including the next
|
||||
paragraph) shall be included in all copies or substantial portions of the
|
||||
Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
|
||||
OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
|
||||
OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
3
README.md
Normal file
3
README.md
Normal file
@ -0,0 +1,3 @@
|
||||
# advanced C standard library
|
||||
|
||||
A collection of C functions, I commonly use.
|
||||
5
build_and_run.sh
Executable file
5
build_and_run.sh
Executable file
@ -0,0 +1,5 @@
|
||||
#!/bin/sh
|
||||
rm -rf build
|
||||
cmake .
|
||||
make
|
||||
./build/test.out
|
||||
5
include/array.h
Normal file
5
include/array.h
Normal file
@ -0,0 +1,5 @@
|
||||
#ifndef array_h
|
||||
#define array_h
|
||||
void* acl_arraylist_create(size_t array_size, size_t sizeof_one_element);
|
||||
void* acl_arraylist_append(void *arraylist_void, void *element_void);
|
||||
#endif
|
||||
5
include/file.h
Normal file
5
include/file.h
Normal file
@ -0,0 +1,5 @@
|
||||
#ifndef acl_file_h
|
||||
#define acl_file_h
|
||||
#include <stdbool.h>
|
||||
char* acl_ReadFileString(char *filePath, bool *sucess);
|
||||
#endif
|
||||
37
src/array.c
Normal file
37
src/array.c
Normal file
@ -0,0 +1,37 @@
|
||||
#include <stdio.h>
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
union arraylist_meta {
|
||||
double dummy_double;
|
||||
long double dummy_long_double;
|
||||
long long dummy_long_long;
|
||||
void *dummy_ptr;
|
||||
void (*dummy_func_ptr)(void);
|
||||
struct {
|
||||
size_t len;
|
||||
size_t cap;
|
||||
size_t sizeof_one_element;
|
||||
};
|
||||
};
|
||||
|
||||
void* acl_arraylist_create(size_t array_size, size_t sizeof_one_element) {
|
||||
union arraylist_meta *arraylist_new = malloc(array_size * sizeof_one_element + sizeof*arraylist_new);
|
||||
arraylist_new->len = array_size;
|
||||
arraylist_new->cap = array_size;
|
||||
arraylist_new->sizeof_one_element = sizeof_one_element;
|
||||
return arraylist_new+1;
|
||||
}
|
||||
|
||||
void* acl_arraylist_append(void *arraylist_void, void *element) {
|
||||
union arraylist_meta *arraylist = arraylist_void;
|
||||
--arraylist;
|
||||
if(arraylist->len == arraylist->cap) {
|
||||
arraylist->cap = arraylist->len + 10;
|
||||
arraylist = realloc(arraylist, arraylist->cap * arraylist->sizeof_one_element + sizeof *arraylist);
|
||||
}
|
||||
memcpy((char*)(arraylist + 1) + arraylist->sizeof_one_element * arraylist->len, element, arraylist->sizeof_one_element);
|
||||
++arraylist->len;
|
||||
return arraylist+1;
|
||||
}
|
||||
28
src/file.c
Normal file
28
src/file.c
Normal file
@ -0,0 +1,28 @@
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
char* acl_ReadFileString(char *filePath, bool *sucess){
|
||||
FILE *fp = fopen(filePath, "rb");
|
||||
size_t lSize;
|
||||
char *buffer;
|
||||
if(!fp) {
|
||||
*sucess = false;
|
||||
return buffer;
|
||||
}
|
||||
fseek(fp, 0L, SEEK_END);
|
||||
lSize = ftell(fp);
|
||||
rewind(fp);
|
||||
buffer = malloc(lSize + 1);
|
||||
if(!buffer) {
|
||||
fclose(fp);
|
||||
*sucess = false;
|
||||
return buffer;
|
||||
}
|
||||
size_t readReturn = fread(buffer, lSize, 1, fp);
|
||||
fclose(fp);
|
||||
if(readReturn != 1) {
|
||||
*sucess = false;
|
||||
}
|
||||
*sucess = true;
|
||||
return buffer;
|
||||
}
|
||||
13
src/main.c
Normal file
13
src/main.c
Normal file
@ -0,0 +1,13 @@
|
||||
#include <stdio.h>
|
||||
#include "array.h"
|
||||
#include <stddef.h>
|
||||
#include "file.h"
|
||||
#include <stdbool.h>
|
||||
int main() {
|
||||
bool sucess;
|
||||
char *content = acl_ReadFileString("LICENSE", &sucess);
|
||||
if(sucess) {
|
||||
printf("%s", content);
|
||||
}
|
||||
else printf("Erorror\n");
|
||||
}
|
||||
Reference in New Issue
Block a user