xref: /trunk/main/soltools/testSHL/util/tutil.cxx (revision 7a4715d3)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_soltools.hxx"
26 #include "tutil.hxx"
27 
28 // <namespace_tstutl>
29 namespace tstutl {
30 
31 // getcwd hack is deprecated as soon as normalizePath works as intend
32 #ifdef WNT
33 #define _getcwd getcwd
34 #include <direct.h>                         // _getcwd
35 #else
36 #include <unistd.h>							// getcwd
37 #endif
38 
39 // <function_cnvrtPth>
cnvrtPth(::rtl::OString sysPth)40 ::rtl::OUString cnvrtPth( ::rtl::OString sysPth ) {
41 
42     using ::osl::FileBase;
43     using ::rtl::OUString;
44     using ::rtl::OString;
45 
46     ::rtl::OUString ret;
47 	sysPth = sysPth.replace( '\\','/' );
48     OUString pth( OUString::createFromAscii( sysPth.getStr() ) );
49 
50 	if ( sysPth.indexOf("..") != -1 ) {
51 
52         // <hack> for osl_normalizePath() can't handle relatives
53             char buffer[256];
54             OString curPth(getcwd(buffer,256));
55         // </hack>
56         OUString nrmCurPth;
57         FileBase::normalizePath( OUString::createFromAscii( curPth ) ,
58                                                                 nrmCurPth );
59         FileBase::getAbsolutePath( nrmCurPth, pth, ret );
60 	}
61 	else  {
62         FileBase::normalizePath( pth, ret );
63 	}
64 	return ret;
65 
66 } // </function_cnvrtPth>
67 
68 // <function_getEntriesFromFile>
getEntriesFromFile(sal_Char * fName,vector<sal_Char * > & entries)69 sal_uInt32 getEntriesFromFile( sal_Char* fName,
70                                             vector< sal_Char* >& entries ) {
71 
72     ::osl::File inFile( cnvrtPth( fName ) );
73     if ( inFile.open( OpenFlag_Read ) == ::osl::FileBase::E_None) {
74         ::rtl::ByteSequence byteSeq;
75         inFile.readLine( byteSeq );
76         while ( byteSeq.getLength() ) {
77             sal_uInt32 len = byteSeq.getLength();
78             sal_uInt32 i;
79             sal_Char* pEnt = new sal_Char[ len+1 ];
80             sal_Char* bsPtr = (sal_Char*)byteSeq.getArray();
81             for ( i=0; i<len; i++ ) {
82                 pEnt[i] = bsPtr[i];
83             }
84             pEnt[len] = '\0';
85             entries.push_back( pEnt );
86 
87             inFile.readLine( byteSeq );
88         }
89     }
90     return ( entries.size() );
91 
92 } // </function_getEntriesFromFile>
93 
94 // <function_cpy>
cpy(sal_Char ** dest,const sal_Char * src)95 sal_Char* cpy( sal_Char** dest, const sal_Char* src ) {
96 
97     *dest = new sal_Char[ ln(src)+1 ];
98     // set pointer
99     sal_Char* pdest = *dest;
100     const sal_Char* psrc = src;
101 
102     // copy string by char
103     while( *pdest++ = *psrc++ );
104 
105     return ( *dest );
106 
107 } // </function_cpy>
108 
109 // <function_cat>
cat(const sal_Char * str1,const sal_Char * str2)110 sal_Char* cat( const sal_Char* str1, const sal_Char* str2 ) {
111 
112     // allocate memory for destination string
113     sal_Char* dest = new sal_Char[ ln(str1)+ln(str2)+1 ];
114 
115     // set pointers
116     sal_Char* pdest = dest;
117     const sal_Char* psrc = str1;
118 
119     // copy string1 by char to dest
120     while( *pdest++ = *psrc++ );
121     pdest--;
122     psrc = str2;
123     while( *pdest++ = *psrc++ );
124 
125     return ( dest );
126 
127 } // </function_cat>
128 
129 // <function_ln>
ln(const sal_Char * str)130 sal_uInt32 ln( const sal_Char* str ) {
131 
132     sal_uInt32 len = 0;
133     const sal_Char* ptr = str;
134 
135     if( ptr ) {
136         while( *ptr++ ) len++;
137     }
138 
139     return(len);
140 } // <function_ln>
141 
142 } // </namespace_tstutl>
143 
144