Author Topic: Reading Data from a File  (Read 10641 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?

MindprisM

  • Senior Community Member
  • Posts: 127
  • Hero Points: 8
Re: Reading Data from a File
« Reply #1 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;
}