Search And Replace

(redirected from Main.SAR)

Site in transition, you might want to look at the old version.

Search and Replace

<P>AdaCL features a powerful text search and replace library. Motivation is to provide search and replace functionality which is easier to read and maintain then regular expressions. If you don't know what I mean: Thin of a regular expression which inserts a “<CODE CLASS="western">--

 </CODE>“ before every “<CODE CLASS="western">AdaCL.Trace</CODE>”.

Easy? But you don't really want to end up with “<CODE CLASS="western">--

 --  --  --  AdaCL.Trace</CODE>”, do you? In <CITE CLASS="package-western">AdaCL.SAR</CITE>

<FONT SIZE=2>(with named parameters and without use or rename)</FONT> it looks like this:</P> <PRE LANG="" CLASS="border-western"><A HREF="../../AdaCL/Document/html/sarBC-CommandLine__adb.htm#148_13"></A><A HREF="../../AdaCL/Document/html/______Include__AdaCL__ads.htm#151_5">AdaCL</A><A HREF="../../AdaCL/Document/html/sarBC-CommandLine__adb.htm#148_13">.</A><A HREF="../../AdaCL/Document/html/______Include__AdaCL-SAR__ads.htm#127_11">SAR</A><A HREF="../../AdaCL/Document/html/sarBC-CommandLine__adb.htm#148_13">.</A><A HREF="../../AdaCL/Document/html/______Include__AdaCL-SAR-Filter__ads.htm#166_15">Filter</A><A HREF="../../AdaCL/Document/html/sarBC-CommandLine__adb.htm#148_13">.</A><A HREF="../../AdaCL/Document/html/______Include__AdaCL-SAR-Filter-List__ads.htm#121_22">List</A><A HREF="../../AdaCL/Document/html/sarBC-CommandLine__adb.htm#148_13"></A>.<A HREF="../../AdaCL/Document/html/______Include__bc-indefinite_containers-collections-unbounded__ads.htm#58_14">Append</A> ( <A NAME="58_22"></A> <SPAN LANG=""><A HREF="../../AdaCL/Document/html/______Include__bc-indefinite_containers-collections-unbounded__adb.htm#57_22">C</A> => <A HREF="../../AdaCL/Document/html/sarBC-CommandLine__adb.htm#345_13">Filters</A>,</SPAN> <A NAME="58_45"></A> <SPAN LANG=""><A HREF="../../AdaCL/Document/html/______Include__bc-indefinite_containers-collections-unbounded__adb.htm#57_45">Elem</A> => <A HREF="../../AdaCL/Document/html/______Include__AdaCL-SAR-Filter-Insert__ads.htm#98_22"></A><A HREF="../../AdaCL/Document/html/______Include__AdaCL__ads.htm#151_5">AdaCL</A><A HREF="../../AdaCL/Document/html/______Include__AdaCL-SAR-Filter-Insert__ads.htm#98_22"></A><A HREF="../../AdaCL/Document/html/sarBC-CommandLine__adb.htm#149_13">.</A><A HREF="../../AdaCL/Document/html/______Include__AdaCL-SAR__ads.htm#127_11">SAR</A><A HREF="../../AdaCL/Document/html/sarBC-CommandLine__adb.htm#149_13">.</A><A HREF="../../AdaCL/Document/html/______Include__AdaCL-SAR-Filter__ads.htm#166_15">Filter</A><A HREF="../../AdaCL/Document/html/sarBC-CommandLine__adb.htm#149_13">.</A><A HREF="../../AdaCL/Document/html/______Include__AdaCL-SAR-Filter-Insert__ads.htm#98_22">Insert</A>.<A HREF="../../AdaCL/Document/html/______Include__AdaCL-SAR-Filter-Insert-Text__ads.htm#120_29">Text</A>.<A HREF="../../AdaCL/Document/html/______Include__AdaCL-SAR-Filter-Insert-Text__ads.htm#153_14">New_Object</A> (</SPAN>

        <SPAN LANG=""><A HREF="../../AdaCL/Document/html/______Include__AdaCL-SAR-Filter-Insert-Text__ads.htm#155_9">SearchString</A>  => "AdaCL.Trace",</SPAN>
        <SPAN LANG=""><A HREF="../../AdaCL/Document/html/______Include__AdaCL-SAR-Filter-Insert-Text__ads.htm#157_9">InsertString</A>  => "..  ",</SPAN>
        <SPAN LANG=""><A HREF="../../AdaCL/Document/html/______Include__AdaCL-SAR-Filter-Insert-Text__ads.htm#159_9">Search_At</A>     => <A HREF="../../AdaCL/Document/html/______Include__AdaCL__ads.htm#151_5">AdaCL</A><A HREF="../../AdaCL/Document/html/sarBC-CommandLine__adb.htm#149_13">.</A><A HREF="../../AdaCL/Document/html/______Include__AdaCL-SAR__ads.htm#127_11">SAR</A><A HREF="../../AdaCL/Document/html/sarBC-CommandLine__adb.htm#149_13">.</A><A HREF="../../AdaCL/Document/html/______Include__AdaCL-SAR-Filter__ads.htm#166_15">Filter</A><A HREF="../../AdaCL/Document/html/sarBC-CommandLine__adb.htm#149_13">.</A><A HREF="../../AdaCL/Document/html/______Include__AdaCL-SAR-Filter-Insert__ads.htm#98_22">Insert</A>.<A HREF="../../AdaCL/Document/html/______Include__AdaCL-SAR-Filter-Insert__ads.htm#130_9">First</A>,</SPAN>
        <SPAN LANG=""><A HREF="../../AdaCL/Document/html/______Include__AdaCL-SAR-Filter-Insert-Text__ads.htm#161_9">Insert_At</A>     => <A HREF="../../AdaCL/Document/html/______Include__AdaCL__ads.htm#151_5">AdaCL</A><A HREF="../../AdaCL/Document/html/sarBC-CommandLine__adb.htm#149_13">.</A><A HREF="../../AdaCL/Document/html/______Include__AdaCL-SAR__ads.htm#127_11">SAR</A><A HREF="../../AdaCL/Document/html/sarBC-CommandLine__adb.htm#149_13">.</A><A HREF="../../AdaCL/Document/html/______Include__AdaCL-SAR-Filter__ads.htm#166_15">Filter</A><A HREF="../../AdaCL/Document/html/sarBC-CommandLine__adb.htm#149_13">.</A><A HREF="../../AdaCL/Document/html/______Include__AdaCL-SAR-Filter-Insert__ads.htm#98_22">Insert</A>.<A HREF="../../AdaCL/Document/html/______Include__AdaCL-SAR-Filter-Insert__ads.htm#113_9">Before</A>));</SPAN></PRE><P>

