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