initial commit
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
#include <odweta/vector.h>
|
||||
#include <odweta/file.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
printf("demonstration of the power of dynamically allocated arrays in c:\n-----------\n\n");
|
||||
|
||||
printf("initializing an integer vector\n");
|
||||
vector_t *v = vectorInit(INT);
|
||||
|
||||
printf("vector after initialization\n");
|
||||
vectorPrint(v);
|
||||
|
||||
printf("pushing an element of value 1 into vector\n");
|
||||
int element = 1;
|
||||
vectorPush(v, &element);
|
||||
vectorPrint(v);
|
||||
|
||||
printf("pushing another element (value 2) into vector\n");
|
||||
element = 2;
|
||||
vectorPush(v, &element);
|
||||
vectorPrint(v);
|
||||
|
||||
printf("inserting 3 at index 1\n");
|
||||
element = 3;
|
||||
vectorInsertAt(v, 1, &element);
|
||||
vectorPrint(v);
|
||||
|
||||
printf("popping the last value from the vector\n");
|
||||
int popped = *((int *)vectorPop(v));
|
||||
printf("popped: %d\n", popped);
|
||||
printf("vector after popping\n");
|
||||
vectorPrint(v);
|
||||
|
||||
printf("initializing a second vector of type vector_t (multidimensional vector => vector of vectors)\n");
|
||||
vector_t *w = vectorInit(VECTOR);
|
||||
|
||||
printf("vector after initialization\n");
|
||||
vectorPrint(w);
|
||||
|
||||
printf("pushing the first vector into the newly created one\n");
|
||||
vectorPush(w, v);
|
||||
vectorPrint(w);
|
||||
|
||||
printf("initializing a char vector (variable-length string) with value \"Hello, world!\"\n");
|
||||
vector_t *charVector = vectorInit(CHAR);
|
||||
char *word = "Hello, world!";
|
||||
for (int i = 0; i < strlen(word); i++) {
|
||||
vectorPush(charVector, &word[i]);
|
||||
}
|
||||
printf("char vector as string\n");
|
||||
vectorPrintAsString(charVector);
|
||||
|
||||
printf("char vector converted into a regular string\n");
|
||||
char *newWord = vectorToString(charVector);
|
||||
printf("new word: %s\n", newWord);
|
||||
|
||||
printf("setting a value in the char vector on index 10 to \"\" (nothing, effectively deleting the character, although the size of the vector stays the same!)\n");
|
||||
vectorSetAt(charVector, 10, ""); // should change the vector to "Hello, word!" (size stays the same!)
|
||||
vectorPrintAsString(charVector);
|
||||
|
||||
printf("files section\n---------------\n");
|
||||
printf("initializing a file called test.txt with mode \"r\" (reading it)\n");
|
||||
file_t* f = fileInit("test.txt", "r");
|
||||
|
||||
printf("printing the file contents\n");
|
||||
filePrint(f);
|
||||
printf("file length (line count): %d\n", f->length);
|
||||
printf("file as vector\n");
|
||||
vectorPrint(f->lines);
|
||||
printf("file to string\n");
|
||||
char* strFile = fileToString(f);
|
||||
printf("%s", strFile);
|
||||
|
||||
printf("cleaning up vectors\n");
|
||||
vectorCleanup(v);
|
||||
vectorCleanup(w);
|
||||
vectorCleanup(charVector);
|
||||
|
||||
printf("cleaning up files\n");
|
||||
fileCleanup(f);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user