Disk ARchive  2.4.12
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines
secu_string.hpp
Go to the documentation of this file.
00001 /*********************************************************************/
00002 // dar - disk archive - a backup/restoration program
00003 // Copyright (C) 2002-2052 Denis Corbin
00004 //
00005 // This program is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU General Public License
00007 // as published by the Free Software Foundation; either version 2
00008 // of the License, or (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, write to the Free Software
00017 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00018 //
00019 // to contact the author : http://dar.linux.free.fr/email.html
00020 /*********************************************************************/
00021 
00031 
00032 #ifndef SECU_STRING_HPP
00033 #define SECU_STRING_HPP
00034 
00035 #include "../my_config.h"
00036 
00037 #include <string>
00038 #include "integers.hpp"
00039 #include "special_alloc.hpp"
00040 
00041 namespace libdar
00042 {
00043 
00046 
00048 
00056 
00057     class secu_string
00058     {
00059     public:
00061 
00066         static bool is_string_secured();
00067 
00069 
00072         secu_string(U_I size = 0) { init(size); };
00073 
00075 
00077         secu_string(const char *ptr, U_I size) { init(size); append(ptr, size); };
00078 
00080         secu_string(const secu_string & ref) { copy_from(ref); };
00081 
00082 
00084         secu_string & operator = (const secu_string & ref) { clean_and_destroy(); copy_from(ref); return *this; };
00085 
00086         bool operator != (const std::string & ref) const { return ! (*this == ref); };
00087         bool operator != (const secu_string & ref) const { return ! (*this == ref); };
00088         bool operator == (const std::string &ref) const { return compare_with(ref.c_str(),(U_I)(ref.size())); };
00089         bool operator == (const secu_string &ref) const { return compare_with(ref.mem, *ref.string_size); };
00090 
00092         ~secu_string() { clean_and_destroy(); };
00093 
00095 
00100         void read(int fd, U_I size);
00101 
00103 
00110         void append(const char *ptr, U_I size);
00111 
00113         void append(int fd, U_I size);
00114 
00117         void reduce_string_size_to(U_I pos);
00118 
00120         void clear() { clean_and_destroy(); init(0); };
00121 
00123 
00125         void clear_and_resize(U_I size) { clean_and_destroy(); init(size); };
00126 
00127         void clear_and_not_resize() {  string_size = 0; };
00128 
00130 
00134         const char*c_str() const { return mem == NULL ? throw SRC_BUG : mem; };
00135 
00137 
00140         U_I size() const { return *string_size; }; // returns the size of the string
00141 
00142 #ifdef LIBDAR_SPECIAL_ALLOC
00143         USE_SPECIAL_ALLOC(secu_string);
00144 #endif
00145     private:
00146         U_I *allocated_size;
00147         char *mem;
00148         U_I *string_size;
00149 
00150         void init(U_I size);
00151         void copy_from(const secu_string & ref);
00152         bool compare_with(const char *ptr, U_I size) const;
00153         void clean_and_destroy();
00154     };
00155 
00157 
00158 } // end of namespace
00159 
00160 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines