Disk ARchive  2.4.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
secu_string.hpp
Go to the documentation of this file.
1 /*********************************************************************/
2 // dar - disk archive - a backup/restoration program
3 // Copyright (C) 2002-2052 Denis Corbin
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 //
19 // to contact the author : http://dar.linux.free.fr/email.html
20 /*********************************************************************/
21 // $Id: secu_string.hpp,v 1.4 2011/01/09 17:25:58 edrusb Rel $
22 //
23 /*********************************************************************/
24 //
25 
35 
36 #ifndef SECU_STRING_HPP
37 #define SECU_STRING_HPP
38 
39 #include "../my_config.h"
40 
41 #include <string>
42 #include "integers.hpp"
43 
44 
45 namespace libdar
46 {
47 
50 
52 
60 
62  {
63  public:
65 
70  static bool is_string_secured();
71 
73 
76  secu_string(U_I size = 0) { init(size); };
77 
79 
81  secu_string(const char *ptr, U_I size) { init(size); append(ptr, size); };
82 
84  secu_string(const secu_string & ref) { copy_from(ref); };
85 
86 
88  secu_string & operator = (const secu_string & ref) { clean_and_destroy(); copy_from(ref); return *this; };
89 
90  bool operator != (const std::string & ref) const { return ! (*this == ref); };
91  bool operator != (const secu_string & ref) const { return ! (*this == ref); };
92  bool operator == (const std::string &ref) const { return compare_with(ref.c_str(),(U_I)(ref.size())); };
93  bool operator == (const secu_string &ref) const { return compare_with(ref.mem, *ref.string_size); };
94 
96  ~secu_string() { clean_and_destroy(); };
97 
99 
104  void read(int fd, U_I size);
105 
107 
114  void append(const char *ptr, U_I size);
115 
117  void append(int fd, U_I size);
118 
121  void reduce_string_size_to(U_I pos);
122 
124  void clear() { clean_and_destroy(); init(0); };
125 
127 
129  void clear_and_resize(U_I size) { clean_and_destroy(); init(size); };
130 
131  void clear_and_not_resize() { string_size = 0; };
132 
134 
138  const char*c_str() const { return mem == NULL ? throw SRC_BUG : mem; };
139 
141 
144  U_I size() const { return *string_size; }; // returns the size of the string
145 
146  private:
147  U_I *allocated_size;
148  char *mem;
149  U_I *string_size;
150 
151  void init(U_I size);
152  void copy_from(const secu_string & ref);
153  bool compare_with(const char *ptr, U_I size) const;
154  void clean_and_destroy();
155  };
156 
158 
159 } // end of namespace
160 
161 #endif