nis-util  1.0.D108
lib/rcstring.h
Go to the documentation of this file.
00001 //
00002 // nis-util - NIS Administration Utilities
00003 // Copyright (C) 2001, 2008, 2009, 2011, 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 2 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 LIB_RCSTRING_H
00020 #define LIB_RCSTRING_H
00021 
00022 #include <lib/ac/stddef.h>
00023 #include <lib/ac/stdarg.h>
00024 #include <lib/gcc-attr.h>
00025 
00026 #include <vector> // for break_up method
00027 
00028 class rcstring
00029 {
00030 public:
00034     virtual ~rcstring()
00035     {
00036         value->one_less();
00037     }
00038 
00042     rcstring()
00043     {
00044         value = string_ty::from_c("");
00045     }
00046 
00050     rcstring(const char *s)
00051     {
00052         value = string_ty::from_c(s);
00053     }
00054 
00058     rcstring(const char *s, size_t len)
00059     {
00060         value = string_ty::n_from_c(s, len);
00061     }
00062 
00066     rcstring(const rcstring &arg)
00067     {
00068         value = arg.value;
00069         value->one_more();
00070     }
00071 
00075     rcstring &
00076     operator=(const rcstring &arg)
00077     {
00078         if (this != &arg)
00079         {
00080             value->one_less();
00081             value = arg.value;
00082             value->one_more();
00083         }
00084         return *this;
00085     }
00086 
00087     const char *
00088     c_str(void)
00089         const
00090     {
00091         return value->c_str();
00092     }
00093 
00094     typedef unsigned long hash_ty;
00095 
00096     hash_ty
00097     get_hash(void)
00098         const
00099     {
00100         return value->get_hash();
00101     }
00102 
00107     rcstring trim_right(void) const;
00108 
00116     static rcstring spaces(size_t n);
00117 
00123     bool is_printable(void) const;
00124 
00130     bool is_lower_case(void) const;
00131 
00136     bool starts_with(const rcstring &prefix) const;
00137 
00142     bool ends_with(const rcstring &suffix) const;
00143 
00156     rcstring replace(const rcstring &from, const rcstring &to, size_t ntimes)
00157         const;
00158 
00163     unsigned char front(void) const;
00164 
00169     unsigned char back(void) const;
00170 
00175     rcstring basename(void) const;
00176 
00181     rcstring dirname(void) const;
00182 
00187     rcstring identifier(void) const;
00188 
00194     rcstring trim(void) const;
00195 
00196 private:
00201     class string_ty
00202     {
00203     public:
00207         string_ty(const char *);
00208 
00212         string_ty(const char *, size_t);
00213 
00217         void one_more(void);
00218 
00223         void one_less(void);
00224 
00225         const char *
00226         c_str(void)
00227             const
00228         {
00229             return text;
00230         }
00231 
00232         bool
00233         empty(void)
00234             const
00235         {
00236             return (str_length == 0);
00237         }
00238 
00239         size_t
00240         size(void)
00241             const
00242         {
00243             return str_length;
00244         }
00245 
00246         rcstring::hash_ty
00247         get_hash(void)
00248             const
00249         {
00250             return str_hash;
00251         }
00252 
00253     private:
00257         ~string_ty();
00258 
00259         string_ty(); // do not use
00260         string_ty(const string_ty &); // do not use
00261         string_ty &operator=(const string_ty &); // do not use
00262 
00263         rcstring::hash_ty str_hash;
00264         string_ty *next;
00265         long references;
00266         size_t str_length;
00267         char *text;
00268 
00269         static void initialize(void);
00270         static void split(void);
00271 
00272         static string_ty *from_c(const char *);
00273         static string_ty *n_from_c(const char *, size_t);
00274         void construct(const char *, size_t);
00275 
00276         static string_ty **hash_table;
00277         static rcstring::hash_ty hash_modulus;
00278         static rcstring::hash_ty hash_mask;
00279         static rcstring::hash_ty hash_load;
00280 
00281         friend class rcstring;
00282     };
00283 
00288     string_ty *value;
00289 
00290 public:
00294     rcstring upcase(void) const;
00295 
00299     rcstring downcase(void) const;
00300 
00304     bool
00305     operator==(const rcstring &rhs)
00306         const
00307     {
00308         return (this->value == rhs.value);
00309     }
00310 
00314     bool
00315     operator!=(const rcstring &rhs)
00316         const
00317     {
00318         return (this->value != rhs.value);
00319     }
00320 
00321     static rcstring format(const char *, ...)    ATTRIBUTE_PRINTF(1, 2);
00322     static rcstring vformat(const char *, va_list)  ATTRIBUTE_VPRINTF(1, 2);
00323 
00324     bool
00325     empty(void)
00326         const
00327     {
00328         return value->empty();
00329     }
00330 
00331     size_t
00332     size(void)
00333         const
00334     {
00335         return value->size();
00336     }
00337 
00342     std::vector<rcstring> break_up(const char *sep = 0, bool ignsp = true)
00343         const;
00344 
00348     rcstring substr(size_t start, size_t len) const;
00349 
00354     bool to_long(long &n, int base = 10) const;
00355 
00360     static rcstring catenate(const rcstring &, const rcstring &);
00361 
00365     rcstring &
00366     operator+=(const rcstring &arg)
00367     {
00368         return operator=(catenate(*this, arg));
00369     }
00370 
00375     rcstring quote_c(void) const;
00376 
00382     rcstring quote_shell(void) const;
00383 
00389     char operator[](int idx) const;
00390 };
00391 
00392 inline rcstring
00393 operator+(const rcstring &lhs, const rcstring &rhs)
00394 {
00395     return rcstring::catenate(lhs, rhs);
00396 }
00397 
00398 bool operator<(const rcstring &lhs, const rcstring &rhs);
00399 bool operator<=(const rcstring &lhs, const rcstring &rhs);
00400 bool operator>(const rcstring &lhs, const rcstring &rhs);
00401 bool operator>=(const rcstring &lhs, const rcstring &rhs);
00402 
00403 // vim: set ts=8 sw=4 et :
00404 #endif // LIB_RCSTRING_H