Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions formats/ecmp/ecmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@
#include "stdlib.h"
#include "string.h"
#include "unistd.h"
#include "malloc.h"


#include "../../include/libspm.h"
#include "../../include/cutils.h"

#include "../../include/hashtable.h"
#include <stdio.h>

#define uint unsigned int


Expand Down
82 changes: 78 additions & 4 deletions include/cutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ The CUtils Library is a collection of C functions.
It can be used for any C project, but it was originally made for the Libspm/CCCP project.
It is licensed under the GNU General Public License v3.0.

* Copyright (C) 2019-2020 PKD <pkd@sovietlinxu.ml>
* Copyright (C) 2019-2024 PKD <pkd@sovietlinux.ml>

*/
#include "string.h"
Expand Down Expand Up @@ -41,18 +41,22 @@ long rdfile(const char* filePath,char** buffer);
// write entire file safely
int wrnfile(const char* filePath,char* buffer,long size);
#define wrfile(filePath,buffer) wrnfile(filePath,buffer,strlen(buffer))

/*
Check if a dir exists :
* 0 - doesn't exist
* 1 - exists
* 2 - Not a directory
*/
int isdir (const char *d);

// create dir recursivelty (similar to mkdir -p)
int pmkdir (const char *dir);
// move a file and create the dir if it doesn't exist
int mvsp(char* old_path,char* new_path,char* root);
int mvsp(char* old_path,char* new_path);
// move a symlink
int mvlink(char* old_path,char* new_path);
// get the relative path between two paths
char* relpath(char* start,char* end);
// LIST file in a dir
char** ls(char* path);
// exec a shell command and return the output
Expand All @@ -63,9 +67,9 @@ String utils
Functions :
* popchar - Remove a character from a string
* popcharn - Remove a character from a string (with a size limit)
* s_size should be a size_t (=long unsigned int)
* splita - Split a string into an array of strings
* countc - Count the number of occurences of a char in a string
* strinarr - Check if a string is in an array of strings
*/

#define popcharn(str,pos,s_size) if (pos < s_size) { memmove(&str[pos], &str[pos + 1], s_size - pos - 1); str[s_size-1] = '\0'; }
Expand All @@ -77,6 +81,9 @@ unsigned int splita (char* string,char delim,char*** dest);
// to count the number of occurences of a char in a string
unsigned int countc(const char* string,char c);

// check if a string is in an array of strings
int strinarr( char* val, char** arr,long arrsize);

/*
Logging and debug utils
Functions :
Expand All @@ -102,6 +109,73 @@ int f_dbg__(int level,int line,const char* function,const char* file,char* messa
#define dbg(level,message,...) f_dbg__(level,__LINE__,__func__,__FILE__,message,##__VA_ARGS__)


/* Hashtable (Dict) implementation */

// A pair of values :
// * key : the key of the pair (in bytes)
// * value : the value of the pair (pointer to anything)
typedef struct pair {
char* key;
void* value;
} pair;

// An item in the hashtable :
// * data : the pairs of the item
// * size : the number of pairs in the item
// * capacity : the capacity of the item
typedef struct {
pair* data;
int size;
int capacity;
} item;

// The hashtable :
// * items : the items in the hashtable
// * capacity : the capacity of the hashtable
typedef struct {
item *items;
int capacity;
} hashtable;

// create a new hashtable
// * capacity : the capacity of the hashtable you want
hashtable *hm_create(int capacity);
// destroy a hashtable (free the memory)
// * hm : the hashtable you want to destroy
void hm_destroy(hashtable *hm);
// add a pair to the hashtable
// * hm : the hashtable you want to add the pair to
// * key : the key of the pair
// * value : the value of the pair
// Equivalent to hm[key] = value
int hm_add(hashtable *hm, char *key, void *value);
// get a value from the hashtable
// * hm : the hashtable you want to get the value from
// * key : the key of the pair
// Equivalent to hm[key]
void* hm_get(hashtable *hm, char *key);
// remove a pair from the hashtable
// * hm : the hashtable you want to remove the pair from
// * key : the key of the pair
// Equivalent to del hm[key]
int hm_rm(hashtable *hm, char *key);
// visualize the hashtable
// basic pretty print of the hashtable
// * hm : the hashtable you want to visualize
int hm_visualize(hashtable *hm);
// initialize a hashtable with a list of key-value pairs
// * kvlist : the list of key-value pairs
// * size : the size of the list
hashtable* hm_init(void* kvlist[][2],int size);
// get the hash of a key
// * hm : the hashtable you want to get the hash from
// * key : the key you want to get the hash of
// WARNING : Used only internally
unsigned int hm_hash(hashtable *hm, char *key);;




// memory safety and debugging
void* dbg_malloc(size_t size,char* file,int line);
void* dbg_calloc(size_t nmemb,size_t size,char* file,int line);
Expand Down
26 changes: 0 additions & 26 deletions include/hashtable.h

This file was deleted.

1 change: 1 addition & 0 deletions include/libspm.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "cutils.h"
#include "globals.h"
#include <stdio.h>



Expand Down
2 changes: 1 addition & 1 deletion lib/cutils
Submodule cutils updated 3 files
+ cutils.a
+68 −1 cutils.h
+136 −0 src/hashtable.c
3 changes: 2 additions & 1 deletion makefile
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ check: test check-all

libs:
for i in $(LOCAL_LIBS); do make -C $$(dirname $$i) all; done
for i in $(LOCAL_LIBS); do cp $$(dirname $$i)/*.h include ; done

direct:
$(CC) $(CFLAGS) $(SRCS) $(LIBS) -g -shared -fPIC -o $(LIBOUT)
Expand All @@ -96,7 +97,7 @@ formats:
for i in $(FMT_DIR)/*; do \
echo "Building $$i"; \
if [ -d $$i ]; then \
$(CC) $(CFLAGS) -shared -fPIC $$i/*.c -o $(BINDIR)/plugins/$$(basename $$i).so; \
$(CC) $(CFLAGS) -shared -fPIC $$i/*.c $(LOCAL_LIBS) -o $(BINDIR)/plugins/$$(basename $$i).so; \
fi; \
done

Expand Down
Loading