File reading basics - ReadRawLineBack(); [WORKAROUND]

Started by WHAM, Fri 17/01/2014 10:27:32

Previous topic - Next topic

WHAM

Hello everyone!

Working on a small proof of concept text adventure thingamabob, and now I am a bit stumped with the ways AGS works.

I have a data file, which is basically a generated .csv file, which contains rows such as the following (note that each line of content is quite long for test purposes):

1;WHAM1;Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin eget magna lacus. Suspendisse pharetra non nisl quis tristique. Cras suscipit felis ante, et adipiscing libero aliquet non. Vestibulum luctus elit mi, sit amet viverra arcu rutrum mattis. Donec eget tellus sed mauris accumsan sagittis lacinia nec nisl. Nam rhoncus, nunc sit amet condimentum imperdiet, quam erat aliquam eros, ut tincidunt tellus dui vitae lorem.
2;WHAM2;Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin eget magna lacus. Suspendisse pharetra non nisl quis tristique. Cras suscipit felis ante, et adipiscing libero aliquet non. Vestibulum luctus elit mi, sit amet viverra arcu rutrum mattis. Donec eget tellus sed mauris accumsan sagittis lacinia nec nisl. Nam rhoncus, nunc sit amet condimentum imperdiet, quam erat aliquam eros, ut tincidunt tellus dui vitae lorem.


Next up I have the following bit of code set up to test reading the data for further processing:

Code: ags

String GetTextLine() {
  File *output = File.Open("b001.dat", eFileRead);
  if (output == null)
    Display("Error opening file.");
  else {
    String data = output.ReadRawLineBack();
    Display("%s", data);
    data = output.ReadRawLineBack();
    Display("%s", data);
    output.Close();
  }
}


As far as I can tell, this should return both of the above lines of text. However, instead it returns the following lines displayed:
"1;WHAM1;Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin eget magna lacus. Suspendisse pharetra non nisl quis tristique. Cras suscipit felis ante, et adipiscing libero aliquet non. Vesti"
and
"bulum luctus elit mi, sit amet viverra arcu rutrum mattis. Donec eget tellus sed mauris accumsan sagittis lacinia nec nisl. Nam rhoncus, nunc sit amet condimentum imperdiet, quam erat aliquam eros, u"


So it seems that the ReadRawLineBack(); -function is restricted to importing 199 characters or so.

1. Is this correct, or is there something wrong with my data file?
2. Is there a way around this issue?

Thanks in advance!
Wrongthinker and anticitizen one. Pending removal to memory hole. | WHAMGAMES proudly presents: The Night Falls, a community roleplaying game

Billbis

Quote from: WHAMSo it seems that the ReadRawLineBack(); -function is restricted to importing 199 characters or so.
This is correct.
Quote from: WHAM2. Is there a way around this issue?
I am certainly not enough qualified to answer that question. When I encounter the problem, kitai produced this marvelous piece of code :
Code: AGS
String FichierParam::GetParam(String param) {
 
  if (String.IsNullOrEmpty(param) || String.IsNullOrEmpty(this.sep)) return "";
 
  File* fich = File.Open(this.fichier, eFileRead);
  String debut = param.Append(this.sep), fin = this.sep.Append(param);
 
  while (!fich.EOF && !fich.Error) {
    String ret = fich.ReadRawLineBack();
    if (ret != null && ret.StartsWith(debut, true)) {
      while (!fich.EOF && !ret.EndsWith(fin, true) && !fich.Error)
        ret = ret.Append(fich.ReadRawLineBack());
      fich.Close();
      return ret.Substring(debut.Length, ret.Length - 2*fin.Length);
    }
  }
  fich.Close();
  return "";
  
}

(Which is part of his module FichierParam, full code available here)
I do not understand this code, but I hope you will. ;)
Note that the previous code is designed for a custom separator (this.sep) and not for csv.
The idea is to put a second time the name at the end of the text:

WHAM1;Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin eget magna lacus. Suspendisse pharetra non nisl quis tristique. Cras suscipit felis ante, et adipiscing libero aliquet non. Vestibulum luctus elit mi, sit amet viverra arcu rutrum mattis. Donec eget tellus sed mauris accumsan sagittis lacinia nec nisl. Nam rhoncus, nunc sit amet condimentum imperdiet, quam erat aliquam eros, ut tincidunt tellus dui vitae lorem.;WHAM1
WHAM2;Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin eget magna lacus. Suspendisse pharetra non nisl quis tristique. Cras suscipit felis ante, et adipiscing libero aliquet non. Vestibulum luctus elit mi, sit amet viverra arcu rutrum mattis. Donec eget tellus sed mauris accumsan sagittis lacinia nec nisl. Nam rhoncus, nunc sit amet condimentum imperdiet, quam erat aliquam eros, ut tincidunt tellus dui vitae lorem.;WHAM2


Hope that helps.

WHAM

At first I looked at that code and was like 8-0.

Then I gave it a proper look over and it actually makes quite a bit of sense. Using that basic logic I might be able to alter it slightly to make it better suit my purposes.

Thanks for the help, Billbis!
Wrongthinker and anticitizen one. Pending removal to memory hole. | WHAMGAMES proudly presents: The Night Falls, a community roleplaying game

Khris

What you should do is read raw chars until you hit 10 13 (lf, cr).
It's probably faster to read the file in strings, but it doesn't require you ending line with a closing tag.

WHAM

My idea was to have a simple symbol or character at the start of each line, then use that to identify when we start writing data into a new string in the array I am reading these lines into. Whichever way I decide to do it, I think it will be good practice for file handling and data transfer.

The project I am thinking of has about a hundred text files which need to be used in a varying order, so the plan is to have an array into which I read the file being used, then use the array until lines run out, then run the read function again on another file and voila!
Wrongthinker and anticitizen one. Pending removal to memory hole. | WHAMGAMES proudly presents: The Night Falls, a community roleplaying game

SMF spam blocked by CleanTalk