First of all, this was my first experiment with void pointers and pointers to functions. I hope that is useful for some purpose of someone. This work is still in progress and development.
Second, just explaining, C language don't really have object. So this code is not really a class, resembling more a simulation of a class object, and if you ask me if it's safe to use it on any system I'd say no haha but i'm still testing and optimizing to be feasible one day, still testing. So, be kind :D
I want to thank my friend Marcos Heitor for having helped me understand the operation of void pointers and pointer to function, thus enabling the execution of creating this code.
Let's see how a basic code works and i'll explain in your due time.
#include <stdio.h>
#include "string.c" // The Library
int main(int argc, char const *argv[]){
string x = newString();
// (YOUR CODE)
destroyString(&x);
}
If you didn't understand, calm down and breathe, i'll explain right about now.
So, as you can see, we create a variable called 'x' of the type "string". Then, before we can use it as a object, we have to set all the pointers that this variable contains. That's why we use string x = newString()
. This way, the newString();
function will do all the dirty work to set all pointers and variables inside of our struct.
If you're familiar with constructor and 'new' stuffs, you should know that you will need to use thedeleteString(&x)
to release memory.
Note: Still it is necessary to send the address of the structure (Ex.: x.read(&x,0), as you can see, we must send '&x' as parameter even though we're using the function of the class of that same variable)
Non-object Functions | Information |
---|---|
newString(void) | Set pointers |
destroyString(string*) | Release memory |
Object Functions | Information |
---|---|
read(string*, char) | Read string until the 'char' is read (0 for Enter as default). Overwrites the current contents if not empty |
size(string*) | Return the size of the string |
clear(string*) | Clear the content of the string |
at(string*, int) | Return the car at the give position if exists, otherwise return -1 |
assign(string*, char*) | Assign the string to the input text |
print(string*, char) | Print string and put the 'char' at the end (0 for no character and '\n' fow newline) |
toupper(string*, char) | Set the string to uppercase |
tolower(string*) | Set the string to lowercase |
strcmp(string*, string*) | Compare strings with sensitive case. Returns a boolean |
stricmp(string*, string*) | Compare strings ignoring sensitive case. Returns a boolean |
constcmp(string*, char*) | Allow to compare directly with a text with sensitive case. Returns a boolean |
concat(string*, string*) | Concat to the first input string |
shrink_to_fit(string*) | Decreases the capacity of the string and makes it euqla to its size |