19f73e17bSAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
39f73e17bSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
49f73e17bSAndrew Rist * or more contributor license agreements. See the NOTICE file
59f73e17bSAndrew Rist * distributed with this work for additional information
69f73e17bSAndrew Rist * regarding copyright ownership. The ASF licenses this file
79f73e17bSAndrew Rist * to you under the Apache License, Version 2.0 (the
89f73e17bSAndrew Rist * "License"); you may not use this file except in compliance
99f73e17bSAndrew Rist * with the License. You may obtain a copy of the License at
109f73e17bSAndrew Rist *
119f73e17bSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
129f73e17bSAndrew Rist *
139f73e17bSAndrew Rist * Unless required by applicable law or agreed to in writing,
149f73e17bSAndrew Rist * software distributed under the License is distributed on an
159f73e17bSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
169f73e17bSAndrew Rist * KIND, either express or implied. See the License for the
179f73e17bSAndrew Rist * specific language governing permissions and limitations
189f73e17bSAndrew Rist * under the License.
199f73e17bSAndrew Rist *
209f73e17bSAndrew Rist *************************************************************/
219f73e17bSAndrew Rist
229f73e17bSAndrew Rist
23cdf0e10cSrcweir #include <osl/process.h>
24cdf0e10cSrcweir #ifndef _RTL_OSTRINGBUFFER_HXX_
25cdf0e10cSrcweir #include <rtl/strbuf.hxx>
26cdf0e10cSrcweir #endif
27cdf0e10cSrcweir #include <rtl/ustring.hxx>
28cdf0e10cSrcweir #include <osl/thread.h>
29cdf0e10cSrcweir #include <osl/file.hxx>
30cdf0e10cSrcweir
31cdf0e10cSrcweir #include <stdlib.h>
32cdf0e10cSrcweir #include <stdio.h>
33cdf0e10cSrcweir #if defined(SAL_W32) || defined(SAL_OS2)
34cdf0e10cSrcweir #include <io.h>
35cdf0e10cSrcweir #include <direct.h>
36cdf0e10cSrcweir #include <errno.h>
37cdf0e10cSrcweir #endif
38cdf0e10cSrcweir
39cdf0e10cSrcweir #ifdef UNX
40cdf0e10cSrcweir #include <sys/stat.h>
41cdf0e10cSrcweir #include <errno.h>
42cdf0e10cSrcweir #include <unistd.h>
43cdf0e10cSrcweir #endif
44cdf0e10cSrcweir #include <codemaker/global.hxx>
45cdf0e10cSrcweir
46cdf0e10cSrcweir #ifdef SAL_UNX
47cdf0e10cSrcweir #define SEPARATOR '/'
48cdf0e10cSrcweir #else
49cdf0e10cSrcweir #define SEPARATOR '\\'
50cdf0e10cSrcweir #endif
51cdf0e10cSrcweir
52cdf0e10cSrcweir using namespace ::rtl;
53cdf0e10cSrcweir using namespace ::osl;
54cdf0e10cSrcweir
inGlobalSet(const OUString & rValue)55cdf0e10cSrcweir const OString inGlobalSet(const OUString & rValue)
56cdf0e10cSrcweir {
57cdf0e10cSrcweir OString sValue( OUStringToOString(rValue, RTL_TEXTENCODING_UTF8) );
58cdf0e10cSrcweir static StringSet aGlobalMap;
59cdf0e10cSrcweir StringSet::iterator iter = aGlobalMap.find( sValue );
60cdf0e10cSrcweir if( iter != aGlobalMap.end() )
61cdf0e10cSrcweir return *iter;
62cdf0e10cSrcweir return *(aGlobalMap.insert( sValue ).first);
63cdf0e10cSrcweir }
64cdf0e10cSrcweir
isFileUrl(const OString & fileName)65cdf0e10cSrcweir static sal_Bool isFileUrl(const OString& fileName)
66cdf0e10cSrcweir {
67cdf0e10cSrcweir if (fileName.indexOf("file://") == 0 )
68cdf0e10cSrcweir return sal_True;
69cdf0e10cSrcweir return sal_False;
70cdf0e10cSrcweir }
71cdf0e10cSrcweir
convertToFileUrl(const OString & fileName)72cdf0e10cSrcweir OUString convertToFileUrl(const OString& fileName)
73cdf0e10cSrcweir {
74cdf0e10cSrcweir if ( isFileUrl(fileName) )
75cdf0e10cSrcweir {
76cdf0e10cSrcweir return OStringToOUString(fileName, osl_getThreadTextEncoding());
77cdf0e10cSrcweir }
78cdf0e10cSrcweir
79cdf0e10cSrcweir OUString uUrlFileName;
80cdf0e10cSrcweir OUString uFileName(fileName.getStr(), fileName.getLength(), osl_getThreadTextEncoding());
81cdf0e10cSrcweir if ( fileName.indexOf('.') == 0 || fileName.indexOf(SEPARATOR) < 0 )
82cdf0e10cSrcweir {
83cdf0e10cSrcweir OUString uWorkingDir;
84cdf0e10cSrcweir if (osl_getProcessWorkingDir(&uWorkingDir.pData) != osl_Process_E_None) {
85cdf0e10cSrcweir OSL_ASSERT(false);
86cdf0e10cSrcweir }
87cdf0e10cSrcweir if (FileBase::getAbsoluteFileURL(uWorkingDir, uFileName, uUrlFileName)
88cdf0e10cSrcweir != FileBase::E_None)
89cdf0e10cSrcweir {
90cdf0e10cSrcweir OSL_ASSERT(false);
91cdf0e10cSrcweir }
92cdf0e10cSrcweir } else
93cdf0e10cSrcweir {
94cdf0e10cSrcweir if (FileBase::getFileURLFromSystemPath(uFileName, uUrlFileName)
95cdf0e10cSrcweir != FileBase::E_None)
96cdf0e10cSrcweir {
97cdf0e10cSrcweir OSL_ASSERT(false);
98cdf0e10cSrcweir }
99cdf0e10cSrcweir }
100cdf0e10cSrcweir
101cdf0e10cSrcweir return uUrlFileName;
102cdf0e10cSrcweir }
103cdf0e10cSrcweir
104cdf0e10cSrcweir //*************************************************************************
105cdf0e10cSrcweir // FileStream
106cdf0e10cSrcweir //*************************************************************************
FileStream()107cdf0e10cSrcweir FileStream::FileStream()
108cdf0e10cSrcweir {
109cdf0e10cSrcweir }
110cdf0e10cSrcweir
~FileStream()111cdf0e10cSrcweir FileStream::~FileStream()
112cdf0e10cSrcweir {
113cdf0e10cSrcweir if ( isValid() )
114cdf0e10cSrcweir {
115cdf0e10cSrcweir fflush(m_pFile);
116cdf0e10cSrcweir fclose(m_pFile);
117cdf0e10cSrcweir }
118cdf0e10cSrcweir }
119cdf0e10cSrcweir
isValid()120cdf0e10cSrcweir sal_Bool FileStream::isValid()
121cdf0e10cSrcweir {
122cdf0e10cSrcweir if ( m_pFile )
123cdf0e10cSrcweir return sal_True;
124cdf0e10cSrcweir
125cdf0e10cSrcweir return sal_False;
126cdf0e10cSrcweir }
127cdf0e10cSrcweir
open(const OString & name,FileAccessMode mode)128cdf0e10cSrcweir void FileStream::open(const OString& name, FileAccessMode mode)
129cdf0e10cSrcweir {
130cdf0e10cSrcweir if ( name.getLength() > 0 )
131cdf0e10cSrcweir {
132cdf0e10cSrcweir m_name = name;
133*24c56ab9SHerbert Dürr m_pFile = fopen( m_name.getStr(), checkAccessMode(mode));
134cdf0e10cSrcweir }
135cdf0e10cSrcweir }
136cdf0e10cSrcweir
close()137cdf0e10cSrcweir void FileStream::close()
138cdf0e10cSrcweir {
139cdf0e10cSrcweir if ( isValid() )
140cdf0e10cSrcweir {
141cdf0e10cSrcweir fflush(m_pFile);
142cdf0e10cSrcweir fclose(m_pFile);
143cdf0e10cSrcweir m_pFile = NULL;
144cdf0e10cSrcweir m_name = OString();
145cdf0e10cSrcweir }
146cdf0e10cSrcweir }
147cdf0e10cSrcweir
checkAccessMode(FileAccessMode mode)148cdf0e10cSrcweir const sal_Char* FileStream::checkAccessMode(FileAccessMode mode)
149cdf0e10cSrcweir {
150cdf0e10cSrcweir switch( mode )
151cdf0e10cSrcweir {
152cdf0e10cSrcweir case FAM_READ:
153cdf0e10cSrcweir return "r";
154cdf0e10cSrcweir case FAM_WRITE:
155cdf0e10cSrcweir return "w";
156cdf0e10cSrcweir case FAM_APPEND:
157cdf0e10cSrcweir return "a";
158cdf0e10cSrcweir case FAM_READWRITE_EXIST:
159cdf0e10cSrcweir return "r+";
160cdf0e10cSrcweir case FAM_READWRITE:
161cdf0e10cSrcweir return "w+";
162cdf0e10cSrcweir case FAM_READAPPEND:
163cdf0e10cSrcweir return "a+";
164cdf0e10cSrcweir }
165cdf0e10cSrcweir return "w+";
166cdf0e10cSrcweir }
167cdf0e10cSrcweir
168