Aegis  4.25.D505
/home/archives/aegis/branch.4/branch.25/delta28933.505/common/wstring.h
Go to the documentation of this file.
00001 //
00002 //      aegis - project change supervisor
00003 //      Copyright (C) 2004-2008, 2012 Peter Miller
00004 //
00005 //      This program is free software; you can redistribute it and/or modify
00006 //      it under the terms of the GNU General Public License as published by
00007 //      the Free Software Foundation; either version 3 of the License, or
00008 //      (at your option) any later version.
00009 //
00010 //      This program is distributed in the hope that it will be useful,
00011 //      but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 //      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 //      GNU General Public License for more details.
00014 //
00015 //      You should have received a copy of the GNU General Public License
00016 //      along with this program. If not, see
00017 //      <http://www.gnu.org/licenses/>.
00018 //
00019 
00020 #ifndef COMMON_WSTRING_H
00021 #define COMMON_WSTRING_H
00022 
00029 #include <common/wstr.h>
00030 
00031 class nstring; // forward
00032 
00041 class wstring
00042 {
00043 public:
00049     ~wstring();
00050 
00054     wstring() :
00055         ref(get_empty_ref())
00056     {
00057     }
00058 
00062     explicit
00063     wstring(const char *arg) :
00064         ref(arg ? wstr_from_c(arg) : get_empty_ref())
00065     {
00066     }
00067 
00071     explicit
00072     wstring(const wchar_t *arg) :
00073         ref(arg ? wstr_from_wc(arg) : get_empty_ref())
00074     {
00075     }
00076 
00080     wstring(const char *data, size_t len) :
00081         ref(wstr_n_from_c(data, len))
00082     {
00083     }
00084 
00088     wstring(const wchar_t *data, size_t len) :
00089         ref(wstr_n_from_wc(data, len))
00090     {
00091     }
00092 
00096     wstring(wstring_ty *arg) :
00097         ref(arg ? wstr_copy(arg) : get_empty_ref())
00098     {
00099     }
00100 
00106     wstring(string_ty *arg);
00107 
00111     wstring(const nstring &arg);
00112 
00116     wstring(const wstring &arg) :
00117         ref(wstr_copy(arg.ref))
00118     {
00119     }
00120 
00124     wstring &
00125     operator=(const wstring &arg)
00126     {
00127         if (this != &arg)
00128         {
00129             wstr_free(ref);
00130             ref = wstr_copy(arg.ref);
00131         }
00132         return *this;
00133     }
00134 
00139     void clear();
00140 
00145     const wchar_t *
00146     c_str()
00147         const
00148     {
00149         return ref->wstr_text;
00150     }
00151 
00156     nstring to_nstring() const;
00157 
00158     bool
00159     empty()
00160         const
00161     {
00162         return (ref->wstr_length == 0);
00163     }
00164 
00165     size_t
00166     size()
00167         const
00168     {
00169         return ref->wstr_length;
00170     }
00171 
00172     size_t
00173     length()
00174         const
00175     {
00176         return ref->wstr_length;
00177     }
00178 
00194     wstring
00195     catenate(const wstring &arg)
00196         const
00197     {
00198         return wstring(wstr_catenate(ref, arg.ref));
00199     }
00200 
00201     wstring
00202     operator+(const wstring &arg)
00203         const
00204     {
00205         return wstring(wstr_catenate(ref, arg.ref));
00206     }
00207 
00208     wstring &
00209     operator+=(const wstring &arg)
00210     {
00211         if (!arg.empty())
00212         {
00213             wstring_ty *s = wstr_catenate(ref, arg.ref);
00214             wstr_free(ref);
00215             ref = s;
00216         }
00217         return *this;
00218     }
00219 
00237     wstring
00238     cat_three(const wstring &str2, const wstring &str3)
00239         const
00240     {
00241         return wstring(wstr_cat_three(ref, str2.ref, str3.ref));
00242     }
00243 
00257     wstring
00258     upcase()
00259         const
00260     {
00261         return wstring(wstr_to_upper(ref));
00262     }
00263 
00277     wstring
00278     downcase()
00279         const
00280     {
00281         return wstring(wstr_to_lower(ref));
00282     }
00283 
00296     wstring
00297     capitalize()
00298         const
00299     {
00300         return wstring(wstr_capitalize(ref));
00301     }
00302 
00321     bool
00322     equal(const wstring &arg)
00323         const
00324     {
00325         return (ref == arg.ref);
00326     }
00327 
00328     bool
00329     operator==(const wstring &arg)
00330         const
00331     {
00332         return (ref == arg.ref);
00333     }
00334 
00335     bool
00336     operator!=(const wstring &arg)
00337         const
00338     {
00339         return (ref != arg.ref);
00340     }
00341 
00350     wstring_ty *
00351     get_ref()
00352         const
00353     {
00354         return ref;
00355     }
00356 
00362     wstring
00363     identifier()
00364         const
00365     {
00366         return wstr_to_ident(get_ref());
00367     }
00368 
00380     char
00381     operator[](size_t n)
00382         const
00383     {
00384         return (n < size() ? ref->wstr_text[n] : L'\0');
00385     }
00386 
00393     int
00394     column_width()
00395         const
00396     {
00397         return wstr_column_width(ref);
00398     }
00399 
00400 private:
00406     wstring_ty *ref;
00407 
00412     static wstring_ty *
00413     get_empty_ref()
00414     {
00415         return wstr_from_c("");
00416     }
00417 };
00418 
00419 inline wstring
00420 operator+(const char *lhs, const wstring &rhs)
00421 {
00422     return wstring(lhs).catenate(rhs);
00423 }
00424 
00425 inline wstring
00426 operator+(const wstring &lhs, const char *rhs)
00427 {
00428     return lhs.catenate(wstring(rhs));
00429 }
00430 
00433 #endif // COMMON_WSTRING_H
00434 // vim: set ts=8 sw=4 et :