xref: /aoo41x/main/soltools/testSHL/util/tutil.cxx (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 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_soltools.hxx"
30 #include "tutil.hxx"
31 
32 // <namespace_tstutl>
33 namespace tstutl {
34 
35 // getcwd hack is deprecated as soon as normalizePath works as intend
36 #ifdef WNT
37 #define _getcwd getcwd
38 #include <direct.h>                         // _getcwd
39 #else
40 #include <unistd.h>							// getcwd
41 #endif
42 
43 // <function_cnvrtPth>
44 ::rtl::OUString cnvrtPth( ::rtl::OString sysPth ) {
45 
46     using ::osl::FileBase;
47     using ::rtl::OUString;
48     using ::rtl::OString;
49 
50     ::rtl::OUString ret;
51 	sysPth = sysPth.replace( '\\','/' );
52     OUString pth( OUString::createFromAscii( sysPth.getStr() ) );
53 
54 	if ( sysPth.indexOf("..") != -1 ) {
55 
56         // <hack> for osl_normalizePath() can't handle relatives
57             char buffer[256];
58             OString curPth(getcwd(buffer,256));
59         // </hack>
60         OUString nrmCurPth;
61         FileBase::normalizePath( OUString::createFromAscii( curPth ) ,
62                                                                 nrmCurPth );
63         FileBase::getAbsolutePath( nrmCurPth, pth, ret );
64 	}
65 	else  {
66         FileBase::normalizePath( pth, ret );
67 	}
68 	return ret;
69 
70 } // </function_cnvrtPth>
71 
72 // <function_getEntriesFromFile>
73 sal_uInt32 getEntriesFromFile( sal_Char* fName,
74                                             vector< sal_Char* >& entries ) {
75 
76     ::osl::File inFile( cnvrtPth( fName ) );
77     if ( inFile.open( OpenFlag_Read ) == ::osl::FileBase::E_None) {
78         ::rtl::ByteSequence byteSeq;
79         inFile.readLine( byteSeq );
80         while ( byteSeq.getLength() ) {
81             sal_uInt32 len = byteSeq.getLength();
82             sal_uInt32 i;
83             sal_Char* pEnt = new sal_Char[ len+1 ];
84             sal_Char* bsPtr = (sal_Char*)byteSeq.getArray();
85             for ( i=0; i<len; i++ ) {
86                 pEnt[i] = bsPtr[i];
87             }
88             pEnt[len] = '\0';
89             entries.push_back( pEnt );
90 
91             inFile.readLine( byteSeq );
92         }
93     }
94     return ( entries.size() );
95 
96 } // </function_getEntriesFromFile>
97 
98 // <function_cpy>
99 sal_Char* cpy( sal_Char** dest, const sal_Char* src ) {
100 
101     *dest = new sal_Char[ ln(src)+1 ];
102     // set pointer
103     sal_Char* pdest = *dest;
104     const sal_Char* psrc = src;
105 
106     // copy string by char
107     while( *pdest++ = *psrc++ );
108 
109     return ( *dest );
110 
111 } // </function_cpy>
112 
113 // <function_cat>
114 sal_Char* cat( const sal_Char* str1, const sal_Char* str2 ) {
115 
116     // allocate memory for destination string
117     sal_Char* dest = new sal_Char[ ln(str1)+ln(str2)+1 ];
118 
119     // set pointers
120     sal_Char* pdest = dest;
121     const sal_Char* psrc = str1;
122 
123     // copy string1 by char to dest
124     while( *pdest++ = *psrc++ );
125     pdest--;
126     psrc = str2;
127     while( *pdest++ = *psrc++ );
128 
129     return ( dest );
130 
131 } // </function_cat>
132 
133 // <function_ln>
134 sal_uInt32 ln( const sal_Char* str ) {
135 
136     sal_uInt32 len = 0;
137     const sal_Char* ptr = str;
138 
139     if( ptr ) {
140         while( *ptr++ ) len++;
141     }
142 
143     return(len);
144 } // <function_ln>
145 
146 } // </namespace_tstutl>
147 
148