I had a problem reading multi-line strings (with \n line feeds) from text files using file_str_read. The solution produced a small function I decided to share, because it may be useful for other matters.


1) A search and replace function

Code:

STRING clipstr;
function searchAndReplace(str,searchstr,replacestr)
{
str_cpy(clipstr,str);
var foundat;
foundat = str_stri(clipstr,searchstr);

while(foundat)
{
str_trunc(str,str_len(clipstr)-foundat+1);
str_cat(str,replacestr);
str_clip(clipstr,foundat+str_len(searchstr)-1);
str_cat(str,clipstr);
foundat = str_stri(clipstr,searchstr);
}

}


Example:
mystring="could not read the file";
searchAndReplace(mystring,"read","LOAD");
//mystring now is: "could not LOAD the file";






2) The 'special characters' trick

Code:

// ...somewhere in your text loading function
file_str_read(mystring);
searchAndReplace(mystring,"\\n","\n"); //replaces "\n" with linefeeds in the string
text.string[0] = mystring;




Explanation: special characters are converted directly in C-Script. When you code:
STRING mystring = "Multi \n Line";
You are not putting a real '\n' in the string. The engine replaces the \n for a linefeed.

The same works inverse: if your text file contains "Multi \n Line", the engine doesn't see a linefeed, but a backslash and a 'n'. Thus, you must scan the text looking for a backslash and a 'n' and replacing that by a linefeed.

The special character for backslash is "\\".
So, if you search for "\\n" and replace it by "\n", you are asking "search the text for a backslash followed by 'n' and replace them by a linefeed".