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