Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

moving hashtable to cutils #102

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
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 All @@ -24,14 +21,14 @@
}section;


unsigned int getsections(char* path,section*** sectionq);

Check warning on line 24 in formats/ecmp/ecmp.c

View workflow job for this annotation

GitHub Actions / c-linter

formats/ecmp/ecmp.c:24:14 [readability-inconsistent-declaration-parameter-name]

function 'getsections' has a definition with different parameter names

unsigned int parseinfo(char* s, struct package* dest);
unsigned int parseraw(char* s, char** dest);
unsigned int parsenl(char* s,char*** dest);

hashtable* hm;

Check warning on line 30 in formats/ecmp/ecmp.c

View workflow job for this annotation

GitHub Actions / c-linter

formats/ecmp/ecmp.c:30:12 [cppcoreguidelines-avoid-non-const-global-variables]

variable 'hm' is non-const and globally accessible, consider making it const

Check warning on line 30 in formats/ecmp/ecmp.c

View workflow job for this annotation

GitHub Actions / c-linter

formats/ecmp/ecmp.c:30:12 [cppcoreguidelines-avoid-non-const-global-variables]

variable 'hm' provides global access to a non-const object; consider making the pointed-to data 'const'
hashtable* infohm;

Check warning on line 31 in formats/ecmp/ecmp.c

View workflow job for this annotation

GitHub Actions / c-linter

formats/ecmp/ecmp.c:31:12 [cppcoreguidelines-avoid-non-const-global-variables]

variable 'infohm' is non-const and globally accessible, consider making it const

Check warning on line 31 in formats/ecmp/ecmp.c

View workflow job for this annotation

GitHub Actions / c-linter

formats/ecmp/ecmp.c:31:12 [cppcoreguidelines-avoid-non-const-global-variables]

variable 'infohm' provides global access to a non-const object; consider making the pointed-to data 'const'

#ifdef STATIC
int open_ecmp(char* path,struct package* pkg)
Expand Down Expand Up @@ -96,9 +93,9 @@



unsigned int (*parser)(char* s,void* dest);

Check warning on line 96 in formats/ecmp/ecmp.c

View workflow job for this annotation

GitHub Actions / c-linter

formats/ecmp/ecmp.c:96:17 [cppcoreguidelines-init-variables]

variable 'parser' is not initialized

section** sections;

Check warning on line 98 in formats/ecmp/ecmp.c

View workflow job for this annotation

GitHub Actions / c-linter

formats/ecmp/ecmp.c:98:12 [cppcoreguidelines-init-variables]

variable 'sections' is not initialized
uint count = getsections(path,&sections);

for (unsigned int i = 0; i < count; i++) {
Expand Down Expand Up @@ -141,7 +138,7 @@

unsigned int parsenl(char* s,char*** dest)
{
char* str;

Check warning on line 141 in formats/ecmp/ecmp.c

View workflow job for this annotation

GitHub Actions / c-linter

formats/ecmp/ecmp.c:141:8 [cppcoreguidelines-init-variables]

variable 'str' is not initialized
// the parseraw below is useless but i'll keep since in case
parseraw(s,&str);
return splita(str,'\n',dest);
Expand All @@ -165,14 +162,14 @@
unsigned int index = p - s;
// ^ this could be a long, but it doesn't need to be 64 bits
dbg(3, "Removing space '%c']'%c'['%c' at %p (index=%d)", *(p - 1), *p, *(p + 1), p, index);
popchar(s, index);

Check warning on line 165 in formats/ecmp/ecmp.c

View workflow job for this annotation

GitHub Actions / c-linter

formats/ecmp/ecmp.c:165:13 [clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling]

Call to function 'memmove' is insecure as it does not provide security checks introduced in the C11 standard. Replace with analogous functions that support length arguments or provides boundary checks such as 'memmove_s' in case of C11
p--;
}
p++;
}

// split with nl
char** nlist;

Check warning on line 172 in formats/ecmp/ecmp.c

View workflow job for this annotation

GitHub Actions / c-linter

formats/ecmp/ecmp.c:172:12 [cppcoreguidelines-init-variables]

