1
1
mirror of https://github.com/theoludwig/libcproject.git synced 2025-05-21 23:21:15 +02:00

5 Commits

4 changed files with 250 additions and 71 deletions

View File

@ -20,7 +20,7 @@
C is a low-level programming language and we often end up reinventing the wheel as the C standard library (`libc`) is quite small and in my humble opinion, not well designed. C is a low-level programming language and we often end up reinventing the wheel as the C standard library (`libc`) is quite small and in my humble opinion, not well designed.
**libcproject** solve this by providing common functions or data structures (`hash_map`, `array_list`, `linked_list`, `queue`, `stack`, etc.) we might need in our C projects. **libcproject** solve this by providing common functions or data structures (`hash_map`, `array_list`, `linked_list`, `queue`, `stack`, etc.), we might need in our C projects.
[Online documentation](https://libcproject.vercel.app/). [Online documentation](https://libcproject.vercel.app/).
@ -36,7 +36,10 @@ C is a low-level programming language and we often end up reinventing the wheel
For example on GNU/Linux Ubuntu: For example on GNU/Linux Ubuntu:
```sh ```sh
# Install Build Tools
sudo apt-get install build-essential gcc make clang-format sudo apt-get install build-essential gcc make clang-format
# Install Documentation Tools
sudo apt-get install doxygen doxygen-gui doxygen-doc graphviz sudo apt-get install doxygen doxygen-gui doxygen-doc graphviz
``` ```
@ -55,7 +58,14 @@ nm ./build/libcproject.a # to see the symbols
Steps to create a new C project that uses `libcproject`: Steps to create a new C project that uses `libcproject`:
### Step 1: Install and Compile `libcproject` ### Step 1: Create a new project
```sh
mkdir my-project
cd my-project
```
### Step 2: Install and Compile `libcproject` in the project
```sh ```sh
# Clone the repository # Clone the repository
@ -68,25 +78,10 @@ cd libcproject
make make
``` ```
### Step 2: Create a new project ### Step 3: Create a new C file
```sh
mkdir my-project
cd my-project
```
### Step 3: Install `libcproject` in the project
```sh
mkdir libcproject
cp --recursive <path-to-libcproject> ./ # copy
# or
ln -s <path-to-libcproject> ./ # symbolic link
```
### Step 4: Create a new C file
```sh ```sh
cd ..
touch main.c touch main.c
``` ```
@ -104,7 +99,7 @@ int main() {
} }
``` ```
### Step 5: Compile your project and link it with the library ### Step 4: Compile your project and link it with the library
```sh ```sh
gcc -o ./main ./main.c -L. -l:./libcproject/build/libcproject.a gcc -o ./main ./main.c -L. -l:./libcproject/build/libcproject.a

View File

