From: Han-Wen Nienhuys & Jan Nieuwenhuizen Date: Wed, 19 Mar 1997 00:52:59 +0000 (+0100) Subject: partial: 0.0.42.pre3.hanjan X-Git-Tag: release/0.0.42.pre3~1 X-Git-Url: https://git.donarmstrong.com/?a=commitdiff_plain;h=cfafdae9300eb768269ff92118858128e2a8a515;p=lilypond.git partial: 0.0.42.pre3.hanjan --- diff --git a/flower/include/pcursor.hh b/flower/include/pcursor.hh new file mode 100644 index 0000000000..37512d553b --- /dev/null +++ b/flower/include/pcursor.hh @@ -0,0 +1,68 @@ +/* + pcursor.hh -- part of flowerlib + + (c) 1996 Han-Wen Nienhuys&Jan Nieuwenhuizen +*/ + +#ifndef PCURSOR_HH +#define PCURSOR_HH + +#include "plist.hh" +#include "cursor.hh" + +/** cursor to go with PointerList. + don't create PointerList's. + This cursor is just an interface class for Cursor. It takes care of the + appropriate type casts + */ +template +class PCursor : private Cursor { + friend class IPointerList; + + /// delete contents + void junk(); +public: + Cursor::ok; + Cursor::del; + Cursor::backspace; + T remove_p() { + T p = ptr(); + Cursor::del(); + return p; + } + T remove_prev_p() { + assert( ok() ); + (*this)--; + return remove_p(); + } + + PointerList &list() { return (PointerList&)Cursor::list(); } + PCursor operator++(int) { return Cursor::operator++(0);} + PCursor operator--(int) { return Cursor::operator--(0); } + PCursor operator+=(int i) { return Cursor::operator+=(i);} + PCursor operator-=(int i) { return Cursor::operator-=(i); } + PCursor operator -(int no) const { return Cursor::operator-(no);} + int operator -(PCursor op) const { return Cursor::operator-(op);} + PCursor operator +( int no) const {return Cursor::operator+(no);} PCursor(const PointerList & l) : Cursor (l) {} + + PCursor( const Cursor& cursor ) : Cursor(cursor) { } + void* vptr() const { return *((Cursor &) *this); } + + // should return T& ? + T ptr() const { return (T) vptr(); } + T operator ->() const { return ptr(); } + operator T() { return ptr(); } + T operator *() { return ptr(); } + void add(T const & p ) { Cursor::add((void*) p); } + void insert(T const & p ) { Cursor::insert((void*) p);} + static int compare(PCursor a,PCursorb) { + return Cursor::compare(a,b); + } +}; + + + +#include "compare.hh" +template_instantiate_compare(PCursor, PCursor::compare, template); + +#endif diff --git a/flower/include/plist.hh b/flower/include/plist.hh new file mode 100644 index 0000000000..f9af4c693b --- /dev/null +++ b/flower/include/plist.hh @@ -0,0 +1,72 @@ +/* + list.hh -- part of flowerlib + + (c) 1996 Han-Wen Nienhuys & Jan Nieuwenhuizen +*/ + +#ifndef PLIST_HH +#define PLIST_HH + +#include "list.hh" + +/** + A list of pointers. + + Use for list of pointers, e.g. PointerList. + This class does no deletion of the pointers, but it knows how to + copy itself (shallow copy). We could have derived it from List, + but this design saves a lot of code dup; for all PointerLists in the + program only one parent List is instantiated. + */ +template +class PointerList : public List +{ + public: + PCursor top() const{ + return PCursor (List::top()); + } + PCursor bottom() const { + return PCursor (List::bottom()); + } + PCursor find(T) const; + void concatenate(PointerList const &s) { List::concatenate(s); } + PointerList() {} +}; + +/** PointerList which deletes pointers given to it. + NOTE: + + The copy constructor doesn't do what you'd want: + Since T might have a virtual ctor, we don't try to do a + + new T(*cursor) + + You have to copy this yourself, or use the macro PointerList__copy + + */ +template +class IPointerList : public PointerList { +public: + IPointerList(IPointerList const &) { set_empty(); } + IPointerList() { } + ~IPointerList(); +}; + +#define IPointerList__copy(T, to, from, op) \ + for (PCursor _pc_(from); _pc_.ok(); _pc_++)\ + to.bottom().add(_pc_->op)\ + \ + + +template +void PL_copy(IPointerList &dst,IPointerList const&src); + + +#define PL_instantiate(a) template class PointerList; \ + template class PCursor; +#define IPL_instantiate(a) PL_instantiate(a); \ + template class IPointerList + +#include "plist.inl" + +#endif diff --git a/flower/include/plist.inl b/flower/include/plist.inl new file mode 100644 index 0000000000..82be364334 --- /dev/null +++ b/flower/include/plist.inl @@ -0,0 +1,21 @@ +/* -*-c++-*- + plist.inl -- part of flowerlib + + (c) 1996 Han-Wen Nienhuys& Jan Nieuwenhuizen +*/ + +#ifndef PLIST_INL +#define PLIST_INL + +template +void +PL_copy(IPointerList &to, IPointerList const&src) +{ + for (PCursor pc(src); pc.ok(); pc++) { + T *q = pc; + T *p=new T(*q) ; + to.bottom().add(p); + } +} + +#endif diff --git a/flower/lib/include/pcursor.hh b/flower/lib/include/pcursor.hh deleted file mode 100644 index f1a098f71b..0000000000 --- a/flower/lib/include/pcursor.hh +++ /dev/null @@ -1,68 +0,0 @@ -/* - pcursor.hh -- part of flowerlib - - (c) 1996 Han-Wen Nienhuys&Jan Nieuwenhuizen -*/ - -#ifndef PCURSOR_HH -#define PCURSOR_HH - -#include "plist.hh" -#include "cursor.hh" - -/** cursor to go with PointerList. - don't create PointerList's. - This cursor is just an interface class for Cursor. It takes care of the - appropriate type casts - */ -template -class PCursor : private Cursor { - friend class IPointerList; - - /// delete contents - void junk(); -public: - Cursor::ok; - Cursor::del; - Cursor::backspace; - T remove_p() { - T p = ptr(); - Cursor::del(); - return p; - } - T remove_prev_p() { - assert( ok() ); - (*this)--; - return remove_p(); - } - - PointerList &list() { return (PointerList&)Cursor::list(); } - PCursor operator++(int) { return Cursor::operator++(0);} - PCursor operator--(int) { return Cursor::operator--(0); } - PCursor operator+=(int i) { return Cursor::operator+=(i);} - PCursor operator-=(int i) { return Cursor::operator-=(i); } - PCursor operator -(int no) const { return Cursor::operator-(no);} - int operator -(PCursor op) const { return Cursor::operator-(op);} - PCursor operator +( int no) const {return Cursor::operator+(no);} PCursor(const PointerList & l) : Cursor (l) {} - - PCursor( const Cursor& cursor ) : Cursor(cursor) { } - void* vptr() const { return *((Cursor &) *this); } - - // should return T& ? - T ptr() const { return (T) vptr(); } - T operator ->() const { return ptr(); } - operator T() { return ptr(); } - T operator *() { return ptr(); } - void add(const T& p ) { Cursor::add((void*) p); } - void insert(const T& p ) { Cursor::insert((void*) p);} - static int compare(PCursor a,PCursorb) { - return Cursor::compare(a,b); - } -}; - - - -#include "compare.hh" -template_instantiate_compare(PCursor, PCursor::compare, template); - -#endif diff --git a/flower/lib/include/plist.hh b/flower/lib/include/plist.hh deleted file mode 100644 index 426b861c02..0000000000 --- a/flower/lib/include/plist.hh +++ /dev/null @@ -1,72 +0,0 @@ -/* - list.hh -- part of flowerlib - - (c) 1996 Han-Wen Nienhuys & Jan Nieuwenhuizen -*/ - -#ifndef PLIST_HH -#define PLIST_HH - -#include "list.hh" - -/** - A list of pointers. - - Use for list of pointers, e.g. PointerList. - This class does no deletion of the pointers, but it knows how to - copy itself (shallow copy). We could have derived it from List, - but this design saves a lot of code dup; for all PointerLists in the - program only one parent List is instantiated. - */ -template -class PointerList : public List -{ - public: - PCursor top() const{ - return PCursor (List::top()); - } - PCursor bottom() const { - return PCursor (List::bottom()); - } - PCursor find(T) const; - void concatenate(PointerList const &s) { List::concatenate(s); } - PointerList() {} -}; - -/** PointerList which deletes pointers given to it. - NOTE: - - The copy constructor doesn't do what you'd want: - Since T might have a virtual ctor, we don't try to do a - - new T(*cursor) - - You have to copy this yourself, or use the macro PointerList__copy - - */ -template -class IPointerList : public PointerList { -public: - IPointerList(const IPointerList&) { set_empty(); } - IPointerList() { } - ~IPointerList(); -}; - -#define IPointerList__copy(T, to, from, op) \ - for (PCursor _pc_(from); _pc_.ok(); _pc_++)\ - to.bottom().add(_pc_->op)\ - \ - - -template -void PL_copy(IPointerList &dst,IPointerList const&src); - - -#define PL_instantiate(a) template class PointerList; \ - template class PCursor; -#define IPL_instantiate(a) PL_instantiate(a); \ - template class IPointerList - -#include "plist.inl" - -#endif diff --git a/flower/lib/include/plist.inl b/flower/lib/include/plist.inl deleted file mode 100644 index 82be364334..0000000000 --- a/flower/lib/include/plist.inl +++ /dev/null @@ -1,21 +0,0 @@ -/* -*-c++-*- - plist.inl -- part of flowerlib - - (c) 1996 Han-Wen Nienhuys& Jan Nieuwenhuizen -*/ - -#ifndef PLIST_INL -#define PLIST_INL - -template -void -PL_copy(IPointerList &to, IPointerList const&src) -{ - for (PCursor pc(src); pc.ok(); pc++) { - T *q = pc; - T *p=new T(*q) ; - to.bottom().add(p); - } -} - -#endif diff --git a/flower/lib/string.cc b/flower/lib/string.cc deleted file mode 100644 index 9aa8bc9a92..0000000000 --- a/flower/lib/string.cc +++ /dev/null @@ -1,365 +0,0 @@ -/* - - string.cc - implement String - - (c) 1997 Han-Wen Nienhuys & Jan Nieuwenhuizen - - */ - -#include -#include - -#include -#include - -#include "string.hh" -#include "libc-extension.hh" -#include "string-convert.hh" - -#ifdef STRING_DEBUG -void* mymemmove( void* dest, void const* src, size_t n ); -#define memmove mymemmove -#endif - -// return array, alloced with new. -Byte* -String::copy_byte_p() const -{ - Byte const* src = strh_.byte_c_l(); - Byte* dest = new Byte[strh_.length_i() + 1]; - memcpy( dest, src, strh_.length_i() + 1 ); - return dest; -} -void -String::print_on(ostream& os) const -{ - if (!strh_.is_binary_bo()) - os << ch_c_l(); - else - for ( int i = 0; i < length_i(); i++ ) - os << (Byte)(*this)[ i ]; -} - -/* - copying, constructing. - */ -String& -String::operator = (String const&source ) -{ - strh_ = source.strh_; - return *this; -} - - -String::String(Rational r) -{ - *this = String_convert::rational_str(r); -} - -String::String (double f, char const* fmt) -{ - *this= String_convert::double_str(f,fmt); -} - -String::String( char c, int n ) -{ - *this = String_convert::char_str (c,n); -} - -/** - @see - String_convert::int_str - */ -String::String(int i, const char * format ) -{ - *this = String_convert::int_str(i,format); -} - -String::String (bool b) -{ - *this = (char const* ) (b ? "true" : "false"); -} - -String::String( char const* source ) -{ - assert(source); - strh_ = source; -} - -String::String( Byte const* byte_l, int length_i ) -{ - strh_.set( byte_l, length_i ); -} - -void -String::append(String s) -{ - strh_.append( s.byte_c_l(), s.length_i() ); -} -void -String::operator +=(String s) -{ - append(s); -} - -void -String::prepend(String s) -{ - s += *this; - *this = s; -} - -int -String::length_i() const -{ - return strh_.length_i(); -} - -Byte const* -String::byte_c_l() const -{ - return strh_.byte_c_l(); -} - -char const* -String::ch_c_l() const -{ - return strh_.ch_c_l(); -} - -Byte* -String::byte_l() -{ - return strh_.byte_l(); -} - -char* -String::ch_l() -{ - return strh_.ch_l(); -} - -/** - Do a signed comparison, analogous to memcmp; - */ -int -String::compare_i(String const& s1, String const& s2 ) -{ - Byte const* p1 = s1.byte_c_l(); - Byte const* p2 = s2.byte_c_l(); - if ( p1 == p2 ) - return 0; - - int i1 = s1.length_i(); - int i2 = s2.length_i(); - - int result= memcmp( p1, p2, i1 = 0 ) { - index_i += next_i; - next_i = right_str( length_i() - index_i - length ).index_i( string ); - } - return index_i; -} - -/** find a character. - - @return - the index of the leftmost character #c# (0 <= return < length_i()), - or -1 if not found. - - ? should return length_i()?, as in string.left_str(index_i(delimiter)) -*/ -int -String::index_i(char c ) const -{ - char const* me = strh_.ch_c_l(); - char const* p = (char const *) memchr( me,c, length_i()); - if ( p ) - return p - me; - return -1; -} - -/** - find the substring. - - @return - index of leftmost occurrence of #searchfor# - */ -int -String::index_i( String searchfor ) const -{ - char const* me = strh_.ch_c_l(); - char const* p = (char const *) memmem( - me, length_i(), searchfor.ch_c_l(), searchfor.length_i()); - - if ( p ) - return p - me; - else - return -1; -} - -/** find chars of a set. - - @return - the index of the leftmost occurance of an element of #set# - */ -int -String::index_any_i( String set ) const -{ - int n = length_i(); - if ( !n ) - return -1; - - const void * me_l = (const void*) strh_.ch_c_l(); - for (int i=0; i < set.length_i(); i++) { - char * found=(char*) memchr(me_l, set[i], n ); - if (found) { - return found - me_l; - } - } - return -1; -} - -String -String::left_str( int n ) const -{ - if (n >= length_i()) - return *this; - - String retval; - if (n < 1) - return retval; - - retval = *this; - retval.strh_.trunc(n); - return retval; -} - -String -String::right_str( int n ) const -{ - if (n > length_i()) - return *this; - - if ( n < 1) - return ""; - - return String( strh_.byte_c_l() + length_i() - n, n ); -} - - -String -String::nomid_str( int index_i, int n ) const -{ - if ( index_i < 0 ) { - n += index_i; - index_i = 0; - } - if ( n <= 0) - return *this; - - return - left_str( index_i ) + - right_str( length_i() - index_i - n ) ; -} - -/* - proposal: change to "cut()" - */ -String -String::mid_str( int index_i, int n ) const -{ - if (index_i <0) { - n += index_i; - index_i=0; - } - - if ( !length_i() || ( index_i < 0 ) || ( index_i >= length_i() ) || ( n < 1 ) ) - return String(); - - if ( ( n > length_i() ) || ( index_i + n > length_i() ) ) - n = length_i() - index_i; - - return String( byte_c_l() + index_i, n ); -} - -String -String::upper_str() const -{ - String str = *this; - str.to_upper(); - return str; -} -void -String::to_upper() -{ - char *s = (char*)strh_.byte_l(); - strnupr( s ,length_i()); -} - -void -String::to_lower() -{ - char* s = strh_.ch_l(); - strnlwr(s,length_i()); -} - - -String -String::lower_str() const -{ - String str = *this; - str.to_lower(); - return str; -} -String -String::reversed_str() const -{ - String str = *this; - strrev( str.byte_l(), str.length_i() ); - return str; -} - -int -String::value_i() const -{ - return String_convert::dec2_i( *this ); -} - -double -String::value_f() const -{ - return String_convert::dec2_f( *this ); -} - - diff --git a/flower/string.cc b/flower/string.cc new file mode 100644 index 0000000000..3eaa5c1b09 --- /dev/null +++ b/flower/string.cc @@ -0,0 +1,365 @@ +/* + + string.cc - implement String + + (c) 1997 Han-Wen Nienhuys & Jan Nieuwenhuizen + + */ + +#include +#include + +#include +#include + +#include "string.hh" +#include "libc-extension.hh" +#include "string-convert.hh" + +#ifdef STRING_DEBUG +void* mymemmove( void* dest, void const* src, size_t n ); +#define memmove mymemmove +#endif + +// return array, alloced with new. +Byte* +String::copy_byte_p() const +{ + Byte const* src = strh_.byte_c_l(); + Byte* dest = new Byte[strh_.length_i() + 1]; + memcpy( dest, src, strh_.length_i() + 1 ); + return dest; +} +void +String::print_on(ostream& os) const +{ + if (!strh_.is_binary_bo()) + os << ch_c_l(); + else + for ( int i = 0; i < length_i(); i++ ) + os << (Byte)(*this)[ i ]; +} + +/* + copying, constructing. + */ +String& +String::operator = (String const&source ) +{ + strh_ = source.strh_; + return *this; +} + + +String::String(Rational r) +{ + *this = String_convert::rational_str(r); +} + +String::String (double f, char const* fmt) +{ + *this= String_convert::double_str(f,fmt); +} + +String::String( char c, int n ) +{ + *this = String_convert::char_str (c,n); +} + +/** + @see + String_convert::int_str + */ +String::String(int i, char const * format ) +{ + *this = String_convert::int_str(i,format); +} + +String::String (bool b) +{ + *this = (char const* ) (b ? "true" : "false"); +} + +String::String( char const* source ) +{ + assert(source); + strh_ = source; +} + +String::String( Byte const* byte_l, int length_i ) +{ + strh_.set( byte_l, length_i ); +} + +void +String::append(String s) +{ + strh_.append( s.byte_c_l(), s.length_i() ); +} +void +String::operator +=(String s) +{ + append(s); +} + +void +String::prepend(String s) +{ + s += *this; + *this = s; +} + +int +String::length_i() const +{ + return strh_.length_i(); +} + +Byte const* +String::byte_c_l() const +{ + return strh_.byte_c_l(); +} + +char const* +String::ch_c_l() const +{ + return strh_.ch_c_l(); +} + +Byte* +String::byte_l() +{ + return strh_.byte_l(); +} + +char* +String::ch_l() +{ + return strh_.ch_l(); +} + +/** + Do a signed comparison, analogous to memcmp; + */ +int +String::compare_i(String const& s1, String const& s2 ) +{ + Byte const* p1 = s1.byte_c_l(); + Byte const* p2 = s2.byte_c_l(); + if ( p1 == p2 ) + return 0; + + int i1 = s1.length_i(); + int i2 = s2.length_i(); + + int result= memcmp( p1, p2, i1 = 0 ) { + index_i += next_i; + next_i = right_str( length_i() - index_i - length ).index_i( string ); + } + return index_i; +} + +/** find a character. + + @return + the index of the leftmost character #c# (0 <= return < length_i()), + or -1 if not found. + + ? should return length_i()?, as in string.left_str(index_i(delimiter)) +*/ +int +String::index_i(char c ) const +{ + char const* me = strh_.ch_c_l(); + char const* p = (char const *) memchr( me,c, length_i()); + if ( p ) + return p - me; + return -1; +} + +/** + find the substring. + + @return + index of leftmost occurrence of #searchfor# + */ +int +String::index_i( String searchfor ) const +{ + char const* me = strh_.ch_c_l(); + char const* p = (char const *) memmem( + me, length_i(), searchfor.ch_c_l(), searchfor.length_i()); + + if ( p ) + return p - me; + else + return -1; +} + +/** find chars of a set. + + @return + the index of the leftmost occurance of an element of #set# + */ +int +String::index_any_i( String set ) const +{ + int n = length_i(); + if ( !n ) + return -1; + + void const * me_l = (void const *) strh_.ch_c_l(); + for (int i=0; i < set.length_i(); i++) { + char * found=(char*) memchr(me_l, set[i], n ); + if (found) { + return found - me_l; + } + } + return -1; +} + +String +String::left_str( int n ) const +{ + if (n >= length_i()) + return *this; + + String retval; + if (n < 1) + return retval; + + retval = *this; + retval.strh_.trunc(n); + return retval; +} + +String +String::right_str( int n ) const +{ + if (n > length_i()) + return *this; + + if ( n < 1) + return ""; + + return String( strh_.byte_c_l() + length_i() - n, n ); +} + + +String +String::nomid_str( int index_i, int n ) const +{ + if ( index_i < 0 ) { + n += index_i; + index_i = 0; + } + if ( n <= 0) + return *this; + + return + left_str( index_i ) + + right_str( length_i() - index_i - n ) ; +} + +/* + proposal: change to "cut()" + */ +String +String::mid_str( int index_i, int n ) const +{ + if (index_i <0) { + n += index_i; + index_i=0; + } + + if ( !length_i() || ( index_i < 0 ) || ( index_i >= length_i() ) || ( n < 1 ) ) + return String(); + + if ( ( n > length_i() ) || ( index_i + n > length_i() ) ) + n = length_i() - index_i; + + return String( byte_c_l() + index_i, n ); +} + +String +String::upper_str() const +{ + String str = *this; + str.to_upper(); + return str; +} +void +String::to_upper() +{ + char *s = (char*)strh_.byte_l(); + strnupr( s ,length_i()); +} + +void +String::to_lower() +{ + char* s = strh_.ch_l(); + strnlwr(s,length_i()); +} + + +String +String::lower_str() const +{ + String str = *this; + str.to_lower(); + return str; +} +String +String::reversed_str() const +{ + String str = *this; + strrev( str.byte_l(), str.length_i() ); + return str; +} + +int +String::value_i() const +{ + return String_convert::dec2_i( *this ); +} + +double +String::value_f() const +{ + return String_convert::dec2_f( *this ); +} + +