xref: /aoo42x/main/autodoc/inc/cosv/stringdata.hxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #ifndef COSV_STRINGDATA_HXX
29 #define COSV_STRINGDATA_HXX
30 
31 
32 #include <cosv/str_types.hxx>
33 
34 
35 
36 namespace csv
37 {
38 
39 /** @tpl CHAR
40     The expression CHAR(0) has to be valid.
41 */
42 template <class CHAR>
43 class StringData
44 {
45   public:
46     typedef StringData          self;
47 
48     typedef str::size           size_type;
49     typedef str::position       position_type;
50 
51     // LIFECYCLE
52                         StringData();
53     /** @precond i_pData != 0
54         @precond i_nValidLength <= strlen(i_pData)
55     */
56                         StringData(
57                             const CHAR *        i_pData,
58                             size_type           i_nValidLength );
59                         ~StringData();
60     // OPERATORS
61 
62     // OPERATIONS
63 
64     // INQUIRY
65     const CHAR *        Data() const;
66 
67     /** @returns the allocated number of CHAR.
68         This may be different from the number of bytes.
69         There is actually allocated one more CHAR,
70         which is guaranteed to be CHAR(0) in all circumstances.
71     */
72     size_type           Size() const;
73 
74   private:
75     /*  Because this is used only within a refcounted structure,
76         these functions are forbidden - at least yet.
77     */
78                         StringData(const self&);
79     self &              operator=(const self&);
80 
81     // DATA
82     DYN CHAR *          dpData;
83     size_type           nSize;      /// The allocated size - 1 (for the finishing 0).
84 };
85 
86 
87 
88 // IMPLEMENTATION
89 
90 template <class CHAR>
91 StringData<CHAR>::StringData()
92     :   dpData( new CHAR[1] ),
93         nSize(0)
94 {
95     *dpData = CHAR(0);
96 }
97 
98 template <class CHAR>
99 StringData<CHAR>::StringData( const CHAR *        i_pData,
100                               size_type           i_nValidLength )
101     :   dpData( new CHAR[i_nValidLength + 1] ),
102         nSize(i_nValidLength)
103 {
104     memcpy( dpData, i_pData, i_nValidLength * sizeof(CHAR) );
105     dpData[nSize] = CHAR(0);
106 }
107 
108 template <class CHAR>
109 StringData<CHAR>::~StringData()
110 {
111     delete [] dpData;
112 }
113 
114 template <class CHAR>
115 const CHAR *
116 StringData<CHAR>::Data() const
117 {
118     return dpData;
119 }
120 
121 template <class CHAR>
122 typename StringData<CHAR>::size_type
123 StringData<CHAR>::Size() const
124 {
125     return nSize;
126 }
127 
128 
129 
130 }   // namespace csv
131 
132 
133 #endif
134 
135 
136