To much to type? With use and rename you could reduce it to:</P> <PRE LANG="" CLASS="border-western"><A HREF="../../AdaCL/Document/html/______Include__bc-indefinite_containers-collections-unbounded__ads.htm#58_14">Append</A> (<A HREF="../../AdaCL/Document/html/sarBC-CommandLine__adb.htm#345_13">Flt</A>, <A HREF="../../AdaCL/Document/html/______Include__AdaCL-SAR-Filter-Insert__ads.htm#98_22">I</A><A HREF="../../AdaCL/Document/html/______Include__AdaCL-SAR-Filter-Insert-Text__ads.htm#120_29">T</A>.<A HREF="../../AdaCL/Document/html/______Include__AdaCL-SAR-Filter-Insert-Text__ads.htm#153_14">New_Object</A> ("AdaCL.Trace", ".. ", <A HREF="../../AdaCL/Document/html/______Include__AdaCL-SAR-Filter-Insert__ads.htm#130_9">First</A>,.<A HREF="../../AdaCL/Document/html/______Include__AdaCL-SAR-Filter-Insert__ads.htm#113_9">Before</A>));</PRE><P> Which is still easer to read then <FONT SIZE=2>(and proving that I can use regular expression if I want to)</FONT>:</P> <PRE LANG="" CLASS="border-western">/(-- ){0}(AdaCL\.Trace)/-- \2/</PRE><P> <A NAME="71_14"></A>Last not least: If you still want to use regular expressions, it should not take an experienced Ada programmer more then two hours to write a <CITE CLASS="package-western"><A HREF="../../AdaCL/Document/html/______________________________opt__ada__lib__gcc-lib__i686-pc-linux-gnu__2__8__1__rts-native__adainclude__gnat__ads.htm#38_9">GNAT</A>.<A HREF="../../AdaCL/Document/html/______________________________opt__ada__lib__gcc-lib__i686-pc-linux-gnu__2__8__1__rts-native__adainclude__g-regpat__adb.htm#49_19">Regpat</A></CITE> filter for <CITE CLASS="package-western">AdaCL.SAR</CITE>.</P> <H2 CLASS="title">Files</H2> <P><CITE CLASS="package-western">AdaCL.SAR</CITE> filters text files. This is done with <CITE CLASS="package-western"><A HREF="html/______Include__AdaCL__ads.htm#151_5">AdaCL</A>.<A HREF="html/______Include__AdaCL-SAR__ads.htm#127_11">SAR</A>.<A HREF="html/______Include__AdaCL-SAR-File__ads.htm#177_15">File</A></CITE> object:</P> <PRE LANG="" CLASS="border-western"><A NAME="239_9"></A> <FONT COLOR="DarkOrchid">Filters</FONT> : <A HREF="html/______Include__AdaCL-SAR-CommandLine__adb.htm#117_13"></A><A HREF="html/______Include__AdaCL__ads.htm#151_5">AdaCL</A><A HREF="html/______Include__AdaCL-SAR-CommandLine__adb.htm#117_13">.</A><A HREF="html/______Include__AdaCL-SAR__ads.htm#127_11">SAR</A><A HREF="html/______Include__AdaCL-SAR-CommandLine__adb.htm#117_13">.</A><A HREF="html/______Include__AdaCL-SAR-Filter__ads.htm#166_15">Filter</A><A HREF="html/______Include__AdaCL-SAR-CommandLine__adb.htm#117_13">.</A><A HREF="html/______Include__AdaCL-SAR-Filter-List__ads.htm#121_22">List</A><A HREF="html/______Include__AdaCL-SAR-CommandLine__adb.htm#117_13"></A>.<A HREF="html/______Include__bc-indefinite_containers-collections-unbounded__ads.htm#40_9">Collection</A>; <A NAME="240_9"></A> <SPAN LANG=""><FONT COLOR="DarkOrchid">File_Filter</FONT> : <B>aliased</B> <A HREF="html/______Include__AdaCL__ads.htm#151_5">AdaCL</A><A HREF="html/______Include__AdaCL-SAR-CommandLine__adb.htm#117_13">.</A><A HREF="html/______Include__AdaCL-SAR__ads.htm#127_11">SAR</A><A HREF="html/______Include__AdaCL-SAR-CommandLine__adb.htm#117_13">.</A><A HREF="html/______Include__AdaCL-SAR-File__ads.htm#177_15">File</A>.<A HREF="html/______Include__AdaCL-SAR-File__ads.htm#186_9">Object</A></SPAN>

                <SPAN LANG="">:= <A HREF="html/______Include__AdaCL__ads.htm#151_5">AdaCL</A><A HREF="html/______Include__AdaCL-SAR-CommandLine__adb.htm#117_13">.</A><A HREF="html/______Include__AdaCL-SAR__ads.htm#127_11">SAR</A><A HREF="html/______Include__AdaCL-SAR-CommandLine__adb.htm#117_13">.</A><A HREF="html/______Include__AdaCL-SAR-File__ads.htm#177_15">File</A>.<A HREF="html/______Include__AdaCL-SAR-File__ads.htm#209_14">New_Object</A> (</SPAN>
                       <SPAN LANG=""><A HREF="html/______Include__AdaCL-SAR-File__ads.htm#211_9">In_File</A>     => "Name.Old",</SPAN>
                       <SPAN LANG=""><A HREF="html/______Include__AdaCL-SAR-File__ads.htm#213_9">Out_File</A>    => "Name.New",</SPAN>
                       <SPAN LANG=""><A HREF="html/______Include__AdaCL-SAR-File__ads.htm#215_9">Filter_List</A> => <A HREF="html/______Include__AdaCL-SAR-CommandLine__adb.htm#239_9">Filters</A>);</SPAN>

