1*ac3d0c65SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*ac3d0c65SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*ac3d0c65SAndrew Rist * or more contributor license agreements. See the NOTICE file 5*ac3d0c65SAndrew Rist * distributed with this work for additional information 6*ac3d0c65SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*ac3d0c65SAndrew Rist * to you under the Apache License, Version 2.0 (the 8*ac3d0c65SAndrew Rist * "License"); you may not use this file except in compliance 9*ac3d0c65SAndrew Rist * with the License. You may obtain a copy of the License at 10cdf0e10cSrcweir * 11*ac3d0c65SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12cdf0e10cSrcweir * 13*ac3d0c65SAndrew Rist * Unless required by applicable law or agreed to in writing, 14*ac3d0c65SAndrew Rist * software distributed under the License is distributed on an 15*ac3d0c65SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*ac3d0c65SAndrew Rist * KIND, either express or implied. See the License for the 17*ac3d0c65SAndrew Rist * specific language governing permissions and limitations 18*ac3d0c65SAndrew Rist * under the License. 19cdf0e10cSrcweir * 20*ac3d0c65SAndrew Rist *************************************************************/ 21*ac3d0c65SAndrew Rist 22*ac3d0c65SAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir #ifndef COSV_STRINGDATA_HXX 25cdf0e10cSrcweir #define COSV_STRINGDATA_HXX 26cdf0e10cSrcweir 27cdf0e10cSrcweir 28cdf0e10cSrcweir #include <cosv/str_types.hxx> 29cdf0e10cSrcweir 30cdf0e10cSrcweir 31cdf0e10cSrcweir 32cdf0e10cSrcweir namespace csv 33cdf0e10cSrcweir { 34cdf0e10cSrcweir 35cdf0e10cSrcweir /** @tpl CHAR 36cdf0e10cSrcweir The expression CHAR(0) has to be valid. 37cdf0e10cSrcweir */ 38cdf0e10cSrcweir template <class CHAR> 39cdf0e10cSrcweir class StringData 40cdf0e10cSrcweir { 41cdf0e10cSrcweir public: 42cdf0e10cSrcweir typedef StringData self; 43cdf0e10cSrcweir 44cdf0e10cSrcweir typedef str::size size_type; 45cdf0e10cSrcweir typedef str::position position_type; 46cdf0e10cSrcweir 47cdf0e10cSrcweir // LIFECYCLE 48cdf0e10cSrcweir StringData(); 49cdf0e10cSrcweir /** @precond i_pData != 0 50cdf0e10cSrcweir @precond i_nValidLength <= strlen(i_pData) 51cdf0e10cSrcweir */ 52cdf0e10cSrcweir StringData( 53cdf0e10cSrcweir const CHAR * i_pData, 54cdf0e10cSrcweir size_type i_nValidLength ); 55cdf0e10cSrcweir ~StringData(); 56cdf0e10cSrcweir // OPERATORS 57cdf0e10cSrcweir 58cdf0e10cSrcweir // OPERATIONS 59cdf0e10cSrcweir 60cdf0e10cSrcweir // INQUIRY 61cdf0e10cSrcweir const CHAR * Data() const; 62cdf0e10cSrcweir 63cdf0e10cSrcweir /** @returns the allocated number of CHAR. 64cdf0e10cSrcweir This may be different from the number of bytes. 65cdf0e10cSrcweir There is actually allocated one more CHAR, 66cdf0e10cSrcweir which is guaranteed to be CHAR(0) in all circumstances. 67cdf0e10cSrcweir */ 68cdf0e10cSrcweir size_type Size() const; 69cdf0e10cSrcweir 70cdf0e10cSrcweir private: 71cdf0e10cSrcweir /* Because this is used only within a refcounted structure, 72cdf0e10cSrcweir these functions are forbidden - at least yet. 73cdf0e10cSrcweir */ 74cdf0e10cSrcweir StringData(const self&); 75cdf0e10cSrcweir self & operator=(const self&); 76cdf0e10cSrcweir 77cdf0e10cSrcweir // DATA 78cdf0e10cSrcweir DYN CHAR * dpData; 79cdf0e10cSrcweir size_type nSize; /// The allocated size - 1 (for the finishing 0). 80cdf0e10cSrcweir }; 81cdf0e10cSrcweir 82cdf0e10cSrcweir 83cdf0e10cSrcweir 84cdf0e10cSrcweir // IMPLEMENTATION 85cdf0e10cSrcweir 86cdf0e10cSrcweir template <class CHAR> 87cdf0e10cSrcweir StringData<CHAR>::StringData() 88cdf0e10cSrcweir : dpData( new CHAR[1] ), 89cdf0e10cSrcweir nSize(0) 90cdf0e10cSrcweir { 91cdf0e10cSrcweir *dpData = CHAR(0); 92cdf0e10cSrcweir } 93cdf0e10cSrcweir 94cdf0e10cSrcweir template <class CHAR> 95cdf0e10cSrcweir StringData<CHAR>::StringData( const CHAR * i_pData, 96cdf0e10cSrcweir size_type i_nValidLength ) 97cdf0e10cSrcweir : dpData( new CHAR[i_nValidLength + 1] ), 98cdf0e10cSrcweir nSize(i_nValidLength) 99cdf0e10cSrcweir { 100cdf0e10cSrcweir memcpy( dpData, i_pData, i_nValidLength * sizeof(CHAR) ); 101cdf0e10cSrcweir dpData[nSize] = CHAR(0); 102cdf0e10cSrcweir } 103cdf0e10cSrcweir 104cdf0e10cSrcweir template <class CHAR> 105cdf0e10cSrcweir StringData<CHAR>::~StringData() 106cdf0e10cSrcweir { 107cdf0e10cSrcweir delete [] dpData; 108cdf0e10cSrcweir } 109cdf0e10cSrcweir 110cdf0e10cSrcweir template <class CHAR> 111cdf0e10cSrcweir const CHAR * 112cdf0e10cSrcweir StringData<CHAR>::Data() const 113cdf0e10cSrcweir { 114cdf0e10cSrcweir return dpData; 115cdf0e10cSrcweir } 116cdf0e10cSrcweir 117cdf0e10cSrcweir template <class CHAR> 118cdf0e10cSrcweir typename StringData<CHAR>::size_type 119cdf0e10cSrcweir StringData<CHAR>::Size() const 120cdf0e10cSrcweir { 121cdf0e10cSrcweir return nSize; 122cdf0e10cSrcweir } 123cdf0e10cSrcweir 124cdf0e10cSrcweir 125cdf0e10cSrcweir 126cdf0e10cSrcweir } // namespace csv 127cdf0e10cSrcweir 128cdf0e10cSrcweir 129cdf0e10cSrcweir #endif 130cdf0e10cSrcweir 131cdf0e10cSrcweir 132