Author Topic: Macro to extract string list from file based on targets of a regex  (Read 5937 times)

MindprisM

  • Senior Community Member
  • Posts: 127
  • Hero Points: 8
Let me know if this has any issues.   
IMPORTANT: remove underbar from "sub_str" (Cannot post that string for some reason!!!)

/**
 * Extract regular expression "hits" from a file. A hit is a marked portion of a
 * Regular expression - enclosed in {}.
 *
 * @param exp    Regular expresion containing hit markup
 * @param f      Target file
 * @param i      The hit you want returned - used for multiple specification of hits;
 *               ie. more than one set of {} in expression.
 * @param opts   Regex search options - will be added to the built-in 'r' for Slick RE.
 *
 * @return Array of strings, the hits.
 */



Code: [Select]
int file_open_view(_str filename,int &temp_view_id,int &orig_view_id,_str load_options="",boolean must_exist=false)
{
   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;
}

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;
}

typeless file2array(_str f)
{
   status=file_open_view(f,temp_view_id,orig_view_id);
   if (status) {
      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);
}
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);
      if (p1) {
         pp1=pos('S'itm);
         pp2=pos(''itm);
         w=sub_str(s,pp1,pp2);
         a[a._length()]=w;
         s=sub_str(s,p1+pp1);
      }else{
         return a;
      }
   }
   return a;
}
typeless array_merge(typeless a,typeless b)
{
   //typeless c[];
   for (x=0;x<b._length();x++) {
      a[a._length()]=b[x];
   }
   return a;
}
typeless array_paste(a,_str pfx='',_str sfx='')
{
   x=0;
   for (x=0;x<a._length();x++) {
      insert_line(pfx''a[x]''sfx);
   }
   return (0);
}


/**
 * Extract regular expression "hits" from a file. A hit is a marked portion of a
 * Regular expression - enclosed in {}.
 *
 * @param exp    Regular expresion containing hit markup
 * @param f      Target file
 * @param i      The hit you want returned - used for multiple specification of hits;
 *               ie. more than one set of {} in expression.
 * @param opts   Regex search options - will be added to the built-in 'r' for Slick RE.
 *
 * @return Array of strings, the hits.
 */
typeless file_extract_exp_n(_str exp,_str f=p_buf_name,int i=0,_str opts='')
{
   //typeless a[];
   a=file2array(f);
   typeless b[];
   //exp='target name="{?*}"';
   for (x=0;x<a._length();x++) {
      s=a[x];
      word=str_extract(s,exp,i,opts);
      b=array_merge(b,word);
   }
   return b;
}

/**
 * Demonstration of file_extract_exp_n function.
 *
 * Purpose: extract an array of regex find targets in a particular file
 * and paste the results into the current buffer.
 *
 * Param: exp - contains expression with a target (enclosed in {})
 *
 * File Build.xml contains series of - see example
 *
 * @example
 *   <target name="explore">
 *     <echo>
 *       explore.dir: ${explore.dir}
 *     </echo>
 *     <exec dir="." executable="${explorer.exe}" failonerror="true">
 *       <arg line='${explore.dir}'/>
 *     </exec>
 *   </target>
 */
_command void usage___() name_info(',')
{
   _str cw=getcwd();
   f=cw'\build.xml';
   exp='target name="{?*}"';
   bb=file_extract_exp_n(exp,f,0);
   array_paste(bb);
}


« Last Edit: July 14, 2008, 09:24:02 AM by MindprisM »