variable 'nlist' is not initialized
int count = parsenl(s, &nlist);

dbg(3, "count : %d", count);
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
137 changes: 0 additions & 137 deletions src/hashtable.c

This file was deleted.

2 changes: 1 addition & 1 deletion src/move.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ void move_binaries(char** locations, long loc_size) {
}

// Move the files from the build directory to the destination location
switch (mvsp(build_loc, dest_loc, getenv("SOVIET_BUILD_DIR")))
switch (mvsp(build_loc, dest_loc))
{
case -1:
msg(FATAL, "Moving %s to %s failed, could not create dir", build_loc, dest_loc);
Expand Down
12 changes: 5 additions & 7 deletions src/update.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,12 @@ int update()
free(files_array);


if(new_version_found != 0)
{
msg(WARNING, "new version found for one or more packages, use --upgrade to upgrade");
if(new_version_found != 0) {
msg(WARNING, "New version found for one or more packages, use --upgrade to upgrade");
}
else {
msg(WARNING, "all packages are up to date");
}
else
{
msg(WARNING, "all packages are up to date");
}

return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion test/assets/split.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Ldg5En7J5,wK4haKNq21CLxQAKJpe9lgbe2tjHBg7DmL9CAt7j8LqXJ8zykW8gMdCU0TpFryahJksx9oZUheaaTByxEOznQPwnb23uNecK8rakjLtRhxjgcl4pTgda5s4DPqNvITQJfcIB1sja9GsCfYUf7K9CmT,m,l1nXLYkTv2pMEwPOlHY6QmhFUrfsivAU11GnQiEL73X9IibIpY7MMC8djqqZ2NzSufmaSWP7gU1LUjv1,bgmbpXnM8FSdCG61ioC7zvbOzd5DPRKCryKMlxezsvMCTnzYhXkSlKzy5y77RLr6mrA4haWUyr8ZbrayoRjCjEZCvyMgzOcD6ETRthgkqmGGUy6oqYyNgJBMLpEZsAmhIcKa45KWMRGShvGNKuK4upc43BEW,sjwCUo4ykb2h6v,NETEaX9V2g0JgXI45qwiruNAGeTsPf5fSXGskdLFof1OkBnabL53HLL5CWteosxyVTQFmoBeFqpbOzC69kSslJO45eKpBse6
oVil0JdC1BlYeL07Fv8VIwy2LBSxQxAHuXRBYfsNx2UbiBbBrli4ydwhwvd08CTnHxsdmmqG2WSqSnE33d3IsalyOrljQWXh8zUP8TgNP,EzszJSkt1CVWJ899MCNkQCCePjIfrLQNVaUuZEkWAi0X2UrzV3Pvz4fPVebdgENIF6ld,8Fccg9PT,h2MCth6JvahLEKwtLnsg98zz1DJhjmQb29EbAotjUNgHjLIORR6BuHCwbmWoj3whfVuqTPP,1MDxpKQ,6Z5GfHX3GQBhLBna,a24DvMFd4mwlD7LEEQvzhRBjD4AQFsQvvgl735AAe,W4IxFjfNmHPjFIx1iO5M0D0vLCQgZBnJTGuiWbZUa5UFLDrT,RSuvj20HLTqXXrBQow4WlqU,JyWgjBArm,Y0w7o6lUdBCHzIWR4BfgA3KH1DBTqXwnpBqEMZPDHqcmU9bIP8PEtt7xbkQDVS4HbUd6aQegldT0a2YL0xQPiX7sJylECkcLFk0GGkKvNv
3 changes: 2 additions & 1 deletion test/spm.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "test.h"
#include <threads.h>
#include "../include/libspm.h"
#include "../include/cutils.h"

char WORKING_DIR[2048];

Expand Down
2 changes: 2 additions & 0 deletions test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ void test_pm(char* spm_path) {
init() ;

assert(install_package_source(spm_path,0) == 0);
assert(check("vim") == 0);
assert(uninstall("vim") == 0);
assert(check("vim") == 1);

unsetenv("SOVIET_ROOT");
unsetenv("SOVIET_ROOT");
Expand Down
Loading