a wrapper around strings
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
CC := gcc
|
CC := gcc
|
||||||
CFLAGS := -Wall -Werror
|
CFLAGS := -Wall -Werror
|
||||||
LIBS := -lvector -lfile -llist -lutil
|
LIBS := -lvector -lfile -llist -lutil -lstring
|
||||||
LIBSPATH := -L/usr/lib/odweta/
|
LIBSPATH := -L/usr/lib/odweta/
|
||||||
INCLUDEDIR := -I/usr/include/odweta/
|
INCLUDEDIR := -I/usr/include/odweta/
|
||||||
PATCH := -Wl,-rpath=/usr/lib/odweta/
|
PATCH := -Wl,-rpath=/usr/lib/odweta/
|
||||||
@@ -28,15 +28,19 @@ libutil.so: src/util.c
|
|||||||
sudo $(CC) -c -fpic $< -o /usr/lib/odweta/util.o
|
sudo $(CC) -c -fpic $< -o /usr/lib/odweta/util.o
|
||||||
sudo $(CC) -shared -o /usr/lib/odweta/$@ /usr/lib/odweta/util.o
|
sudo $(CC) -shared -o /usr/lib/odweta/$@ /usr/lib/odweta/util.o
|
||||||
|
|
||||||
|
libstring.so: src/string.c
|
||||||
|
sudo $(CC) -c -fpic $< -o /usr/lib/odweta/string.o
|
||||||
|
sudo $(CC) -shared -o /usr/lib/odweta/$@ /usr/lib/odweta/string.o
|
||||||
|
|
||||||
install:
|
install:
|
||||||
sudo mkdir -p /usr/include/odweta/
|
sudo mkdir -p /usr/include/odweta/
|
||||||
sudo mkdir -p /usr/lib/odweta/
|
sudo mkdir -p /usr/lib/odweta/
|
||||||
sudo cp src/vector.c src/file.c src/list.c src/util.c include/vector.h include/file.h include/list.h include/util.h /usr/include/odweta/
|
sudo cp src/vector.c src/file.c src/list.c src/util.c src/string.c include/vector.h include/file.h include/list.h include/util.h include/string.h /usr/include/odweta/
|
||||||
sudo make libvector.so libfile.so liblist.so libutil.so
|
sudo make libvector.so libfile.so liblist.so libutil.so libstring.so
|
||||||
sudo ldconfig # update ldconfig cache
|
sudo ldconfig # update ldconfig cache
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
sudo rm -f main /usr/lib/odweta/vector.o /usr/lib/odweta/file.o /usr/lib/odweta/list.o /usr/lib/odweta/util.o /usr/lib/odweta/libvector.so /usr/lib/odweta/libfile.so /usr/lib/odweta/liblist.so /usr/lib/odweta/libutil.so
|
sudo rm -f main /usr/lib/odweta/vector.o /usr/lib/odweta/file.o /usr/lib/odweta/list.o /usr/lib/odweta/util.o /usr/lib/odweta/string.o /usr/lib/odweta/libvector.so /usr/lib/odweta/libfile.so /usr/lib/odweta/liblist.so /usr/lib/odweta/libutil.so /usr/lib/odweta/libstring.so
|
||||||
|
|
||||||
cleanrepo:
|
cleanrepo:
|
||||||
rm -fr ./demo ./moved_copy_of_test.txt ./copy_of_test.txt
|
rm -fr ./demo ./moved_copy_of_test.txt ./copy_of_test.txt
|
||||||
|
|||||||
@@ -1,106 +1,21 @@
|
|||||||
// #include <stdio.h>
|
#include <odweta/string.h>
|
||||||
// #include <odweta/util.h>
|
|
||||||
//
|
|
||||||
// int main() {
|
|
||||||
// char* string = intToString(1729843);
|
|
||||||
//
|
|
||||||
// printf("final string: %s\n", string);
|
|
||||||
//
|
|
||||||
// char* sstring = intToString(123);
|
|
||||||
//
|
|
||||||
// printf("final string: %s\n", sstring);
|
|
||||||
//
|
|
||||||
// return 0;
|
|
||||||
// }
|
|
||||||
|
|
||||||
#include <odweta/file.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
printf("creating a file using 'fileInit(<filename>)' (returns a file_t*)\n");
|
//printf("before init\n");
|
||||||
file_t *f = fileInit("test.txt");
|
string_t *s = string_from("test string");
|
||||||
|
|
||||||
printf("printing the file contents\n\n");
|
//printf("before 1st println\n");
|
||||||
filePrint(f);
|
string_println(s);
|
||||||
|
|
||||||
printf("\nconvering the file contents into a string (char*)\n");
|
//printf("before pushing chars\n");
|
||||||
char *string = fileToString(f);
|
string_push_chars(s, " appended");
|
||||||
printf("\"\"\"\n%s\"\"\"\n", string);
|
|
||||||
|
|
||||||
printf("writing is quite simple, just call the 'fileWrite(file_t*, char*)' "
|
//printf("before 2nd println\n");
|
||||||
"or 'fileWriteln(file_t*, char*)' function, where the second argument "
|
string_println(s);
|
||||||
"is the string you want to write into a file\nthe 'fileWriteln()' "
|
|
||||||
"function writes the specified string AND a '\\n' after it, whereas "
|
|
||||||
"the 'fileWrite()' function does not append the '\\n'\n");
|
|
||||||
fileWrite(f, "'this will not end with a \"\\n\"' ");
|
|
||||||
fileWriteln(f, "this line was appended");
|
|
||||||
filePrint(f);
|
|
||||||
|
|
||||||
printf(
|
//printf("before cleanup\n");
|
||||||
"if you are finished working with the file BEFORE the immediate end of "
|
string_cleanup(s);
|
||||||
"the program, run 'fileCleanup(file_t*)', otherwise it is not strictly "
|
|
||||||
"necessary\nit will, though, append a '\\n' at the end of the file if "
|
|
||||||
"you have not done so, making writing into files more consistent, so for "
|
|
||||||
"this reason, I recommend you use it with every file_t*\n");
|
|
||||||
fileCleanup(f);
|
|
||||||
|
|
||||||
printf("copying and moving\nafter this block of code, the contents of "
|
|
||||||
"test.txt will have been copied into copy_of_test.txt and moved to "
|
|
||||||
"moved_copy_of_test.txt, which means that copy_of_test.txt is deleted "
|
|
||||||
"and the contents of test.txt are only in test.txt and "
|
|
||||||
"moved_copy_of_test.txt\n");
|
|
||||||
|
|
||||||
file_t *src = fileInit("test.txt");
|
|
||||||
|
|
||||||
fileCopy(src, "copy_of_test.txt");
|
|
||||||
|
|
||||||
printf("src:\n");
|
|
||||||
filePrint(src);
|
|
||||||
|
|
||||||
printf("\ndst:\n");
|
|
||||||
file_t *dst = fileInit("copy_of_test.txt");
|
|
||||||
filePrint(dst);
|
|
||||||
|
|
||||||
fileMove(dst, "moved_copy_of_test.txt");
|
|
||||||
|
|
||||||
printf("moved file:\n");
|
|
||||||
file_t *move_dst = fileInit("moved_copy_of_test.txt");
|
|
||||||
filePrint(move_dst);
|
|
||||||
|
|
||||||
fileCleanup(src);
|
|
||||||
printf("DO NOT cleanup a deleted file! (dst, in this case) it is already "
|
|
||||||
"done for you by the 'fileMove(file_t*, char*)' function\n");
|
|
||||||
// fileCleanup(dst);
|
|
||||||
fileCleanup(move_dst);
|
|
||||||
|
|
||||||
printf("when copying and moving files, the first argument is a file_t* "
|
|
||||||
"(source file) and the second argument is a char* (the path of the "
|
|
||||||
"destination file), not a file_t*!\n");
|
|
||||||
|
|
||||||
printf("prepending line numbers at the beggining of each line in a file\n");
|
|
||||||
|
|
||||||
file_t *g = fileInit("test.txt");
|
|
||||||
fileCopy(g, "copy_of_test.txt");
|
|
||||||
file_t *g_copy = fileInit("copy_of_test.txt");
|
|
||||||
fileNumberLines(g_copy);
|
|
||||||
|
|
||||||
printf("file withOUT prepended numbers:\n");
|
|
||||||
filePrint(g);
|
|
||||||
|
|
||||||
printf("file with prepended numbers:\n");
|
|
||||||
filePrint(g_copy);
|
|
||||||
|
|
||||||
fileCleanup(g);
|
|
||||||
// fileCleanup(g_copy);
|
|
||||||
|
|
||||||
printf("counting words in a file\n");
|
|
||||||
|
|
||||||
file_t *words = fileInit("test.txt");
|
|
||||||
|
|
||||||
int word_count = fileCountWords(words);
|
|
||||||
printf("word count of file %s is %d\n", words->path, word_count);
|
|
||||||
|
|
||||||
fileCleanup(words);
|
|
||||||
|
|
||||||
|
//printf("before end\n");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,21 @@
|
|||||||
|
#include <odweta/string.h>
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
//printf("before init\n");
|
||||||
|
string_t *s = string_from("test string");
|
||||||
|
|
||||||
|
//printf("before 1st println\n");
|
||||||
|
string_println(s);
|
||||||
|
|
||||||
|
//printf("before pushing chars\n");
|
||||||
|
string_push_chars(s, " appended");
|
||||||
|
|
||||||
|
//printf("before 2nd println\n");
|
||||||
|
string_println(s);
|
||||||
|
|
||||||
|
//printf("before cleanup\n");
|
||||||
|
string_cleanup(s);
|
||||||
|
|
||||||
|
//printf("before end\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Binary file not shown.
|
After Width: | Height: | Size: 32 KiB |
Binary file not shown.
@@ -1,3 +1,6 @@
|
|||||||
|
#ifndef LIST_H
|
||||||
|
#define LIST_H
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* What is this?
|
* What is this?
|
||||||
*
|
*
|
||||||
@@ -34,3 +37,5 @@ void listRemoveAt(list_t* l, int index);
|
|||||||
void listSetAt(list_t* l, int index, dataType_e type, void* element);
|
void listSetAt(list_t* l, int index, dataType_e type, void* element);
|
||||||
|
|
||||||
void* listGetAt(list_t* l, int index);
|
void* listGetAt(list_t* l, int index);
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
#ifndef STRING_H
|
||||||
|
#define STRING_H
|
||||||
|
|
||||||
|
#include <odweta/util.h>
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
char *content;
|
||||||
|
size_t length;
|
||||||
|
} string_t;
|
||||||
|
|
||||||
|
string_t *string_new(); // create a new empty string
|
||||||
|
|
||||||
|
string_t *string_from(char *chars); // create a new string from a given char_sequence
|
||||||
|
|
||||||
|
bool string_push_chars(string_t *this, char *chars); // append a char_sequence to a string
|
||||||
|
bool string_push(string_t *this, string_t *other); // append a string to a string
|
||||||
|
|
||||||
|
compare_e string_compare(string_t *left, string_t* right); // compare two strings
|
||||||
|
|
||||||
|
void string_print(string_t *this); // print a string
|
||||||
|
void string_println(string_t *this); // print a string with a \n
|
||||||
|
|
||||||
|
bool string_is_empty(string_t *this); // return true when s->content == ""
|
||||||
|
|
||||||
|
void string_cleanup(string_t *this); // memory cleanup
|
||||||
|
|
||||||
|
#endif
|
||||||
+13
-1
@@ -1,6 +1,17 @@
|
|||||||
#ifndef UTIL_H
|
#ifndef UTIL_H
|
||||||
#define UTIL_H
|
#define UTIL_H
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
LESS,
|
||||||
|
LESS_EQUAL,
|
||||||
|
EQUAL,
|
||||||
|
NOT_EQUAL,
|
||||||
|
GREATER,
|
||||||
|
GREATER_EQUAL
|
||||||
|
} compare_e;
|
||||||
|
|
||||||
typedef long unsigned int size_t;
|
typedef long unsigned int size_t;
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
@@ -8,7 +19,8 @@ typedef enum {
|
|||||||
FLOAT = 1,
|
FLOAT = 1,
|
||||||
CHAR = 2,
|
CHAR = 2,
|
||||||
VECTOR = 3, // allows for multi-dimensional arrays
|
VECTOR = 3, // allows for multi-dimensional arrays
|
||||||
LIST = 4
|
LIST = 4,
|
||||||
|
BOOL = 5
|
||||||
} dataType_e;
|
} dataType_e;
|
||||||
|
|
||||||
char* intToString(int x); // convert an int into a string
|
char* intToString(int x); // convert an int into a string
|
||||||
|
|||||||
+123
@@ -0,0 +1,123 @@
|
|||||||
|
#include <odweta/string.h>
|
||||||
|
#include <odweta/util.h>
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
size_t chars_length(char *chars) {
|
||||||
|
int i = 0;
|
||||||
|
while (*(chars + (i++)) != '\0');
|
||||||
|
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
string_t *string_new() {
|
||||||
|
string_t *s = (string_t *)malloc(sizeof(string_t));
|
||||||
|
|
||||||
|
s->content = (char *)malloc(sizeof(char));
|
||||||
|
if (s->content == NULL) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
s->content = &(char){'\0'};
|
||||||
|
s->length = 0;
|
||||||
|
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
string_t *string_from(char *chars) {
|
||||||
|
string_t *s = (string_t *)malloc(sizeof(string_t));
|
||||||
|
s->content = (char *)malloc(sizeof(chars));
|
||||||
|
if (s->content == NULL) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
int i = 0;
|
||||||
|
while (*(chars + i) != '\0') {
|
||||||
|
char *tmp = realloc(s->content, sizeof(s->content) + sizeof(char));
|
||||||
|
if (tmp == NULL) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
s->content = tmp;
|
||||||
|
*(s->content + i) = *(chars + i);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
char *tmp = realloc(s->content, sizeof(s->content) + sizeof(char));
|
||||||
|
if (tmp == NULL) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
s->content = tmp;
|
||||||
|
*(s->content + (++i)) = '\0';
|
||||||
|
|
||||||
|
s->length = (int)chars_length(s->content)-1; // -1 because \0
|
||||||
|
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool string_push_chars(string_t *this, char *chars) {
|
||||||
|
int i = this->length;
|
||||||
|
int j = 0;
|
||||||
|
while (*(chars + j) != '\0') {
|
||||||
|
char *tmp = realloc(this->content, sizeof(this->content) + sizeof(char));
|
||||||
|
if (tmp == NULL) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
this->content = tmp;
|
||||||
|
*(this->content + i) = *(chars + j);
|
||||||
|
i++;
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
|
||||||
|
this->length = (int)chars_length(this->content)-1;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool string_push(string_t *this, string_t* to_be_pushed) {
|
||||||
|
return string_push_chars(this, to_be_pushed->content);
|
||||||
|
}
|
||||||
|
|
||||||
|
compare_e string_compare(string_t* left, string_t *right) {
|
||||||
|
int i = 0;
|
||||||
|
while (
|
||||||
|
*((left->content) + i) != '\0' ||
|
||||||
|
*((right->content) + i) != '\0'
|
||||||
|
) {
|
||||||
|
if (*((left->content) + i) == *((right->content) + i)) {
|
||||||
|
i++;
|
||||||
|
continue;
|
||||||
|
} else {
|
||||||
|
return NOT_EQUAL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return EQUAL;
|
||||||
|
}
|
||||||
|
|
||||||
|
void string_print(string_t *this) {
|
||||||
|
printf("%s", this->content);
|
||||||
|
}
|
||||||
|
|
||||||
|
void string_println(string_t *this) {
|
||||||
|
printf("%s\n", this->content);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool string_is_empty(string_t *this) {
|
||||||
|
compare_e cmp = string_compare(this, string_from(""));
|
||||||
|
switch (cmp) {
|
||||||
|
case EQUAL:
|
||||||
|
return true;
|
||||||
|
case NOT_EQUAL:
|
||||||
|
return false;
|
||||||
|
default:
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void string_cleanup(string_t *this) {
|
||||||
|
free(this->content);
|
||||||
|
free(this);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user