meta,
C++
10 Methoden:
|
/* public: */ char * Ut_Replace :: Replace
/* public: */ void Ut_Replace :: Replace
/* public: */ void Ut_Replace :: expAscii
/* public: */ void Ut_Replace :: Replace
/* public: */ void Ut_Replace :: htm_compressor
#define static
#include ut_repl.h : cpp
#undef BB
#undef BBStr
#undef bb
#undef bbStr
#define BB
#define BBStr( x,y,z ) ;
#define bb
#define bbStr( x,y,z ) ;
#ifndef procNestOn
#undef LB_
#undef LE_
#undef LE_ret
#undef ret
#undef ret_
#undef __
#undef ___
#define LB_( x ) {
#define LE_ }
#define ret_ return ;
#define LE_ret( x ) return( x ); }
#define ret( x ) return( x );
#define __
#define ___
#endif
// #define LB_( x ) { if ( *x == 'U' ) printf( "\n-> %s", x ); //getch();
#define __
// #define __ printf( "\n%i", __LINE__ );
/* public: */ void Ut_Replace :: htm_compressor
/* -----------------------------------------------------------------------------
-----------------------------------------------------------------------------
*/ (
char * aus
, char * nch // darf 0 sein
)
LB_("Ut_Replace.htm_compressor")
if ( ! aus ) fErr1( aus )
char * tmp = "xx_ersetze_nch" ;
char * ers = ( nch )? tmp : nch ;
if ( ! ers ) ers = tmp ;
rBuffer B( aus, "" ); FILE * f = create( ers );
while ( B.find( 1, " ", f ) )
{
char * c = B.z + B.a ; skip_space( c ) c-- ;
B.a = c - B.z ;
}
close( f );
if ( ers == tmp )
then {
cp_file( aus, ers ); Unlink( tmp );
}
LE_
/* public: */ void Ut_Replace :: Replace
/* -----------------------------------------------------------------------------
-----------------------------------------------------------------------------
*/ (
char * aus
, char * nch // darf 0 sein
, char * str1
, char * str2
)
LB_("Ut_Replace.Replace")
if ( ! aus ) fErr1( aus )
if ( ! str1 || ! *str1 ) fErr1( str1 )
if ( ! str2 ) fErr1( str2 )
uint L = strlen( str1 );
char * tmp = "xx_ersetze_nch" ;
char * ers = ( nch )? tmp : nch ;
if ( ! ers ) ers = tmp ;
rBuffer B( aus, "" ); FILE * f = create( ers );
while ( B.find( 0, str1, f ) )
{
B.a += L ;
if ( *str2 ) fprintf( f, "%s", str2 );
}
close( f );
if ( ers == tmp )
then {
cp_file( aus, ers ); Unlink( tmp );
}
LE_
/* public: */ char * Ut_Replace :: Replace
/* -----------------------------------------------------------------------------
Der Aufrufer garantiert:
- In Datei 'spec stehende Zeilen der Form
-/+ str1 str2
-/f from
-/f from to
-/- str1
sind zu verstehen als Befehle
Replace 'str1 by 'str2 in file 'from (or a copy 'to of 'from).
Zwischen -/+ str1 str2 und -/- str1 darf man beliebig viele Komman-
dos -/f geben. Die Funktion sieht jedoch nur Zeilen, die in Spalte 1
mit -/ beginnen: Alle anderen gelten als Kommentar.
Note: If str1 or str2 contains \" or ', enclose it in ' or \" resp.
Strings containing non-printable characters have to be given in the
form ascii.PP.ZZ.PP.ZZ.ZZ.PP ..., ZZ; the decimal representation of a
non-printable ascii character, PP any sequence of printable characters.
Example: ascii.10.10.10; is a sequene of 3 line feeds.
Die Funktion garantiert:
- EN_ok, wenn sie den so spezifizierten Auftrag erledigen konnte
bis auf gewisse Ausnahmen, die sie in Datei X beschreibt.
Return 0, wenn X leer, andernfalls Pointer auf den absoluten
Pfad hin zu dieser Datei %TMP/...
- ansonsten aber EN_err.
-----------------------------------------------------------------------------
*/ (
char * spec // parameter file
)
LB_("Ut_Replace.Replace")
rBuffer Bs( spec, "\n" );
str_set * strset ; new_sto( strset, str_set() )
char * c1 ;
char * c2 ;
char * c ;
char ca ;
char cs ;
while ( Bs.find( 0, "\n-/", 0 ) )
{
Bs.a = Bs.s + 3 ;
c = Bs.z + Bs.a ;
if ( *c == '+' )
then {
while space( *c ) c++ ; c1 = c ;
while ntspace( *c ) c++ ; *c = 0 ; ca = *( c-1 );
if ( ca == '"' || ca == '\'' ) if ( ca == *c1 )
{
c1++ ; *( c-1 ) = 0 ;
}
while space( *c ) c++ ; c2 = c ;
while ntspace( *c ) c++ ;
cs = *c ; *c = 0 ; ca = *( c-1 );
if ( ca == '"' || ca == '\'' ) if ( ca == *c2 )
{
c2++ ; *( c-1 ) = 0 ;
}
strset->insert( c1, c2 ); *c = cs ;
}
esif ( *c == '-' )
then {
while space( *c ) c++ ; c1 = c ;
while ntspace( *c ) c++ ;
cs = *c ; *c = 0 ; ca = *( c-1 );
if ( ca == '"' || ca == '\'' ) if ( ca == *c1 )
{
c1++ ; *( c-1 ) = 0 ;
}
strset->forget( c1 ); *c = cs ;
}
esif ( *c == 'f' )
then {
memcpy( Bs.z + Bs.e, " . ", 3 ); // L1_( str_set )
path source ;
path destin ;
while space( *c ) c++ ; c1 = c ;
while ntspace( *c ) c++ ;
cs = *c ; *c = 0 ; dos_me( c1 );
strcpy( source, c1 ); *c = cs ;
while ( ' ' == *c ) c++ ; c1 = c ;
while ntspace( *c ) c++ ;
cs = *c ; *c = 0 ; dos_me( c1 );
strcpy( destin, c1 ); *c = cs ;
if ( ! *source ) fErr
if ( ! *destin ) strcpy( destin, source );
Replace( destin, source, *strset, 0, 0 );
}
}
rem_sto( strset );
LE_ret( 0 )
/* public: */ void Ut_Replace :: Replace
/* -----------------------------------------------------------------------------
Assume:
Datei ''from existiert und 'to kann erzeugt werden (selbst dann noch,
wenn to == from sein sollte).
Do:
Kopiere ''from nach ''to unter Vornahme aller in ''replace geforderten
Ersetzungen.
Choose errno from EN_ok, EN_err, but choose ...
-----------------------------------------------------------------------------
*/ (
char * to
, char * from
, str_set & replace
, BOOL also_incs // replace also in all includes "..."
, BOOL not_as_prefix //
)
LB_("Ut_Replace.Replace")
FILE * f ; rBuffer B( from, to, f, "" );
B.exp_Umlaute( "ohne_CR", 0 );
if ( also_incs == 1 ) rm_tree( "%TMP/isok" );
new_dos_fn( isok, "%TMP/isok/" ); char * isok_e = isok + strlen( isok );
/*
if is_( to, from )
then P_ "Replace in %s \n", from );
else P_ "Replace %s nach %s\n", from, to );
*/
replace.trace( "RRR", stdout ); fErr
match_rec * rec = B.find( 0, replace, f );
while ( rec )
{
int L = rec -> L ; if ( L <= 0 ) fErr
/*
L4_( L, not_as_prefix, in_name( B.z[ B.a + L ] ), rec->str );
show_str( "STR1", B.z + B.a, L )
L1_( ( B.a == B.s ) )
*/
if ( ! ( not_as_prefix && in_name( B.z[ B.a + L ] ) ) )
then {
/*
P_ "REPL %s --> %s\n", rec->str, rec->str2 );
if ( strlen( rec -> str2 ) < 4 ) fErr
*/
B.a = B.s + L ; fprintf( f, "%s", rec->str2 );
// rec = B.find( L, replace, f );
rec = B.find( 0, replace, f );
}
else rec = B.find( 1, replace, f );
}
close( f );
if ( also_incs )
then {
B.open_rBuffer( from, " " );
path f ; strcpy( f, from ); char * e = f + strlen( f ) - 1 ;
while ( f <= e && ( *e == '.' || in_name( *e ) ) ) e-- ; e++ ;
while ( B.find( 1, "#", 0 ) )
{
char * c = B.z + B.s + 1 ; while space( *c ) c++ ;
if is( c, "include ", 8 )
then {
c += 8 ; while space( *c ) c++ ;
if ( *c == '"' )
then {
c++ ; if ( *c == '"' ) c++ ; // AppStudio kennt 2
char * f = c ;
if ( f[1] == ':' ) fErr // ensure relative paths
while ( *c != '"' ) c++ ; *c = 0 ;
if ( path_exists( f ) )
{
strcpy( isok_e, f ); L2_( f, isok )
if ( ! path_exists( isok ) ) Replace(
f, f, replace, also_incs + 1, not_as_prefix );
dir_exist( isok );
// Dies ist besser, als die Pfade zu merken:
// Kein Pfad hat nur eine Darstellung (so nennt
// z.B. x/a und x/../x/a dieselbe Stelle).
// Note: The path is here always a relative one.
}
}
}
B.a = c - B.z ;
}
}
else ;
if ( also_incs == 1 ) rm_tree( "%TMP/isok" );
LE_
/* public: */ void Ut_Replace :: expAscii
/* -----------------------------------------------------------------------------
Replace substring( ascii.n1.n2.....nn; ) by its value.
-----------------------------------------------------------------------------
*/ (
char * s
)
LB_("Ut_Replace.expAscii")
char * a = s ;
while ( *s )
{
if ( *s != 'a' )
then {
*a++ = *s++ ;
}
esif is( s, "ascii.", 6 )
{
s += 5 ;
while ( *s++ == '.' )
{
if is( s, "LF", 2 )
then {
s += 2 ; *a++ = '\n' ;
}
else {
uint n ; sscanf( s, "%u", &n );
*a++ = ( char )0 + n ;
while digit( *s ) s++ ;
}
}
if ( s[-1] != ';' ) S_"\ns.. = (%s)\n", s-1 _fErr
}
else *a++ = *s++ ;
}
*a = 0 ;
LE_
#undef static
#endif