merge the formfield patch from ooo-build
[ooovba.git] / rdbmaker / source / codemaker / global.cxx
blob138fb623d77492a30b353d77d67b82b2d3ee6cbe
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: global.cxx,v $
10 * $Revision: 1.14.8.1 $
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 ************************************************************************/
30 #include <osl/process.h>
31 #ifndef _RTL_OSTRINGBUFFER_HXX_
32 #include <rtl/strbuf.hxx>
33 #endif
34 #include <rtl/ustring.hxx>
35 #include <osl/thread.h>
36 #include <osl/file.hxx>
38 #include <stdlib.h>
39 #include <stdio.h>
40 #if defined(SAL_W32) || defined(SAL_OS2)
41 #include <io.h>
42 #include <direct.h>
43 #include <errno.h>
44 #endif
46 #ifdef UNX
47 #include <sys/stat.h>
48 #include <errno.h>
49 #include <unistd.h>
50 #endif
51 #include <codemaker/global.hxx>
53 #ifdef SAL_UNX
54 #define SEPARATOR '/'
55 #else
56 #define SEPARATOR '\\'
57 #endif
59 using namespace ::rtl;
60 using namespace ::osl;
62 const OString inGlobalSet(const OUString & rValue)
64 OString sValue( OUStringToOString(rValue, RTL_TEXTENCODING_UTF8) );
65 static StringSet aGlobalMap;
66 StringSet::iterator iter = aGlobalMap.find( sValue );
67 if( iter != aGlobalMap.end() )
68 return *iter;
69 return *(aGlobalMap.insert( sValue ).first);
72 static sal_Bool isFileUrl(const OString& fileName)
74 if (fileName.indexOf("file://") == 0 )
75 return sal_True;
76 return sal_False;
79 OUString convertToFileUrl(const OString& fileName)
81 if ( isFileUrl(fileName) )
83 return OStringToOUString(fileName, osl_getThreadTextEncoding());
86 OUString uUrlFileName;
87 OUString uFileName(fileName.getStr(), fileName.getLength(), osl_getThreadTextEncoding());
88 if ( fileName.indexOf('.') == 0 || fileName.indexOf(SEPARATOR) < 0 )
90 OUString uWorkingDir;
91 if (osl_getProcessWorkingDir(&uWorkingDir.pData) != osl_Process_E_None) {
92 OSL_ASSERT(false);
94 if (FileBase::getAbsoluteFileURL(uWorkingDir, uFileName, uUrlFileName)
95 != FileBase::E_None)
97 OSL_ASSERT(false);
99 } else
101 if (FileBase::getFileURLFromSystemPath(uFileName, uUrlFileName)
102 != FileBase::E_None)
104 OSL_ASSERT(false);
108 return uUrlFileName;
111 //*************************************************************************
112 // FileStream
113 //*************************************************************************
114 FileStream::FileStream()
118 FileStream::~FileStream()
120 if ( isValid() )
122 fflush(m_pFile);
123 fclose(m_pFile);
127 sal_Bool FileStream::isValid()
129 if ( m_pFile )
130 return sal_True;
132 return sal_False;
135 void FileStream::open(const OString& name, FileAccessMode mode)
137 if ( name.getLength() > 0 )
139 m_name = name;
140 m_pFile = fopen(m_name, checkAccessMode(mode));
144 void FileStream::close()
146 if ( isValid() )
148 fflush(m_pFile);
149 fclose(m_pFile);
150 m_pFile = NULL;
151 m_name = OString();
155 const sal_Char* FileStream::checkAccessMode(FileAccessMode mode)
157 switch( mode )
159 case FAM_READ:
160 return "r";
161 case FAM_WRITE:
162 return "w";
163 case FAM_APPEND:
164 return "a";
165 case FAM_READWRITE_EXIST:
166 return "r+";
167 case FAM_READWRITE:
168 return "w+";
169 case FAM_READAPPEND:
170 return "a+";
172 return "w+";