@ -27,63 +27,224 @@ int filesystem_write(string_t path, byte_t *file_content, off_t file_size) {
return 0; return 0;
} }
bool filesystem_exists(string_t path) {
return access(path, F_OK) == 0;
}
int filesystem_remove(string_t path) {
return remove(path);
}
string_t filesystem_get_mimetype(string_t path) { string_t filesystem_get_mimetype(string_t path) {
if (string_ends_with(path, ".html")) { if (string_ends_with(path, ".aac")) {
return "text/html"; return "audio/aac";
} }
if (string_ends_with(path, ".css")) { if (string_ends_with(path, ".abw")) {
return "text/css"; return "application/x-abiword";
} }
if (string_ends_with(path, ".js")) { if (string_ends_with(path, ".arc")) {
return "text/javascript"; return "application/x-freearc";
} }
if (string_ends_with(path, ".png")) { if (string_ends_with(path, ".avif")) {
return "image/png"; return "image/avif";
} }
if (string_ends_with(path, ".jpg") || string_ends_with(path, ".jpeg")) { if (string_ends_with(path, ".avi")) {
return "image/jpeg"; return "video/x-msvideo";
} }
if (string_ends_with(path, ".gif")) { if (string_ends_with(path, ".azw")) {
return "image/gif"; return "application/vnd.amazon.ebook";
}
if (string_ends_with(path, ".ico")) {
return "image/x-icon";
}
if (string_ends_with(path, ".svg")) {
return "image/svg+xml";
}
if (string_ends_with(path, ".json")) {
return "application/json";
}
if (string_ends_with(path, ".pdf")) {
return "application/pdf";
}
if (string_ends_with(path, ".xml")) {
return "application/xml";
}
if (string_ends_with(path, ".csv")) {
return "text/csv";
}
if (string_ends_with(path, ".zip")) {
return "application/zip";
}
if (string_ends_with(path, ".tar")) {
return "application/x-tar";
}
if (string_ends_with(path, ".gz")) {
return "application/x-gzip";
}
if (string_ends_with(path, ".mp3")) {
return "audio/mpeg";
}
if (string_ends_with(path, ".wav")) {
return "audio/wav";
}
if (string_ends_with(path, ".mp4")) {
return "video/mp4";
} }
if (string_ends_with(path, ".bin")) { if (string_ends_with(path, ".bin")) {
return "application/octet-stream"; return "application/octet-stream";
} }
if (string_ends_with(path, ".bmp")) {
return "image/bmp";
}
if (string_ends_with(path, ".bz")) {
return "application/x-bzip";
}
if (string_ends_with(path, ".bz2")) {
return "application/x-bzip2";
}
if (string_ends_with(path, ".cda")) {
return "application/x-cdf";
}
if (string_ends_with(path, ".csh")) {
return "application/x-csh";
}
if (string_ends_with(path, ".css")) {
return "text/css";
}
if (string_ends_with(path, ".csv")) {
return "text/csv";
}
if (string_ends_with(path, ".doc")) {
return "application/msword";
}
if (string_ends_with(path, ".docx")) {
return "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
}
if (string_ends_with(path, ".eot")) {
return "application/vnd.ms-fontobject";
}
if (string_ends_with(path, ".epub")) {
return "application/epub+zip";
}
if (string_ends_with(path, ".gz")) {
return "application/gzip";
}
if (string_ends_with(path, ".gif")) {
return "image/gif";
}
if (string_ends_with(path, ".htm") || string_ends_with(path, ".html")) {
return "text/html";
}
if (string_ends_with(path, ".ico")) {
return "image/vnd.microsoft.icon";
}
if (string_ends_with(path, ".ics")) {
return "text/calendar";
}
if (string_ends_with(path, ".jar")) {
return "application/java-archive";
}
if (string_ends_with(path, ".jpg") || string_ends_with(path, ".jpeg") || string_ends_with(path, ".jpe") || string_ends_with(path, ".jif") || string_ends_with(path, ".jfif") || string_ends_with(path, ".jfi")) {
return "image/jpeg";
}
if (string_ends_with(path, ".js") || string_ends_with(path, ".mjs") || string_ends_with(path, ".cjs")) {
return "text/javascript";
}
if (string_ends_with(path, ".json")) {
return "application/json";
}
if (string_ends_with(path, ".jsonld")) {
return "application/ld+json";
}
if (string_ends_with(path, ".mid") || string_ends_with(path, ".midi")) {
return "audio/midi";
}
if (string_ends_with(path, ".mp3")) {
return "audio/mpeg";
}
if (string_ends_with(path, ".mp4")) {
return "video/mp4";
}
if (string_ends_with(path, ".mpeg")) {
return "video/mpeg";
}
if (string_ends_with(path, ".mpkg")) {
return "application/vnd.apple.installer+xml";
}
if (string_ends_with(path, ".odp")) {
return "application/vnd.oasis.opendocument.presentation";
}
if (string_ends_with(path, ".ods")) {
return "application/vnd.oasis.opendocument.spreadsheet";
}
if (string_ends_with(path, ".odt")) {
return "application/vnd.oasis.opendocument.text";
}
if (string_ends_with(path, ".oga")) {
return "audio/ogg";
}
if (string_ends_with(path, ".ogv")) {
return "video/ogg";
}
if (string_ends_with(path, ".ogx")) {
return "application/ogg";
}
if (string_ends_with(path, ".opus")) {
return "audio/opus";
}
if (string_ends_with(path, ".otf")) {
return "font/otf";
}
if (string_ends_with(path, ".png")) {
return "image/png";
}
if (string_ends_with(path, ".pdf")) {
return "application/pdf";
}
if (string_ends_with(path, ".php")) {
return "application/x-httpd-php";
}
if (string_ends_with(path, ".ppt")) {
return "application/vnd.ms-powerpoint";
}
if (string_ends_with(path, ".pptx")) {
return "application/vnd.openxmlformats-officedocument.presentationml.presentation";
}
if (string_ends_with(path, ".rar")) {
return "application/vnd.rar";
}
if (string_ends_with(path, ".rtf")) {
return "application/rtf";
}
if (string_ends_with(path, ".sh")) {
return "application/x-sh";
}
if (string_ends_with(path, ".svg")) {
return "image/svg+xml";
}
if (string_ends_with(path, ".tar")) {
return "application/x-tar";
}
if (string_ends_with(path, ".tif") || string_ends_with(path, ".tiff")) {
return "image/tiff";
}
if (string_ends_with(path, ".ts")) {
return "application/typescript";
}
if (string_ends_with(path, ".ttf")) {
return "font/ttf";
}
if (string_ends_with(path, ".txt")) {
return "text/plain"; return "text/plain";
}
if (string_ends_with(path, ".vsd")) {
return "application/vnd.visio";
}
if (string_ends_with(path, ".wav")) {
return "audio/wav";
}
if (string_ends_with(path, ".weba") || string_ends_with(path, ".webm")) {
return "audio/webm";
}
if (string_ends_with(path, ".webp")) {
return "image/webp";
}
if (string_ends_with(path, ".woff")) {
return "font/woff";
}
if (string_ends_with(path, ".woff2")) {
return "font/woff2";
}
if (string_ends_with(path, ".xhtml")) {
return "application/xhtml+xml";
}
if (string_ends_with(path, ".xls")) {
return "application/vnd.ms-excel";
}
if (string_ends_with(path, ".xlsx")) {
return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
}
if (string_ends_with(path, ".xml")) {
return "application/xml";
}
if (string_ends_with(path, ".xul")) {
return "application/vnd.mozilla.xul+xml";
}
if (string_ends_with(path, ".zip")) {
return "application/zip";
}
if (string_ends_with(path, ".3gp")) {
return "video/3gpp";
}
if (string_ends_with(path, ".3g2")) {
return "video/3gpp2";
}
if (string_ends_with(path, ".7z")) {
return "application/x-7z-compressed";
}
return "application/octet-stream";
} }

