I'm working on some c code that dynamically allocates memory to an array of strings with a struct.
It basically can be initilized by DSA_INIT(name_of_variable) and strings can be appended with name_of_variable = arrcpy_str(name_of_variable, "some text"); and will append to the string, here is a small example:
DSA_INIT(test)
test = arrcpy_str(test, "hello world!");
// test.size is the amount of strings in the array, if test.size > 0 then it has values stored in it.
printf("Last value: %s\nLength: %d\n", test.contents[test.size - 1], test.size);
test = arrcpy_str(test, "test!");
printf("Last value: %s\nLength: %d\n", test.contents[test.size - 1], test.size);
Output:
Last value: hello world!
Length: 1
Last value: test!
Length: 2
As expected, but sometimes, quite bizarrely it breaks... I was writing another function for the windows operating system, it's quite complicated so instead of posting like 200 lines of code I'll just summarize what it does (in the parts where there are no issues):
os_listdir takes 2 parameters, the directory, and the index. it returns a char * to the index in alphabetical order starting at one of the directory.
e.g. C:\Users\usrname\Desktop contains 2 files: named a.txt and b.txt.
os_listdir("C:\Users\usrname\Desktop", 1) = ".",
os_listdir("C:\Users\usrname\Desktop", 2) = "..",
os_listdir("C:\Users\usrname\Desktop", 3) = "a.txt" and finally,
os_listdir("C:\Users\usrname\Desktop", 4) = "b.txt"
os_listdir returns "N/A" if the directory cannot be found or there are not enough files to get the index.
length is the same as strlen from string.h and strequals is the same as strcmp from string.h
os_isdir returns true if it is a directory and concat concatenates 2 strings. rstrip and replace work like pythons rstrip and replace (pretty self-explanatory for the rest ig)
My code doesnt seem to work however in this, for no seemingly reason.
array source:
struct DYNAMIC_STRING_ARRAY {
char ** contents;
int size;
};
#define DSA struct DYNAMIC_STRING_ARRAY
#define DSA_INIT(DSA_to_initilize) DSA DSA_to_initilize; DSA_to_initilize.size = 0;
/* Returns length of a string excluding null terminator */
int length(const char * __s) {
int i;
for (i = 0; __s[i] != '\0'; ++i);
return i;
}
DSA arrcpy_str(DSA __a, char * to_add) {
DSA __arr = __a;
// It stops right below this line - it works normally but for some reason it stops in this function only:
// Even if i try to do something like directory_list = arrcpy_str(directory_list, "test"); it still wont work.
__arr.contents[__arr.size] = (char *) malloc(length(to_add) * sizeof(char));
__arr.contents[__arr.size] = to_add;
__arr.size++;
return __arr;
}
DSA arrcpy(DSA __a, DSA __add) {
DSA __arr = __a;
for (int i = 0; i < __add.size; i++) {
__arr.contents[__arr.size] = (char *) malloc(length(__add.contents[i]) * sizeof(char));
__arr.contents[__arr.size + i] = __add.contents[i];
__arr.size++;
}
return __arr;
}
and finally the os_listalldir source that is giving me trouble:
DSA os_listalldir(char * _path) {
char * path = rstrip(replace(_path, '/', '\\'), '\\');
DSA_INIT(directory_list)
directory_list = arrcpy_str(directory_list, "test");
if (!os_isdir(path)) {
free(path);
return directory_list;
}
int i = 1;
char * c_directory = os_listdir(path, i);
while (!strequals(c_directory, "N/A")) {
i++;
if (!strequals(c_directory, "..") && !strequals(c_directory, ".")) {
char * full_path = concat(path, concat("\\", c_directory));
// This line below is what breaks the code - refer to the source above to see where it actually stops
directory_list = arrcpy_str(directory_list, full_path);
if (os_isdir(full_path)) {
directory_list = arrcpy(directory_list, os_listalldir(full_path));
}
printf("%s\n", full_path);
}
c_directory = os_listdir(path, i);
}
free(path);
return directory_list;
}
The main problem being it stops running completely after the malloc part in arrcpy_str for some reason, I was wondering if someone could help me figure out why (it sometimes works but it sometimes doesn't??) however in the os_listalldir function it always stops running at that one commented line.