Aegis  4.25.D505
/home/archives/aegis/branch.4/branch.25/delta28933.505/common/nstring.h
Go to the documentation of this file.
00001 //
00002 // aegis - project change supervisor
00003 // Copyright (C) 2004-2010, 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 (at
00008 // 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 GNU
00013 // 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 <http://www.gnu.org/licenses/>.
00017 //
00018 
00019 #ifndef COMMON_NSTRING_H
00020 #define COMMON_NSTRING_H
00021 
00022 #include <common/str.h>
00023 
00024 class nstring_list; // forward
00025 
00034 class nstring
00035 {
00036 public:
00042     ~nstring()
00043     {
00044         str_free(ref);
00045         ref = 0;
00046     }
00047 
00051     nstring() :
00052         ref(get_empty_ref())
00053     {
00054     }
00055 
00059     nstring(const char *arg) :
00060         ref(arg ? str_from_c(arg) : get_empty_ref())
00061     {
00062     }
00063 
00067     nstring(const char *data, size_t len) :
00068         ref(str_n_from_c(data, len))
00069     {
00070     }
00071 
00075     explicit
00076     nstring(string_ty *arg) :
00077         ref(arg ? str_copy(arg) : get_empty_ref())
00078     {
00079     }
00080 
00084     nstring(const nstring &arg) :
00085         ref(str_copy(arg.ref))
00086     {
00087     }
00088 
00092     nstring &
00093     operator=(const nstring &arg)
00094     {
00095         if (this != &arg)
00096         {
00097             str_free(ref);
00098             ref = (arg.ref ? str_copy(arg.ref) : get_empty_ref());
00099         }
00100         return *this;
00101     }
00102 
00103     // ----------  A  ------------------------------------------------------
00104     // ----------  B  ------------------------------------------------------
00105 
00110     unsigned char back(void) const;
00111 
00117     nstring basename(const nstring &suffix = "") const;
00118 
00119     // ----------  C  ------------------------------------------------------
00120 
00133     nstring capitalize(void) const;
00134 
00150     nstring
00151     catenate(const nstring &arg)
00152         const
00153     {
00154         string_ty *tmp = str_catenate(ref, arg.ref);
00155         nstring result(tmp);
00156         str_free(tmp);
00157         return result;
00158     }
00159 
00177     nstring cat_three(const nstring &str2, const nstring &str3) const;
00178 
00183     void clear(void);
00184 
00189     const char *
00190     c_str(void)
00191         const
00192     {
00193         return ref->str_text;
00194     }
00195 
00196     // ----------  D  ------------------------------------------------------
00197 
00202     nstring dirname(void) const;
00203 
00217     nstring downcase(void) const;
00218 
00219     // ----------  E  ------------------------------------------------------
00220 
00221     bool
00222     empty(void)
00223         const
00224     {
00225         return (ref->str_length == 0);
00226     }
00227 
00235     bool ends_with(const nstring &suffix) const;
00236 
00245     bool ends_with_nocase(const nstring &suffix) const;
00246 
00265     bool
00266     equal(const nstring &arg)
00267         const
00268     {
00269         return (ref == arg.ref);
00270     }
00271 
00272     // ----------  F  ------------------------------------------------------
00273 
00292     nstring field(char sep, int nth) const;
00293 
00299     nstring first_dirname(void) const;
00300 
00317     static nstring format(const char *fmt, ...)               ATTR_PRINTF(1, 2);
00318 
00323     unsigned char front(void) const { return ref->str_text[0]; }
00324 
00325     // ----------  G  ------------------------------------------------------
00326 
00332     nstring get_extension(void) const;
00333 
00340     str_hash_ty get_hash(void) const { return ref->str_hash; }
00341 
00350     string_ty *
00351     get_ref(void)
00352         const
00353     {
00354         return ref;
00355     }
00356 
00366     string_ty *
00367     get_ref_copy(void)
00368         const
00369     {
00370         return str_copy(ref);
00371     }
00372 
00382     bool gmatch(const char *pattern) const;
00383 
00393     bool gmatch(const nstring &pattern) const;
00394 
00405     bool gmatch(const nstring_list &pattern) const;
00406 
00407     // ----------  H  ------------------------------------------------------
00408 
00420     nstring html_quote(bool para = false) const;
00421 
00431     nstring html_unquote(void) const;
00432 
00433     // ----------  I  ------------------------------------------------------
00434 
00440     nstring identifier(void) const;
00441 
00442     // ----------  J  ------------------------------------------------------
00443     // ----------  K  ------------------------------------------------------
00444     // ----------  L  ------------------------------------------------------
00445 
00446     size_t
00447     length(void)
00448         const
00449     {
00450         return ref->str_length;
00451     }
00452 
00463     int len_printable(int maxlen) const;
00464 
00465     // ----------  M  ------------------------------------------------------
00466     // ----------  N  ------------------------------------------------------
00467     // ----------  O  ------------------------------------------------------
00468 
00469     nstring
00470     operator+(const nstring &arg)
00471         const
00472     {
00473         string_ty *tmp = str_catenate(ref, arg.ref);
00474         nstring result(tmp);
00475         str_free(tmp);
00476         return result;
00477     }
00478 
00479     nstring &
00480     operator+=(const nstring &arg)
00481     {
00482         if (!arg.empty())
00483         {
00484             string_ty *s = str_catenate(ref, arg.ref);
00485             str_free(ref);
00486             ref = s;
00487         }
00488         return *this;
00489     }
00490 
00504     operator bool() const;
00505 
00510     bool operator!() const;
00511 
00518     bool
00519     operator==(const nstring &rhs)
00520         const
00521     {
00522         return (ref == rhs.ref);
00523     }
00524 
00531     bool
00532     operator!=(const nstring &rhs)
00533         const
00534     {
00535         return (ref != rhs.ref);
00536     }
00537 
00544     bool operator<(const nstring &rhs) const;
00545 
00552     bool operator<=(const nstring &rhs) const;
00553 
00560     bool operator>(const nstring &rhs) const;
00561 
00568     bool operator>=(const nstring &rhs) const;
00569 
00581     char
00582     operator[](size_t n)
00583         const
00584     {
00585         return (n < size() ? ref->str_text[n] : '\0');
00586     }
00587 
00588     // ----------  P  ------------------------------------------------------
00589     // ----------  Q  ------------------------------------------------------
00590 
00598     nstring quote_c(void) const;
00599 
00612     nstring quote_shell(void) const;
00613 
00622     nstring quote_cook(void) const;
00623 
00624     // ----------  R  ------------------------------------------------------
00625 
00644     nstring replace(const nstring &lhs, const nstring &rhs, int maximum = -1)
00645         const;
00646 
00647     // ----------  S  ------------------------------------------------------
00648 
00649     size_t
00650     size(void)
00651         const
00652     {
00653         return ref->str_length;
00654     }
00655 
00666     nstring snip(void) const;
00667 
00675     bool starts_with(const nstring &prefix) const;
00676 
00690     nstring substr(long start, long nbytes) const;
00691 
00692     // ----------  T  ------------------------------------------------------
00693 
00698     long to_long(void) const;
00699 
00711     nstring trim(void) const;
00712 
00718     nstring trim_extension(void) const;
00719 
00725     nstring trim_first_directory(void) const;
00726 
00739     nstring trim_lines(void) const;
00740 
00745     nstring trim_right(void) const;
00746 
00747     // ----------  U  ------------------------------------------------------
00748 
00762     nstring upcase(void) const;
00763 
00770     nstring url_quote(void) const;
00771 
00777     nstring url_unquote(void) const;
00778 
00779     // ----------  V  ------------------------------------------------------
00780 
00792     bool
00793     valid(void)
00794         const
00795     {
00796         return str_validate(ref);
00797     }
00798 
00820     static nstring vformat(const char *fmt, va_list ap);
00821 
00822     // ----------  W  ------------------------------------------------------
00823     // ----------  X  ------------------------------------------------------
00824     // ----------  Y  ------------------------------------------------------
00825     // ----------  Z  ------------------------------------------------------
00826 
00827 private:
00833     string_ty *ref;
00834 
00839     static string_ty *get_empty_ref(void);
00840 };
00841 
00842 inline nstring
00843 operator+(const char *lhs, const nstring &rhs)
00844 {
00845     return nstring(lhs).catenate(rhs);
00846 }
00847 
00848 inline nstring
00849 operator+(const nstring &lhs, const char *rhs)
00850 {
00851     return lhs.catenate(nstring(rhs));
00852 }
00853 
00854 // vim: set ts=8 sw=4 et :
00855 #endif // COMMON_NSTRING_H