LibreOffice Module codemaker (master) 1
global.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20#include <osl/process.h>
21#include <rtl/strbuf.hxx>
22#include <rtl/ustring.hxx>
23#include <osl/thread.h>
24#include <osl/file.hxx>
25#include <o3tl/string_view.hxx>
26
27#include <string.h>
28#include <string_view>
29#include <errno.h>
30
31#if defined(_WIN32)
32# include <io.h>
33# include <direct.h>
34#endif
35
36#ifdef UNX
37# include <sys/stat.h>
38# include <unistd.h>
39#endif
40
41#include <codemaker/global.hxx>
42
43#ifdef SAL_UNX
44#define SEPARATOR '/'
45#else
46#define SEPARATOR '\\'
47#endif
48
49using namespace ::osl;
50
51
52OString getTempDir(const OString& sFileName)
53{
54 sal_Int32 index = 0;
55#ifdef SAL_UNX
56 if ((index=sFileName.lastIndexOf('/')) > 0)
57 return sFileName.copy(0, index);
58#else
59 if ((index=sFileName.lastIndexOf('\\')) > 0)
60 return sFileName.copy(0, index);
61#endif
62 return ".";
63}
64
65OString createFileNameFromType( const OString& destination,
66 const OString& typeName,
67 const OString& postfix )
68{
69 OString type(typeName.replace('.', '/'));
70
71 sal_Int32 length = destination.getLength();
72
73 bool bWithPoint = false;
74 if (length == 0)
75 {
76 length++;
77 bWithPoint = true;
78 }
79
80 length += type.getLength() + postfix.getLength();
81
82 bool bWithSeparator = false;
83 if (!(destination.endsWith("\\") || destination.endsWith("/"))
84 && !(type.startsWith("\\") || type.startsWith("/")))
85 {
86 length++;
87 bWithSeparator = true;
88 }
89
90 OStringBuffer fileNameBuf(length);
91
92 if (bWithPoint)
93 fileNameBuf.append('.');
94 else
95 fileNameBuf.append(destination);
96
97 if (bWithSeparator)
98 fileNameBuf.append("/");
99
100 fileNameBuf.append(type + postfix);
101
102 OString fileName(fileNameBuf.makeStringAndClear());
103
104 char token;
105#ifdef SAL_UNX
106 fileName = fileName.replace('\\', '/');
107 token = '/';
108#else
109 fileName = fileName.replace('/', '\\');
110 token = '\\';
111#endif
112
113 OStringBuffer buffer(length);
114
115 sal_Int32 nIndex = 0;
116 do
117 {
118 buffer.append(o3tl::getToken(fileName, 0, token, nIndex));
119 if( nIndex == -1 )
120 break;
121
122 if (buffer.isEmpty() || std::string_view(".") == buffer)
123 {
124 buffer.append(token);
125 continue;
126 }
127
128#if defined(SAL_UNX)
129 if (mkdir(buffer.getStr(), 0777) == -1)
130#else
131 if (mkdir(buffer.getStr()) == -1)
132#endif
133 {
134 if ( errno == ENOENT )
135 return OString();
136 }
137
138 buffer.append(token);
139 } while(true);
140
141 OUString uSysFileName;
142 OSL_VERIFY( FileBase::getSystemPathFromFileURL(
143 convertToFileUrl(fileName), uSysFileName) == FileBase::E_None );
144 return OUStringToOString(uSysFileName, osl_getThreadTextEncoding());
145}
146
147bool fileExists(const OString& fileName)
148{
149 FILE *f= fopen(fileName.getStr(), "r");
150
151 if (f != nullptr)
152 {
153 fclose(f);
154 return true;
155 }
156
157 return false;
158}
159
160static bool checkFileContent(const OString& targetFileName, const OString& tmpFileName)
161{
162 FILE *target = fopen(targetFileName.getStr(), "r");
163 FILE *tmp = fopen(tmpFileName.getStr(), "r");
164 bool bFindChanges = false;
165
166 if (target != nullptr && tmp != nullptr)
167 {
168 char buffer1[1024+1];
169 char buffer2[1024+1];
170 sal_Int32 n1 = 0;
171 sal_Int32 n2 = 0;
172
173 while ( !bFindChanges && !feof(target) && !feof(tmp))
174 {
175 n1 = fread(buffer1, sizeof(char), 1024, target);
176 n2 = fread(buffer2, sizeof(char), 1024, tmp);
177
178 if ( n1 != n2 )
179 bFindChanges = true;
180 else
181 if ( memcmp(buffer1, buffer2, n2) != 0 )
182 bFindChanges = true;
183 }
184 }
185
186 if (target) fclose(target);
187 if (tmp) fclose(tmp);
188
189 return bFindChanges;
190}
191
192bool makeValidTypeFile(const OString& targetFileName, const OString& tmpFileName,
193 bool bFileCheck)
194{
195 if (bFileCheck) {
196 if (checkFileContent(targetFileName, tmpFileName)) {
197 if ( !unlink(targetFileName.getStr()) )
198 if ( !rename(tmpFileName.getStr(), targetFileName.getStr()) )
199 return true;
200 } else
201 return removeTypeFile(tmpFileName);
202 } else {
203 if (fileExists(targetFileName))
204 if (!removeTypeFile(targetFileName))
205 return false;
206
207 if ( rename(tmpFileName.getStr(), targetFileName.getStr()) ) {
208 if (errno == EEXIST)
209 return true;
210 } else
211 return true;
212 }
213 return false;
214}
215
216bool removeTypeFile(const OString& fileName)
217{
218 return unlink(fileName.getStr()) == 0;
219}
220
221OUString convertToFileUrl(const OString& fileName)
222{
223 if ( fileName.startsWith("file://") )
224 {
225 return OStringToOUString(fileName, osl_getThreadTextEncoding());
226 }
227
228 OUString uUrlFileName;
229 OUString uFileName(fileName.getStr(), fileName.getLength(), osl_getThreadTextEncoding());
230 if ( fileName.startsWith(".") || fileName.indexOf(SEPARATOR) < 0 )
231 {
232 OUString uWorkingDir;
233 if (osl_getProcessWorkingDir(&uWorkingDir.pData) != osl_Process_E_None)
234 {
235 OSL_ASSERT(false);
236 }
237 if (FileBase::getAbsoluteFileURL(uWorkingDir, uFileName, uUrlFileName)
238 != FileBase::E_None)
239 {
240 OSL_ASSERT(false);
241 }
242 } else
243 {
244 if (FileBase::getFileURLFromSystemPath(uFileName, uUrlFileName)
245 != FileBase::E_None)
246 {
247 OSL_ASSERT(false);
248 }
249 }
250
251 return uUrlFileName;
252}
253
254
255// FileStream
256
258 : m_file(nullptr)
259{
260}
261
263{
264 if ( isValid() )
265 osl_closeFile(m_file);
266}
267
268bool FileStream::isValid() const
269{
270 return m_file != nullptr;
271}
272
273void FileStream::createTempFile(const OString& sPath)
274{
275 OString sTmp(".");
276 OUString sTmpPath;
277 OUString sTmpName;
278
279 if (!sPath.isEmpty())
280 sTmp = sPath;
281
282 sTmpPath = convertToFileUrl(sTmp);
283
284 if (osl_createTempFile(sTmpPath.pData, &m_file, &sTmpName.pData) == osl_File_E_None) {
285#ifdef SAL_UNX
286 sal_uInt64 const uAttr = osl_File_Attribute_OwnWrite |
287 osl_File_Attribute_OwnRead |
288 osl_File_Attribute_GrpWrite |
289 osl_File_Attribute_GrpRead |
290 osl_File_Attribute_OthRead;
291 if (osl_setFileAttributes(sTmpName.pData, uAttr) != osl_File_E_None) {
292 m_file = nullptr;
293 return;
294 }
295#endif
296 OUString sSysTmpName;
297 FileBase::getSystemPathFromFileURL(sTmpName, sSysTmpName);
298 m_name = OUStringToOString(sSysTmpName, osl_getThreadTextEncoding());
299 } else
300 m_file = nullptr;
301}
302
303void FileStream::close()
304{
305 if ( isValid() )
306 {
307 osl_closeFile(m_file);
308 m_file = nullptr;
309 m_name.clear();
310 }
311}
312
313bool FileStream::write(void const * buffer, sal_uInt64 size) {
314 while (size > 0) {
315 sal_uInt64 written;
316 if (osl_writeFile(m_file, buffer, size, &written) != osl_File_E_None) {
317 return false;
318 }
319 OSL_ASSERT(written <= size);
320 size -= written;
321 buffer = static_cast< char const * >(buffer) + written;
322 }
323 return true;
324}
325
326FileStream &operator<<(FileStream& o, sal_uInt32 i) {
327 sal_uInt64 writtenBytes;
328 OString s = OString::number(static_cast<sal_Int32>(i));
329 osl_writeFile(o.m_file, s.getStr(), s.getLength() * sizeof(char), &writtenBytes);
330 return o;
331}
332FileStream &operator<<(FileStream& o, char const * s) {
333 sal_uInt64 writtenBytes;
334 osl_writeFile(o.m_file, s, strlen(s), &writtenBytes);
335 return o;
336}
337FileStream &operator<<(FileStream& o, OString const * s) {
338 sal_uInt64 writtenBytes;
339 osl_writeFile(o.m_file, s->getStr(), s->getLength() * sizeof(char), &writtenBytes);
340 return o;
341}
342FileStream &operator<<(FileStream& o, const OString& s) {
343 sal_uInt64 writtenBytes;
344 osl_writeFile(o.m_file, s.getStr(), s.getLength() * sizeof(char), &writtenBytes);
345 return o;
346
347}
348FileStream &operator<<(FileStream& o, OStringBuffer const * s) {
349 sal_uInt64 writtenBytes;
350 osl_writeFile(o.m_file, s->getStr(), s->getLength() * sizeof(char), &writtenBytes);
351 return o;
352}
353FileStream &operator<<(FileStream& o, const OStringBuffer& s) {
354 sal_uInt64 writtenBytes;
355 osl_writeFile(
356 o.m_file, s.getStr(), s.getLength() * sizeof(char), &writtenBytes);
357 return o;
358}
359
360FileStream & operator <<(FileStream & out, std::u16string_view s) {
361 return out << OUStringToOString(s, RTL_TEXTENCODING_UTF8);
362}
363
365
366/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
~CannotDumpException() noexcept
Definition: global.cxx:364
~FileStream() override
Definition: global.cxx:262
FileStream(const Filepath_char_t *filename)
FileStream & operator<<(FileStream &o, sal_uInt32 i)
Definition: global.cxx:326
static bool checkFileContent(const OString &targetFileName, const OString &tmpFileName)
Definition: global.cxx:160
OString getTempDir(const OString &sFileName)
Definition: global.cxx:52
bool removeTypeFile(const OString &fileName)
Definition: global.cxx:216
OString createFileNameFromType(const OString &destination, const OString &typeName, const OString &postfix)
Definition: global.cxx:65
OUString convertToFileUrl(const OString &fileName)
Definition: global.cxx:221
bool fileExists(const OString &fileName)
Definition: global.cxx:147
bool makeValidTypeFile(const OString &targetFileName, const OString &tmpFileName, bool bFileCheck)
Definition: global.cxx:192
#define SEPARATOR
Definition: global.cxx:46
sal_Int32 nIndex
int n2
int n1
size
int i
index
std::basic_string_view< charT, traits > getToken(std::basic_string_view< charT, traits > sv, charT delimiter, std::size_t &position)
OString OUStringToOString(std::u16string_view str, ConnectionSettings const *settings)
OUString typeName
ResultType type