Files
libreoffice/rdbmaker/source/codemaker/global.cxx

172 lines
4.2 KiB
C++
Raw Normal View History

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2000-09-18 14:29:57 +00:00
/*************************************************************************
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2000-09-18 14:29:57 +00:00
*
* Copyright 2000, 2010 Oracle and/or its affiliates.
2000-09-18 14:29:57 +00:00
*
* OpenOffice.org - a multi-platform office productivity suite
2000-09-18 14:29:57 +00:00
*
* This file is part of OpenOffice.org.
2000-09-18 14:29:57 +00:00
*
* OpenOffice.org is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3
* only, as published by the Free Software Foundation.
2000-09-18 14:29:57 +00:00
*
* OpenOffice.org is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License version 3 for more details
* (a copy is included in the LICENSE file that accompanied this code).
2000-09-18 14:29:57 +00:00
*
* You should have received a copy of the GNU Lesser General Public License
* version 3 along with OpenOffice.org. If not, see
* <http://www.openoffice.org/license.html>
* for a copy of the LGPLv3 License.
2000-09-18 14:29:57 +00:00
*
************************************************************************/
2001-03-13 11:45:16 +00:00
#include <osl/process.h>
#include <rtl/strbuf.hxx>
#include <rtl/ustring.hxx>
2001-08-17 12:09:50 +00:00
#include <osl/thread.h>
#include <osl/file.hxx>
2000-09-18 14:29:57 +00:00
#include <stdlib.h>
#include <stdio.h>
2011-04-16 09:08:29 +02:00
#if defined(SAL_W32)
2000-09-18 14:29:57 +00:00
#include <io.h>
#include <direct.h>
#include <errno.h>
#endif
#ifdef UNX
#include <sys/stat.h>
#include <errno.h>
#include <unistd.h>
#endif
#include <codemaker/global.hxx>
2001-08-17 12:09:50 +00:00
#ifdef SAL_UNX
#define SEPARATOR '/'
#else
#define SEPARATOR '\\'
#endif
using namespace ::rtl;
using namespace ::osl;
2000-09-18 14:29:57 +00:00
const OString inGlobalSet(const OUString & rValue)
{
OString sValue( OUStringToOString(rValue, RTL_TEXTENCODING_UTF8) );
static StringSet aGlobalMap;
StringSet::iterator iter = aGlobalMap.find( sValue );
if( iter != aGlobalMap.end() )
return *iter;
return *(aGlobalMap.insert( sValue ).first);
}
2001-08-17 12:09:50 +00:00
static sal_Bool isFileUrl(const OString& fileName)
{
if (fileName.indexOf("file://") == 0 )
return sal_True;
return sal_False;
}
OUString convertToFileUrl(const OString& fileName)
{
if ( isFileUrl(fileName) )
{
return OStringToOUString(fileName, osl_getThreadTextEncoding());
}
OUString uUrlFileName;
OUString uFileName(fileName.getStr(), fileName.getLength(), osl_getThreadTextEncoding());
if ( fileName.indexOf('.') == 0 || fileName.indexOf(SEPARATOR) < 0 )
{
OUString uWorkingDir;
if (osl_getProcessWorkingDir(&uWorkingDir.pData) != osl_Process_E_None) {
OSL_ASSERT(false);
}
if (FileBase::getAbsoluteFileURL(uWorkingDir, uFileName, uUrlFileName)
!= FileBase::E_None)
{
OSL_ASSERT(false);
}
2001-08-17 12:09:50 +00:00
} else
{
if (FileBase::getFileURLFromSystemPath(uFileName, uUrlFileName)
!= FileBase::E_None)
{
OSL_ASSERT(false);
}
2001-08-17 12:09:50 +00:00
}
return uUrlFileName;
}
2000-09-18 14:29:57 +00:00
//*************************************************************************
// FileStream
//*************************************************************************
FileStream::FileStream()
{
}
FileStream::~FileStream()
{
2001-08-17 12:09:50 +00:00
if ( isValid() )
{
fflush(m_pFile);
fclose(m_pFile);
}
2000-09-18 14:29:57 +00:00
}
sal_Bool FileStream::isValid()
{
2001-08-17 12:09:50 +00:00
if ( m_pFile )
return sal_True;
2000-09-18 14:29:57 +00:00
2001-08-17 12:09:50 +00:00
return sal_False;
2000-09-18 14:29:57 +00:00
}
2001-08-17 12:09:50 +00:00
void FileStream::open(const OString& name, FileAccessMode mode)
2000-09-18 14:29:57 +00:00
{
if ( name.getLength() > 0 )
2001-08-17 12:09:50 +00:00
{
2000-09-18 14:29:57 +00:00
m_name = name;
2011-03-01 15:08:22 +00:00
m_pFile = fopen(m_name.getStr(), checkAccessMode(mode));
2001-08-17 12:09:50 +00:00
}
2000-09-18 14:29:57 +00:00
}
2001-08-17 12:09:50 +00:00
void FileStream::close()
2000-09-18 14:29:57 +00:00
{
2001-08-17 12:09:50 +00:00
if ( isValid() )
{
fflush(m_pFile);
fclose(m_pFile);
m_pFile = NULL;
m_name = OString();
}
2000-09-18 14:29:57 +00:00
}
2001-08-17 12:09:50 +00:00
const sal_Char* FileStream::checkAccessMode(FileAccessMode mode)
{
switch( mode )
{
case FAM_READ:
return "r";
case FAM_WRITE:
return "w";
case FAM_APPEND:
return "a";
case FAM_READWRITE_EXIST:
return "r+";
case FAM_READWRITE:
return "w+";
case FAM_READAPPEND:
return "a+";
}
return "w+";
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */