Author Topic: Reading Data from a File  (Read 7790 times)

Absinthe

  • Guest
Reading Data from a File
« on: September 18, 2009, 03:19:02 PM »
I have a macro called "col_list_from_table_def()" all it really does is perform some regex magic on a specifically formatted file and return a string. This functions properly and is for this question unimportant.

However, from a second macro called sel_table(tableName) I want to open a file, execute that macro, close the file and insert the results in the buffer where I was to start with.
Quote
_command sel_table(tableName)
{

   edit_file_in_project(tableName);
   cols = col_list_from_table_def();
   quit_file()

   _insert_text(cols);
}

I have tried several iterations on this code, but all end up badly. Either it writes the text to some other buffer that is also open, but different from the one that was active when it was called. Or it seems to dupe the buffer window when it is done, as if it opened the tableFile in a window and when it quits, it opens the original file in the same window instead of dismissing it.

I am certain I am just doing something stupid, anyone have a clue how to fix this?

Absinthe

  • Guest
Re: Reading Data from a File
« Reply #1 on: September 18, 2009, 04:53:19 PM »
Ok, so it looks like
execute('quit');

is the solution. I am not sure why that is different from quit_file() or quit() or whatever other else but it seems to do the trick for now.

Is there any better way to read a file without creating a window for it such that I can still run regex on it, or just open it and read it into a string variable or something like that to avoid all the flash and stuff?

Absinthe

  • Guest
Re: Reading Data from a File
« Reply #2 on: September 18, 2009, 06:29:21 PM »
Ok, so I lied.

If I use quit() or execute ("quit" ) then the subsequent _insert_text or even keyin will fail to actually output any text at all. If I put a call to either before the process then it outputs just fine but calls after the quit() fail...

Absinthe

  • Guest
Re: Reading Data from a File
« Reply #3 on: September 18, 2009, 08:02:55 PM »
Ok, so here is the current fix, it was a guess, but it worked.

Quote
_command sel_table(tableName)
{
   orig = p_buf_id;

   edit_file_in_project('-D dbo.'tableName'.sql');
   cols = col_list_from_table_def();
   quit();

   p_buf_id = orig;

   _str text = 'SELECT 'stranslate(cols,"\n     ,",",")"\n  FROM dbo."tableName"\n WHERE ";

   _insert_text(text);
}

The red text is what I changed. I am not sure why I had to do that, nor why it had previously worked without it. But for now it seems to work.

MindprisM

  • Senior Community Member
  • Posts: 127
  • Hero Points: 8
Re: Reading Data from a File
« Reply #4 on: September 18, 2009, 09:56:46 PM »
Quote
Is there any better way to read a file without creating a window for it such that I can still run regex on it, or just open it and read it into a string variable or something like that to avoid all the flash and stuff?


What I have been doing is using arrays to get around all the buffer pains. This seems to work fine with moderate size files (perhaps <20,000) lines.



Code: [Select]


// view primatives
typeless view2array(view_id)
{
   typeless a[];
   get_view_id(orig_view_id);
   if (orig_view_id!=view_id) {
      activate_view(view_id);
   }
   save_pos(p);
   top();
   int x=0;
   while (p_Noflines>=p_line) {
      a[a._length()]=line();
      //msg_3(line());
      if (down()==BOTTOM_OF_FILE_RC){break;}
   }
   restore_pos(p);
   if (view_id!=orig_view_id) {
      activate_view(orig_view_id);
   }
   return a;
}
int view_from_array(view_id,a)
{
   get_view_id(orig_view_id);
   activate_view(view_id);
   save_pos(p);
   select_all();
   delete_selection();
   top();
   x=0;
   for (x=0;x<a._length();x++) {
      //Msg('__view:'a[x]);
     insert_line(a[x]);
   }
   restore_pos(p);
   activate_view(orig_view_id);
   return(0);
}
int view_append_array(view_id,a)
{
   get_view_id(orig_view_id);
   activate_view(view_id);
   save_pos(p);
   bottom();
   if (p_line==1) {
      up();
   }
   //up();
   x=0;
   for (x=0;x<a._length();x++) {
     insert_line(a[x]);
   }
   restore_pos(p);
   activate_view(orig_view_id);
   return(0);
}

// file and arrays private primatives
int file_open_view(_str filename,int &temp_view_id,int &orig_view_id,_str load_options="",boolean must_exist=false)
{

   //quiet='';//=' +q';
   //Msg(' file_open_view:filename'filename);
   status=0;
   boolean buffer_already_exists;
   boolean doClear=false;
   boolean doSelectEditMode=false;
   int more_buf_flags=0;
   boolean doCreateIfNotFound=false;
   if (pos('+c',load_options)>-1) {
      load_options=stranslate(load_options,'','+c');
      doCreateIfNotFound=true;
   }
   boolean doCallSelectEditModeLater=false;
   //int _open_temp_view(_str filename,
   // int &temp_wid,
   // int &orig_wid,
   // _str load_options="",
   // boolean &buffer_already_exists=true,
   // boolean doClear=false,
   // boolean doSelectEditMode=false,
   // int more_buf_flags=0,
   // boolean doCreateIfNotFound=false, boolean doCallSelectEditModeLater=false)
   //Msg('file_open_view:maybe_quote_filename(filename):'maybe_quote_filename(filename));
   status=_open_temp_view(maybe_quote_filename(filename),temp_view_id,orig_view_id,load_options,buffer_already_exists,doClear,doSelectEditMode,more_buf_flags,doCreateIfNotFound);
   return status;
}

// file and arrays public
typeless file2array(_str f)
{
   status=file_open_view(f,temp_view_id,orig_view_id);
   if (status) {
      //Msg('file2array:status:'status' f:'f);
      return(status);
   }
   activate_view(temp_view_id);
   a=view2array(temp_view_id);
   _delete_temp_view(temp_view_id,true);
   activate_view(orig_view_id);
   return(a);
}


int file_write_array(_str f,a){
   status=file_open_view(f,temp_view_id,orig_view_id);
   if (status) {
      //Msg('status:'status);
      return(status);
   }
   activate_view(temp_view_id);
   save_pos(p);
   view_from_array(temp_view_id,a);
   restore_pos(p);
   save();
   _delete_temp_view(temp_view_id,true);
   activate_view(orig_view_id);
   return(0);
}
// str primative
typeless str_extract(_str s,_str exp,int itm=0,_str opts='')
{
   at=1;
   word='';
   typeless a[];
   while (true) {
      p1=pos(exp,s,1,'r'opts);
      //Msg('p1:'p1);
      //p1=pos(exp,s,1,'r'opts);
      //at=p1+1;
      if (p1) {
         pp1=pos('S'itm);
         pp2=pos(''itm);
         //Msg('pp1:'pp1' pp2:'pp2);
         w=substr(s,pp1,pp2);
         a[a._length()]=w;
         //Msg('w:'w);
         word=w word;
         //s=substr(s,p1+1);
         s=substr(s,p1+pp1);
         //Msg('s:'s);
      }else{
         return a;
      }
   }
   return a;
}


// array utils
//
typeless array_merge(typeless a,typeless b)
{
   //typeless c[];
   for (x=0;x<b._length();x++) {
      a[a._length()]=b[x];
   }
   return a;
}

// extract regex item
typeless array_extract_exp_n(typeless a,_str exp,int i=0)
{
   // example
   //                0
   // exp=':b*Name="{?*}"';
   // array_extract_exp_n(a,exp,0);
   //
   typeless b[];
   int show=1;
   for (x=0;x<a._length();x++) {
      _str s=a[x];
      typeless word=str_extract(s,exp,i);
      if (word._length()>0) {
         if (show) {
            //Msg(word._length());
            //clip_set_text(s);
            //show=0;
         }
         b=array_merge(b,word);
      }
   }
   return b;
}



Absinthe

  • Guest
Re: Reading Data from a File
« Reply #5 on: September 21, 2009, 01:27:15 PM »
I am not sure if the array stuff helps me in what I am trying to do here, but the _open_temp_view and _delete_temp_view may very well be useful. I will give them a try.

Absinthe

  • Guest
Re: Reading Data from a File
« Reply #6 on: September 21, 2009, 02:26:08 PM »
Excellent. I modified the file_open_view to be project_file_view by adding the line:

   filename = _ProjectFindFile(filename, _project_name, false, true);

That did the trick. Nice and clean, or at least cleaner. I would never have found _open_temp_view had it not been for this example. Since I am opening the file and extracting a few lines that meet a particular criteria anyway, I didn't see any use for the array stuff at this point, but I will keep that in the back of my mind, since it seems like it should be useful some time.