xref: /trunk/main/soltools/testSHL/util/tutil.cxx (revision cdf0e10c)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_soltools.hxx"
30*cdf0e10cSrcweir #include "tutil.hxx"
31*cdf0e10cSrcweir 
32*cdf0e10cSrcweir // <namespace_tstutl>
33*cdf0e10cSrcweir namespace tstutl {
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir // getcwd hack is deprecated as soon as normalizePath works as intend
36*cdf0e10cSrcweir #ifdef WNT
37*cdf0e10cSrcweir #define _getcwd getcwd
38*cdf0e10cSrcweir #include <direct.h>                         // _getcwd
39*cdf0e10cSrcweir #else
40*cdf0e10cSrcweir #include <unistd.h>							// getcwd
41*cdf0e10cSrcweir #endif
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir // <function_cnvrtPth>
44*cdf0e10cSrcweir ::rtl::OUString cnvrtPth( ::rtl::OString sysPth ) {
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir     using ::osl::FileBase;
47*cdf0e10cSrcweir     using ::rtl::OUString;
48*cdf0e10cSrcweir     using ::rtl::OString;
49*cdf0e10cSrcweir 
50*cdf0e10cSrcweir     ::rtl::OUString ret;
51*cdf0e10cSrcweir 	sysPth = sysPth.replace( '\\','/' );
52*cdf0e10cSrcweir     OUString pth( OUString::createFromAscii( sysPth.getStr() ) );
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir 	if ( sysPth.indexOf("..") != -1 ) {
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir         // <hack> for osl_normalizePath() can't handle relatives
57*cdf0e10cSrcweir             char buffer[256];
58*cdf0e10cSrcweir             OString curPth(getcwd(buffer,256));
59*cdf0e10cSrcweir         // </hack>
60*cdf0e10cSrcweir         OUString nrmCurPth;
61*cdf0e10cSrcweir         FileBase::normalizePath( OUString::createFromAscii( curPth ) ,
62*cdf0e10cSrcweir                                                                 nrmCurPth );
63*cdf0e10cSrcweir         FileBase::getAbsolutePath( nrmCurPth, pth, ret );
64*cdf0e10cSrcweir 	}
65*cdf0e10cSrcweir 	else  {
66*cdf0e10cSrcweir         FileBase::normalizePath( pth, ret );
67*cdf0e10cSrcweir 	}
68*cdf0e10cSrcweir 	return ret;
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir } // </function_cnvrtPth>
71*cdf0e10cSrcweir 
72*cdf0e10cSrcweir // <function_getEntriesFromFile>
73*cdf0e10cSrcweir sal_uInt32 getEntriesFromFile( sal_Char* fName,
74*cdf0e10cSrcweir                                             vector< sal_Char* >& entries ) {
75*cdf0e10cSrcweir 
76*cdf0e10cSrcweir     ::osl::File inFile( cnvrtPth( fName ) );
77*cdf0e10cSrcweir     if ( inFile.open( OpenFlag_Read ) == ::osl::FileBase::E_None) {
78*cdf0e10cSrcweir         ::rtl::ByteSequence byteSeq;
79*cdf0e10cSrcweir         inFile.readLine( byteSeq );
80*cdf0e10cSrcweir         while ( byteSeq.getLength() ) {
81*cdf0e10cSrcweir             sal_uInt32 len = byteSeq.getLength();
82*cdf0e10cSrcweir             sal_uInt32 i;
83*cdf0e10cSrcweir             sal_Char* pEnt = new sal_Char[ len+1 ];
84*cdf0e10cSrcweir             sal_Char* bsPtr = (sal_Char*)byteSeq.getArray();
85*cdf0e10cSrcweir             for ( i=0; i<len; i++ ) {
86*cdf0e10cSrcweir                 pEnt[i] = bsPtr[i];
87*cdf0e10cSrcweir             }
88*cdf0e10cSrcweir             pEnt[len] = '\0';
89*cdf0e10cSrcweir             entries.push_back( pEnt );
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir             inFile.readLine( byteSeq );
92*cdf0e10cSrcweir         }
93*cdf0e10cSrcweir     }
94*cdf0e10cSrcweir     return ( entries.size() );
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir } // </function_getEntriesFromFile>
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir // <function_cpy>
99*cdf0e10cSrcweir sal_Char* cpy( sal_Char** dest, const sal_Char* src ) {
100*cdf0e10cSrcweir 
101*cdf0e10cSrcweir     *dest = new sal_Char[ ln(src)+1 ];
102*cdf0e10cSrcweir     // set pointer
103*cdf0e10cSrcweir     sal_Char* pdest = *dest;
104*cdf0e10cSrcweir     const sal_Char* psrc = src;
105*cdf0e10cSrcweir 
106*cdf0e10cSrcweir     // copy string by char
107*cdf0e10cSrcweir     while( *pdest++ = *psrc++ );
108*cdf0e10cSrcweir 
109*cdf0e10cSrcweir     return ( *dest );
110*cdf0e10cSrcweir 
111*cdf0e10cSrcweir } // </function_cpy>
112*cdf0e10cSrcweir 
113*cdf0e10cSrcweir // <function_cat>
114*cdf0e10cSrcweir sal_Char* cat( const sal_Char* str1, const sal_Char* str2 ) {
115*cdf0e10cSrcweir 
116*cdf0e10cSrcweir     // allocate memory for destination string
117*cdf0e10cSrcweir     sal_Char* dest = new sal_Char[ ln(str1)+ln(str2)+1 ];
118*cdf0e10cSrcweir 
119*cdf0e10cSrcweir     // set pointers
120*cdf0e10cSrcweir     sal_Char* pdest = dest;
121*cdf0e10cSrcweir     const sal_Char* psrc = str1;
122*cdf0e10cSrcweir 
123*cdf0e10cSrcweir     // copy string1 by char to dest
124*cdf0e10cSrcweir     while( *pdest++ = *psrc++ );
125*cdf0e10cSrcweir     pdest--;
126*cdf0e10cSrcweir     psrc = str2;
127*cdf0e10cSrcweir     while( *pdest++ = *psrc++ );
128*cdf0e10cSrcweir 
129*cdf0e10cSrcweir     return ( dest );
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir } // </function_cat>
132*cdf0e10cSrcweir 
133*cdf0e10cSrcweir // <function_ln>
134*cdf0e10cSrcweir sal_uInt32 ln( const sal_Char* str ) {
135*cdf0e10cSrcweir 
136*cdf0e10cSrcweir     sal_uInt32 len = 0;
137*cdf0e10cSrcweir     const sal_Char* ptr = str;
138*cdf0e10cSrcweir 
139*cdf0e10cSrcweir     if( ptr ) {
140*cdf0e10cSrcweir         while( *ptr++ ) len++;
141*cdf0e10cSrcweir     }
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir     return(len);
144*cdf0e10cSrcweir } // <function_ln>
145*cdf0e10cSrcweir 
146*cdf0e10cSrcweir } // </namespace_tstutl>
147*cdf0e10cSrcweir 
148