View File

@ -38,9 +38,32 @@ int filesystem_read(string_t path, byte_t **file_content, off_t *file_size);
int filesystem_write(string_t path, byte_t *file_content, off_t file_size); int filesystem_write(string_t path, byte_t *file_content, off_t file_size);
/** /**
* @brief Get the mimetype of a file. * @brief Check if a path exists.
* *
* @param path * @param path
* @retval true if the path exists.
* @retval false if the path does not exist.
* @since v3.1.0
*/
bool filesystem_exists(string_t path);
/**
* @brief Removes a path.
*
* @param path
* @return int
* @retval -1 if there is an error.
* @retval 0 for success.
* @since v3.1.0
*/
int filesystem_remove(string_t path);
/**
* @brief Get the mimetype of a file based on its extension.
*
* @param path
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
* @see https://www.iana.org/assignments/media-types/media-types.xhtml
* @since v1.0.0 * @since v1.0.0
*/ */
string_t filesystem_get_mimetype(string_t path); string_t filesystem_get_mimetype(string_t path);

View File

@ -1,4 +1,4 @@
#ifndef __LIBCPROJECT_VERSION__ #ifndef __LIBCPROJECT_VERSION__
#define __LIBCPROJECT_VERSION__ "3.0.0" #define __LIBCPROJECT_VERSION__ "3.1.0"
#endif #endif