<SPAN LANG=""><B>begin</B></SPAN>

    <SPAN LANG=""><A HREF="html/______Include__AdaCL-SAR-File__ads.htm#177_15">File</A>.<A HREF="html/______Include__AdaCL-SAR-File__ads.htm#222_15">Execute</A> (<A HREF="html/______Include__AdaCL-SAR-CommandLine__adb.htm#240_9">File_Filter</A>'<B>Access</B>);</SPAN></PRE><P>

Execute will not return until the file is processed. In_File and Out_File may be the same in which case a backup copy is created. The Filter_List is a list of filters to be applied to the file.</P> <H2 CLASS="title">Filters</H2> <P>All Filters work on the same principle. Fist you create a filter list then you create one or more filter with New_Object and add it to a filter list with Append.</P> <H3 CLASS="subtitle">Simple Text Replace</H3> <P>New_Object takes two parameters: The text to search for and the text to replace with.</P> <H3 CLASS="subtitle">Insert</H3> <P>Inserts new text into the File. All insert filter will check if the text which should be inserted is not already in place.</P> <H4 CLASS="minortitle">Text</H4> <P>Inserts a new text before, after or around a search text. You may also decide if – within a single line- only the first, the last or all hits should be changed.</P> <H4 CLASS="minortitle">Lines</H4> <P>Inserts a new line of text after the a line which contains the search text. You can copy begin or end of the found line into the insert line. Helpful if you want the found line to be indented just as much as the searched line.</P> <H3 CLASS="subtitle">Remove</H3> <P><BR><BR> </P> <H4 CLASS="minortitle">Tabs</H4> <P>Removes Tabs by inserting a</P> <H4 CLASS="minortitle">Lines</H4> <P>Remove all lines which contain the search text.</P> <H3 CLASS="subtitle">Filter of Filters</H3> <P>The Filter of Filters will apply a given List of Filter on line which do or do not include a given search text.</P> <H4 CLASS="minortitle">Excludes</H4> <P>Filters will be applied when search is not found inside the current line.</P> <H4 CLASS="minortitle">Includes</H4> <P>Filters will be applied when search is found inside the current line.</P> <H2 CLASS="title">Command Line Parsing</H2> <P><BR><BR> </P> <H1 CLASS="maintitle">More Examples</H1> <P>You find some more examples with the following files. The links should take to “<VAR><VAR CLASS="command">gnathtml</VAR>”</VAR> generated web sides.</P> <DIV ALIGN=RIGHT>

	<TABLE WIDTH=100% BORDER=1 CELLPADDING=6 CELLSPACING=5 BGCOLOR="FloralWhite" STYLE="page-break-before: always">
		<COL WIDTH=84*>
		<COL WIDTH=84*>
		<COL WIDTH=87*>
		<THEAD>
			<TR BGCOLOR="OldLace" VALIGN=TOP>
				<TH WIDTH=33% BGCOLOR="OldLace">
					<P CLASS="clr1">File</P>
				</TH>
				<TH WIDTH=33% BGCOLOR="OldLace">
					<P>Filter</P>
				</TH>
				<TH WIDTH=34% BGCOLOR="OldLace">
					<P CLASS="clr1">Command Line</P>
				</TH>
			</TR>
		</THEAD>
		<TBODY>
			<TR BGCOLOR="FloralWhite" VALIGN=TOP>
				<TD WIDTH=33% BGCOLOR="FloralWhite">
					<P CLASS="clr0"><A HREF="html/scanscsi-Main__adb.htm" NAME="scanscsi-Main"><SPAN STYLE="text-decoration: none"><FONT COLOR="firebrick">...</FONT></SPAN></A></P>
				</TD>
				<TD WIDTH=33% BGCOLOR="FloralWhite">
					<P CLASS="clr0"><A HREF="html/TestCommand-Main__adb.htm" NAME="TestCommand-Main"><SPAN STYLE="text-decoration: none"><FONT COLOR="firebrick">....</FONT></SPAN></A></P>
				</TD>
				<TD WIDTH=34%>
					<P CLASS="clr0"><A HREF="html/TestCommand-Main__adb.htm" NAME="TestCommand-Main"><SPAN STYLE="text-decoration: none"><FONT COLOR="firebrick">...</FONT></SPAN></A></P>
				</TD>
			</TR>
		</TBODY>
	</TABLE>

</DIV> <P><BR><BR> </P>


Ada programming, © 2005,2006 the Authors, Content is available under GNU Free Documentation License.