Skip to content
Low-level programming · Higher-level thinking
Login Register
Forum / News

Ziron Library Files

A community discussion post from the original code:zIRON forum.

Colin admin
Posts
928
Threads
49
Location
Bulgaria

Ziron Library Files

right now there is no real documentation on what functions are available inside the library files and their usage... so when i get time i will post in this thread, different functions and their usage, if you want to ask about a specific function then please start a new thread in Source Help section, you will not be able to reply to this thread.


ok to start off with we will look at the most recent procedure addition "strFold" which is inside the "strings.zir" library file.

strFold

strFold will collapse a string by x amount of characters, it takes 2 parameters, the first is the string buffer, and the second is the length you want to collapse by. Lets take a look:

program WIN32CUI 'test';

#include 'strings.zir'; 
#include 'console.zir'; //strings is included in console.zir anyway but i've added it above for reference.

char buf[32];

strAppend(@buf, 'Command 1$Command 2$', 32);

//Here you could process the buf string to the deliminator in this sample.

print('My String: "', @buf, '"\r\n');

strFold(@buf, 10); // <<<<<<<<<<<<<<<<<<<<

//and so on for command 2

print('My String: "', @buf, '"\r\n');

wait_key(nil);
ExitProcess(0);

the output would be as so:

Command 1$Command 2$ Command 2$

notice the first 10 characters are folded and the rest of the buffer has now moved to the start - making way for more appends (which we will take a look at next thread)

more functions will be documented in this thread at later times/dates :)

Replies

Colin admin
Posts
928
Threads
49
Location
Bulgaria

Let us take a closer look at strAppend.

strAppend

program WIN32CUI 'test';

#include 'strings.zir'; 
#include 'console.zir'; //strings is included in console.zir anyway but i've added it above for reference.

char buf[32];

strAppend(@buf, 'Test ', 32);
strAppend(@buf, 'Test ', 32);
strAppend(@buf, 'Test ', 32);
strAppend(@buf, 'Test ', 32);
strAppend(@buf, 'Test ', 32);
strAppend(@buf, 'Test ', 32);
strAppend(@buf, 'Test ', 32);

print('My String: "', @buf, '"\r\n');

//notice there is one Test missing, there is 6 not 7... this is because it does not all fit into the buffer.

//we can perform checks when using strAppend.

eax = strAppend(@buf, 'Test', 32);
if (eax == false) {
	print('strAppend failed - not enough buffer storage to append the string\r\n');
}


//still 6 Test 's
print('My String: "', @buf, '"\r\n');

wait_key(nil);
ExitProcess(0);

as you see in this example, strAppend will append the string from the first null character that is in the buffer, strAppend will return true on success and false if it is unable to append the buffer.

Colin admin
Posts
928
Threads
49
Location
Bulgaria

one of the new functions added is strDup.

strDup

program WIN32CUI 'test';

#include 'strings.zir'; 
#include 'console.zir';


//create our first string "My String"
char buf[32];
strAppend(@buf, 'My String', 32);


//lets duplicate our string into a new allocated buffer using strDup
char* buf2 = strDup(@buf);


//we will lunarize our second string so we can see both buffers are different.
strLunarize(buf2);


//display both our strings
print('My String:         "', @buf, '"\r\n');
print('Duplicated String: "', buf2, '"\r\n');


//duplicated buffers should be deleted with the delete macro to prevent memory leaks
delete buf2;


wait_key(nil);
ExitProcess(0);

output:

My String:         "My String"
Duplicated String: "mY StRiNg"

so here we can see that strDup will take a pointer to a string and then duplicate it into a newly allocated buffer which should be deleted with the "delete" macro when finished with. Pretty straight forward :)

it should be noted that the second buffer will only be allocated enough space to store the actual string, in this case 10 bytes.

Colin admin
Posts
928
Threads
49
Location
Bulgaria

let us take a look this time at a couple of the functions available for searching strings for a substring.

strOffset + strIndexOf

program WIN32CUI 'test';

#include 'strings.zir'; 
#include 'console.zir';


char buf[64];
strAppend(@buf, 'This is my first sentence. This is the second sentence.', 64);


//let us skip past first sentence using strOffset

ecx = strOffset(@buf, '. ');
add ecx, 2 									//move past the ". "
print('str = ', ecx:string, '\r\n');


//let us check if a specific word appears in the text....
eax = strIndexOf(@buf, 'second');
if (eax <> -1) {
	print('second appears in the string\r\n');
} else {
	print('second does NOT appear in the string\r\n');
}


//and again
eax = strIndexOf(@buf, '$');
if (eax <> -1) {
	print('$ appears in the string\r\n');
} else {
	print('$ does NOT appear in the string\r\n');
}

wait_key(nil);
ExitProcess(0);

From this you can see that strOffset actually returns a pointer to the position in the string that contains the found substring, but with strIndexOf, instead it returns a 0-based index of the actual position of the sub-string start.

eax = strIndexOf('test$', '$');

eax would equal 4

we can take a look at the 0-based indexing like so:

0 t
1 e
2 s
3 t
4 $

Hope this is helpful, until next time.