strio.c

This include file contains often used, small functions related to strings and file I/O. It can also serve as example of using string manipulation and Windows API functions.

str_replace(STRING* str, char* original, char* replacement)

Replaces the first occurrence of one substring by another.

str_replaceall(STRING* str, char* original, char* replacement)

Replaces all occurrences of one substring by another.

Parameters:

str - STRING*, pointer to the string in which the substring is replaced.
original - STRING* or char*, the original substring.
replacement - STRING* or char*, the replaced substring.

Returns:

str when a substring was found and replaced, otherwise NULL.

Example:

STRING* str = "I am %name%, your worst nightmare!"
str_replace(str,"%name%",player_name);

str_cut(STRING* to, char* from, var start, var end)

Cuts a string out of a second string.

Parameters:

to - STRING*, target string pointer, or NULL for creating a new string.
from - STRING* or char*, the string from which to cut.
start - the start position from which to cut, beginning with 1 for the first character, or 0 for not cutting from the start.
end - the end position up to which to cut, beginning with 1 for the first character, or 0 for not cutting from the end.

Returns:

to

Example:

STRING* str = "Do not go gentle into this good night";
STRING* newstr = str_cut(NULL,str,11,16); // newstr now contains "gentle"

 

str_parse(STRING* to, char* from, var start)

Parses a word out of a string. The string must consist of words separated by space characters ' '.

Parameters:

to - STRING*, target string pointer, or NULL for creating a new string.
from - STRING* or char*, the string from which to parse.
start - the start position from which to parse (1 = first character), or 0 for parsing the next word after the position of the last str_parse, str_parse_tail, or str_parse_head call if no other string was parsed inbetween.

Returns:

to

Example:

STRING* str = "Do not go gentle into this good night";
STRING* newstr = str_parse(NULL,str,11); // newstr now contains "gentle"
str_parse(newstr,str,0); // newstr now contains "into"

 

str_parse_tail(STRING* to,char*from,char* tail)

str_parse_head(STRING* to,char*from,char* head)

Extracts a word out of a string when its last characters (str_parse_tail) or its first characters (str_parse_head) are given.

Parameters:

to - STRING*, target string pointer, or NULL for creating a new string.
from - STRING* or char*, the string from which to parse.
tail, head - STRING* or char*, the last or first characters of the word to extract.

Returns:

to when a word with the given beginning or ending was found, otherwise NULL.

Example:

// get a file name from the command line
STRING* file_name = str_parse_tail(NULL,command_str,".txt");
var file_handle = 0;
if (file_name)
file_handle = file_open_read(file_name);

 

str_trim(STRING* str)

Removes space characters from the begin and end of a string.

Parameters:

str - STRING* or char*

Returns:

str

 

file_dialog(char* title,char* filter)

file_dialog_save(char* title,char* filter)

Opens a file dialog for opening resp. saving a file, and returns the selected file name.

Parameters:

title - STRING* or char*, the title of the dialog, or NULL.
filter - STRING* or char*, the file filter pattern (f.i. "*.TXT") or NULL. A pattern string can be a combination of valid file name characters and the asterisk (*) wildcard character. To specify multiple filter patterns, use a semicolon (f.i. "*.MDL;*.HMP;*.WMB"). Do not include spaces in the pattern string.

Returns:

Selected file name with path (char*), or NULL when no file was selected.

Remarks:

The current directory is changed to the selected folder. If this is not desired, change it afterwards back to the game folder with the Windows API function SetCurrentDirectory(_chr(work_dir));.

Example:

char* sky_name = file_dialog("Load skycube","*.tga;*.bmp;*.pcx;*.dds");
if (sky_name)
ent_createlayer(sky_name,SKY|CUBE|SHOW,1);

 

See Also:

file_open

► latest version online