SlickEdit Community

SlickEdit Product Discussion => SlickEditĀ® => Slick-CĀ® Macro Programming => Topic started by: tsunokawa on October 22, 2019, 07:33:00 am

Title: How to Read text file with Slick-C Macro
Post by: tsunokawa on October 22, 2019, 07:33:00 am
Hello
I am new to SlickEdit programming environment.
How can I read a text file with Slick-C Macro just like "fgets" function in C language?
For example, a.txt contains "1" as text.  It would be nice if I could get the "1" in the Slick-C Macro like with below.

_str readStr = xxxx("a.txt");

I searched the way, after several hours however I could not find it.
I hope what I want to do is clear. Thank you in advance.
Title: Re: How to Read text file with Slick-C Macro
Post by: Dan on October 22, 2019, 09:16:47 am
I'm sorry you searched so long.  There's a couple of answers to your problem.  One is to record a macro, then tweak it to suit your needs if you need to change it.  SlickEdit's macro recording actually generates Slick-C source instead of just keys.

If this doesn't do what you want, I can get you another solution, but this is very easy.

Again, sorry you looked so long.
Title: Re: How to Read text file with Slick-C Macro
Post by: Dennis on October 22, 2019, 04:24:58 pm
If you want the get the *entire* file in one shot, you could use a function like this:
Code: [Select]
_str get_file_contents(_str filename)
{
   status := _open_temp_view(filename,auto temp_wid,auto orig_wid);
   if (status) return null;
   result := get_text(p_buf_size,0);
   _delete_temp_view(temp_wid);
   p_window_id=orig_wid;
   return(result);
}

If you want to get all the lines in a file, and stuff them into an array, you could do something like this:
Code: [Select]
STRARRAY get_file_lines(_str filename)
{
   status := _open_temp_view(filename,auto temp_wid,auto orig_wid);
   if (status) return null;
   _str lineContents;
   _str lineArray[];
   top();
   loop {
      get_line(lineContents);
      lineArray :+= lineContents;
      if (down()) break;
   }
   _delete_temp_view(temp_wid);
   p_window_id=orig_wid;
   return lineArray;
}

If you want a function that behaves more incrementally like fgets(), you could use a function like this to get each line, until it hits the end of the file and returns null.
Code: [Select]
_str get_one_line(bool startAtFirstLine=false)
{
      if (startAtFirstLine) {
          top();
      } else if (down()) {
          return null;
      }
      get_line(auto lineContents);
      return lineContents;
}
void print_file_contents(_str filename)
{
   status := _open_temp_view(filename,auto temp_wid,auto orig_wid);
   if (status) return null;
   lineContents := get_one_line(startAtFirstLine:true);
   for (i := 1; lineContents != null; i++) {
       say("line["i": "lineContents);
       lineContents = get_one_line();
   }
   _delete_temp_view(temp_wid);
   p_window_id=orig_wid;
}

Title: Re: How to Read text file with Slick-C Macro
Post by: tsunokawa on October 24, 2019, 01:37:44 pm
Hi Dan,
No problem. Thank you for your replay. Maybe it is not what I wanted to do. I want to use the read value in my Macro, not using gui-open.
Thank you anyway.

Hi Dennis,
Thank you very much. It is exactly what I wanted to do. I tried your code and it worked! ("print_file_contents" needed small modification for my environment. "if (status) return null" -> "if (status) return").
Again thank you very much!

Also I found a function "_GetFileContents" which can read text file. This worked as well.

Best regards,