diff options
Diffstat (limited to 'cosv/source/storage')
-rw-r--r-- | cosv/source/storage/dirchain.cxx | 155 | ||||
-rw-r--r-- | cosv/source/storage/file.cxx | 242 | ||||
-rw-r--r-- | cosv/source/storage/makefile.mk | 69 | ||||
-rw-r--r-- | cosv/source/storage/mbstream.cxx | 119 | ||||
-rw-r--r-- | cosv/source/storage/persist.cxx | 113 | ||||
-rw-r--r-- | cosv/source/storage/ploc.cxx | 157 | ||||
-rw-r--r-- | cosv/source/storage/ploc_dir.cxx | 364 | ||||
-rw-r--r-- | cosv/source/storage/plocroot.cxx | 525 |
8 files changed, 1744 insertions, 0 deletions
diff --git a/cosv/source/storage/dirchain.cxx b/cosv/source/storage/dirchain.cxx new file mode 100644 index 000000000000..b183c0f990f7 --- /dev/null +++ b/cosv/source/storage/dirchain.cxx @@ -0,0 +1,155 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * 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. + * + * 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). + * + * 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. + * + ************************************************************************/ + +#include <precomp.h> +#include <cosv/dirchain.hxx> + +// NOT FULLY DECLARED SERVICES +#include <cosv/bstream.hxx> + + + + +namespace csv +{ +namespace ploc +{ + + +DirectoryChain::DirectoryChain() +{ +} + +DirectoryChain::DirectoryChain( const char * i_sSubPath, + bool i_bPathIsAlwaysDir, + const char * i_sDelimiter ) +{ + Set( i_sSubPath, i_bPathIsAlwaysDir, i_sDelimiter ); +} + +DirectoryChain::~DirectoryChain() +{ +} + +void +DirectoryChain::Set( const char * i_sSubPath, + bool i_bPathIsAlwaysDir, + const char * i_sDelimiter ) +{ + csv_assert(i_sDelimiter != 0); + if (i_sSubPath == 0) + return; + + const char * pRestPath = i_sSubPath; + if (*pRestPath == *i_sDelimiter) + ++pRestPath; + + for ( const char * pDirEnd = strchr(pRestPath,*i_sDelimiter); + pDirEnd != 0; + pDirEnd = strchr(pRestPath,*i_sDelimiter) ) + { + aPath.push_back( String(pRestPath, pDirEnd) ); + pRestPath = pDirEnd + 1; + } + if (*pRestPath != 0 AND i_bPathIsAlwaysDir) + aPath.push_back( String(pRestPath) ); +} + +void +DirectoryChain::PushFront( const String & i_sName ) +{ + aPath.insert( aPath.begin(), i_sName ); +} + +void +DirectoryChain::PushFront( const DirectoryChain & i_sPath ) +{ + aPath.insert( aPath.begin(), i_sPath.Begin(), i_sPath.End() ); +} + +void +DirectoryChain::PushBack( const String & i_sName ) +{ + aPath.push_back(i_sName); +} + +void +DirectoryChain::PushBack( const DirectoryChain & i_sPath ) +{ + aPath.insert( aPath.end(), i_sPath.Begin(), i_sPath.End() ); +} + +void +DirectoryChain::PopFront( uintt i_nCount ) +{ + if (i_nCount <= aPath.size()) + aPath.erase( aPath.begin(), aPath.begin() + i_nCount ); + else + aPath.erase( aPath.begin(), aPath.end() ); +} + +void +DirectoryChain::PopBack( uintt i_nCount ) +{ + if (i_nCount <= aPath.size()) + aPath.erase( aPath.end() - i_nCount, aPath.end() ); + else + aPath.erase( aPath.begin(), aPath.end() ); +} + +void +DirectoryChain::Get( ostream & o_rPath, + const char * i_sDelimiter ) const +{ + for ( std::vector<String>::const_iterator it = aPath.begin(); + it != aPath.end(); + ++it ) + { + o_rPath << (*it).c_str() << i_sDelimiter; + } +} + +void +DirectoryChain::Get( bostream & o_rPath, + const char * i_sDelimiter ) const +{ + uintt deliLen = strlen(i_sDelimiter); + + for ( std::vector<String>::const_iterator it = aPath.begin(); + it != aPath.end(); + ++it ) + { + o_rPath.write( (*it).c_str() ); + o_rPath.write( i_sDelimiter, deliLen); + } +} + + + + +} // namespace ploc +} // namespace csv diff --git a/cosv/source/storage/file.cxx b/cosv/source/storage/file.cxx new file mode 100644 index 000000000000..aa1190ec9fab --- /dev/null +++ b/cosv/source/storage/file.cxx @@ -0,0 +1,242 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * 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. + * + * 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). + * + * 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. + * + ************************************************************************/ + +#include <precomp.h> +#include <cosv/file.hxx> + +// NOT FULLY DECLARED SERVICES + + +namespace csv +{ + + +File::File( uintt i_nMode ) + : // aPath, + pStream(0), + nMode(i_nMode), + eLastIO(io_none) +{ +} + +File::File( const ploc::Path & i_rLocation, + uintt i_nMode ) + : aPath(i_rLocation), + pStream(0), + nMode(i_nMode), + eLastIO(io_none) +{ +} + +File::File( const char * i_sLocation, + uintt i_nMode ) + : aPath(i_sLocation), + pStream(0), + nMode(i_nMode), + eLastIO(io_none) +{ +} + +File::File( const String & i_sLocation, + uintt i_nMode ) + : aPath(i_sLocation), + pStream(0), + nMode(i_nMode), + eLastIO(io_none) +{ +} + +File::~File() +{ + if ( inq_is_open() ) + close(); +} + +bool +File::Assign( ploc::Path i_rLocation ) +{ + if (is_open() ) + return false; + + InvalidatePath(); + aPath = i_rLocation; + return true; +} + +bool +File::Assign( const char * i_sLocation ) +{ + if (is_open() ) + return false; + + InvalidatePath(); + aPath.Set( i_sLocation ); + return true; +} + +bool +File::Assign( const String & i_sLocation ) +{ + if (is_open() ) + return false; + + InvalidatePath(); + aPath.Set( i_sLocation ); + return true; +} + +uintt +File::do_read( void * out_pDest, + uintt i_nNrofBytes ) +{ + if ( NOT inq_is_open() ) + return 0; + + if ( eLastIO == io_write ) + ::fseek( pStream, 0, SEEK_CUR ); + uintt ret = position(); + int iRet= ::fread( out_pDest, 1, i_nNrofBytes, pStream ); + if ( iRet < 0 ) { + fprintf(stderr, "warning: read failed in %s line %d \n", __FILE__, __LINE__); + } + ret = position() - ret; + + eLastIO = io_read; + return ret; +} + +bool +File::inq_eod() const +{ + if ( NOT inq_is_open() ) + return true; + return feof(pStream) != 0; +} + +uintt +File::do_write( const void * i_pSrc, + uintt i_nNrofBytes ) +{ + if ( NOT inq_is_open() ) + return 0; + + if ( eLastIO == io_write ) + ::fseek( pStream, 0, SEEK_CUR ); + uintt ret = position(); + ::fwrite( i_pSrc, 1, i_nNrofBytes, pStream ); + ret = position() - ret; + + eLastIO = io_write; + return ret; +} + +uintt +File::do_seek( intt i_nDistance, + seek_dir i_eStartPoint ) +{ + if ( NOT inq_is_open() ) + return uintt(-1); + + static int eSearchDir[3] = { SEEK_SET, SEEK_CUR, SEEK_END }; + + ::fseek( pStream, + intt(i_nDistance), + eSearchDir[i_eStartPoint] ); + return position(); +} + +uintt +File::inq_position() const +{ + if ( inq_is_open() ) + return uintt( ::ftell(pStream) ); + else + return uintt(-1); +} + +bool +File::do_open( uintt i_nOpenMode ) +{ + if ( inq_is_open() ) + { + if ( i_nOpenMode == 0 OR i_nOpenMode == nMode ) + return true; + close(); + } + + if ( i_nOpenMode != 0 ) + nMode = i_nOpenMode; + + const char * sFacadeMode = ""; + switch ( nMode ) + { + case CFM_RW: sFacadeMode = "r+b"; + break; + case CFM_CREATE: sFacadeMode = "w+b"; + break; + case CFM_READ: sFacadeMode = "rb"; + break; + default: + sFacadeMode = "rb"; + } + + pStream = ::fopen( StrPath(), sFacadeMode ); + if ( pStream == 0 AND nMode == CFM_RW ) + { + sFacadeMode = "w+b"; + pStream = ::fopen( StrPath(), sFacadeMode ); + } + + return pStream != 0; +} + +void +File::do_close() +{ + if ( inq_is_open() ) + { + ::fclose(pStream); + pStream = 0; + } + eLastIO = io_none; +} + +bool +File::inq_is_open() const +{ + return pStream != 0; +} + +const ploc::Path & +File::inq_MyPath() const +{ + return aPath; +} + + +} // namespace csv + diff --git a/cosv/source/storage/makefile.mk b/cosv/source/storage/makefile.mk new file mode 100644 index 000000000000..84e396c425ba --- /dev/null +++ b/cosv/source/storage/makefile.mk @@ -0,0 +1,69 @@ +#************************************************************************* +# +# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. +# +# Copyright 2000, 2010 Oracle and/or its affiliates. +# +# OpenOffice.org - a multi-platform office productivity suite +# +# This file is part of OpenOffice.org. +# +# 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. +# +# 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). +# +# 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. +# +#************************************************************************* + +PRJ=..$/.. + +PRJNAME=cosv +TARGET=cosv_storage + +ENABLE_EXCEPTIONS=true + + + +# --- Settings ----------------------------------------------------- + +.INCLUDE : settings.mk + +.INCLUDE : $(PRJ)$/source$/fullcpp.mk + + + + +# --- Files -------------------------------------------------------- + +OBJFILES= \ + $(OBJ)$/dirchain.obj \ + $(OBJ)$/file.obj \ + $(OBJ)$/mbstream.obj \ + $(OBJ)$/persist.obj \ + $(OBJ)$/ploc.obj \ + $(OBJ)$/ploc_dir.obj \ + $(OBJ)$/plocroot.obj + +#SLOFILES= \ +# $(SLO)$/file.obj \ +# $(SLO)$/csfileim.obj \ +# $(SLO)$/memstorg.obj + + + +# --- Targets ------------------------------------------------------ + +.INCLUDE : target.mk + + + diff --git a/cosv/source/storage/mbstream.cxx b/cosv/source/storage/mbstream.cxx new file mode 100644 index 000000000000..5559d1f6d59a --- /dev/null +++ b/cosv/source/storage/mbstream.cxx @@ -0,0 +1,119 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * 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. + * + * 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). + * + * 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. + * + ************************************************************************/ + +#include <precomp.h> +#include <cosv/mbstream.hxx> + +// NOT FULLY DECLARED SERVICES +#include <string.h> + + +namespace csv +{ + + + +mbstream::mbstream( uintt i_nSize ) + : dpOwnedMemorySpace( new char[i_nSize+1] ), + nSize( i_nSize ), + nCurPosition( 0 ) +{ + dpOwnedMemorySpace[i_nSize] = '\0'; +} + +mbstream::~mbstream() +{ + delete [] dpOwnedMemorySpace; +} + +void +mbstream::resize( uintt i_nSize ) +{ + DYN char * pNew = new char[i_nSize]; + memcpy( pNew, dpOwnedMemorySpace, min(i_nSize,nSize) ); + delete [] dpOwnedMemorySpace; + dpOwnedMemorySpace = pNew; + nSize = i_nSize; +} + +uintt +mbstream::do_read( void * out_pDest, + uintt i_nNrofBytes ) +{ + uintt ret = min( i_nNrofBytes, nSize - nCurPosition ); + memcpy( out_pDest, dpOwnedMemorySpace, ret ); + nCurPosition += ret; + return ret; +} + +bool +mbstream::inq_eod() const +{ + return nCurPosition == nSize; +} + +uintt +mbstream::do_write( const void * i_pSrc, + uintt i_nNrofBytes ) +{ + resize( max( 3 * (nSize+1) / 2, nCurPosition + i_nNrofBytes) ); + memcpy( dpOwnedMemorySpace+nCurPosition, i_pSrc, i_nNrofBytes ); + nCurPosition += i_nNrofBytes; + return i_nNrofBytes; +} + +uintt +mbstream::do_seek( intt i_nDistance, + seek_dir i_eStartPoint ) +{ + switch ( i_eStartPoint ) + { + case beg: if ( uintt(i_nDistance) < nSize ) + nCurPosition = uintt(i_nDistance); + break; + case cur: if ( i_nDistance < 0 + ? uintt(-i_nDistance) <= nCurPosition + : uintt(i_nDistance) + nCurPosition < nSize ) + nCurPosition = uintt( intt(nCurPosition) + i_nDistance ); + break; + case end: if ( i_nDistance < 0 + AND uintt(-i_nDistance) < nSize - 1 ) + nCurPosition = uintt( intt(nSize) - 1 + i_nDistance ); + break; + } + return position(); +} + +uintt +mbstream::inq_position() const +{ + return nCurPosition; +} + + +} // namespace csv + diff --git a/cosv/source/storage/persist.cxx b/cosv/source/storage/persist.cxx new file mode 100644 index 000000000000..aa63767c1b0e --- /dev/null +++ b/cosv/source/storage/persist.cxx @@ -0,0 +1,113 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * 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. + * + * 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). + * + * 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. + * + ************************************************************************/ + +#include <precomp.h> +#include <cosv/persist.hxx> + +// NOT FULLY DECLARED SERVICES +#include <cosv/streamstr.hxx> +#include <cosv/ploc.hxx> + + +#ifdef WNT +#include <io.h> + +namespace csv +{ +namespace ploc +{ + +bool +Persistent::Exists() const +{ + return access( StrPath(), 00) == 0; +} + +} // namespace ploc +} // namespace csv + + +#elif defined(UNX) +#include <unistd.h> + +/* +#ifndef __SUNPRO_CC +#include <unistd.h> +#else +#define F_OK 0 // Test for existence of File +extern int access(const char *, int); +#endif +*/ + +namespace csv +{ +namespace ploc +{ + +bool +Persistent::Exists() const +{ + return access( StrPath(), F_OK ) == 0; +} + + +} // namespace ploc +} // namespace csv + +#else +#error For using csv::ploc there has to be defined: WNT or UNX. +#endif + +namespace csv +{ +namespace ploc +{ + +const char * +Persistent::StrPath() const +{ + if (sPath.empty() ) + { +#ifndef CSV_NO_MUTABLE + StreamStr & rsPath = sPath; +#else + StreamStr & rsPath = const_cast< StreamStr& >(sPath); +#endif + rsPath.seekp(0); + rsPath << MyPath(); + if (MyPath().IsDirectory()) + rsPath.pop_back(1); // Remove closing delimiter. + } + return sPath.c_str(); +} + +} // namespace ploc +} // namespace csv + + + + diff --git a/cosv/source/storage/ploc.cxx b/cosv/source/storage/ploc.cxx new file mode 100644 index 000000000000..66061cd8df55 --- /dev/null +++ b/cosv/source/storage/ploc.cxx @@ -0,0 +1,157 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * 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. + * + * 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). + * + * 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. + * + ************************************************************************/ + +#include <precomp.h> +#include <cosv/ploc.hxx> + +// NOT FULLY DECLARED SERVICES +#include <cosv/bstream.hxx> + + + + +namespace csv +{ +namespace ploc +{ + + +Path::Path( const char * i_sPath, + bool i_bPathIsAlwaysDir, + const char * i_sDelimiter ) + : pRoot(0) + // aPath, + // sFile +{ + Set(i_sPath, i_bPathIsAlwaysDir, i_sDelimiter ); +} + +Path::Path( const Path & i_rPath ) + : pRoot(i_rPath.pRoot->CreateCopy()), + aPath(i_rPath.aPath), + sFile(i_rPath.sFile) +{ +} + +Path::~Path() +{ +} + +Path & +Path::operator=( const Path & i_rPath ) +{ + pRoot = i_rPath.pRoot->CreateCopy(); + aPath = i_rPath.aPath; + sFile = i_rPath.sFile; + return *this; +} + + +void +Path::Set( const char * i_sPath, + bool i_bPathIsAlwaysDir, + const char * i_sDelimiter ) +{ + if ( *i_sDelimiter != '\\' AND *i_sDelimiter != '/' ) + return; + + const char * + restPath = 0; + pRoot = Root::Create_( restPath, i_sPath, i_sDelimiter ); + if (restPath == 0) + return; + + aPath.Set(restPath, i_bPathIsAlwaysDir, i_sDelimiter); + + if (NOT i_bPathIsAlwaysDir) + { + const char * + file = strrchr( restPath, *i_sDelimiter ); + if (file == 0) + file = restPath; + else + file++; + sFile = file; + } +} + +void +Path::SetFile( const String & i_sName ) +{ + sFile = i_sName; +} + +const char * +Path::FileExtension() const +{ + const char * + ext = strrchr(sFile, '.'); + if (ext != 0) + ++ext; + else + ext = ""; + return ext; +} + +bool +Path::IsValid() const +{ + return RootDir().OwnDelimiter() != 0; +} + +void +Path::Get( ostream & o_rPath ) const +{ + if (NOT IsValid()) + return; + + pRoot->Get( o_rPath ); + aPath.Get( o_rPath, pRoot->OwnDelimiter() ); + + if ( sFile.length() > 0 ) + o_rPath << sFile; + +} + +void +Path::Get( bostream & o_rPath ) const +{ + if (NOT IsValid()) + return; + + pRoot->Get( o_rPath ); + aPath.Get( o_rPath, pRoot->OwnDelimiter() ); + + if ( sFile.length() > 0 ) + o_rPath.write( sFile ); +} + + + + +} // namespace ploc +} // namespace csv diff --git a/cosv/source/storage/ploc_dir.cxx b/cosv/source/storage/ploc_dir.cxx new file mode 100644 index 000000000000..0f769a5f7e6f --- /dev/null +++ b/cosv/source/storage/ploc_dir.cxx @@ -0,0 +1,364 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * 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. + * + * 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). + * + * 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. + * + ************************************************************************/ + +#include <precomp.h> +#include <cosv/ploc_dir.hxx> + +// NOT FULLY DECLARED SERVICES +#include <cosv/ploc.hxx> + + +namespace csv +{ +namespace ploc +{ + +Directory::Directory() +{ +} + +Directory::Directory( const Path & i_rPath ) + : aPath(i_rPath) + // sPath +{ +} + +Directory::Directory( const Directory & i_rDir ) + : Persistent(), aPath(i_rDir.aPath) + // sPath +{ +} + +Directory::Directory( const char * i_rLocation ) + : aPath(i_rLocation, true) +{ +} + +Directory::Directory( const String & i_rLocation ) + : aPath(i_rLocation.c_str(), true) +{ +} + +Directory::~Directory() +{ +} + +Directory & +Directory::operator+=( const String & i_sName ) +{ + InvalidatePath(); + aPath.DirChain() += i_sName; + return *this; +} + +Directory & +Directory::operator+=( const DirectoryChain & i_sDirChain ) +{ + InvalidatePath(); + aPath.DirChain() += i_sDirChain; + return *this; +} + +Directory & +Directory::operator-=( uintt i_nLevels ) +{ + InvalidatePath(); + aPath.DirChain().PopBack(i_nLevels); + return *this; +} + +bool +Directory::PhysicalCreate( bool i_bCreateParentsIfNecessary ) const +{ + bool ret = PhysicalCreate_Dir( StrPath() ); + if ( ret OR NOT i_bCreateParentsIfNecessary ) + return ret; + + ret = Check_Parent(); + if (ret) + ret = PhysicalCreate_Dir( StrPath() ); + return ret; +} + +bool +Directory::Check_Parent() const +{ + // There is no parent of root directories: + if ( aPath.DirChain().Size() == 0 ) + return false; + + // Become my own parent: + String sLastToken = aPath.DirChain().Back(); + const_cast< Directory* >(this)->operator-=(1); + + // Begin behaving as parent: + bool ret = Exists(); + if (NOT ret) + { + ret = Check_Parent(); + if (ret) + ret = PhysicalCreate_Dir( StrPath() ); + } + // End behaving as parent. + + // Become myself again: + const_cast< Directory* >(this)->operator+=(sLastToken); + return ret; +} + +} // namespace ploc +} // namespace csv + + +#ifdef WNT +#include <direct.h> +#include <io.h> + +namespace csv +{ +namespace ploc +{ + +bool +Directory::PhysicalCreate_Dir( const char * i_sStr ) const +{ + return mkdir( i_sStr ) == 0; +} + +void +Directory::GetContainedDirectories( StringVector & o_rResult ) const +{ + const char * c_sANYDIR = "\\*.*"; + String sNew; + + StreamStr sFilter(200); + sFilter << StrPath() + << c_sANYDIR; + + struct _finddata_t + aEntry; + long hFile = _findfirst( sFilter.c_str(), &aEntry ); + + for ( int bFindMore = (hFile == -1 ? 1 : 0); + bFindMore == 0; + bFindMore = _findnext( hFile, &aEntry ) ) + { + if ( (aEntry.attrib & _A_SUBDIR) AND *aEntry.name != '.' ) + { + sNew = aEntry.name; + o_rResult.push_back( sNew ); + } + } // end for + _findclose(hFile); +} + +void +Directory::GetContainedFiles( StringVector & o_rResult, + const char * i_sFilter, + E_Recursivity i_eRecursivity ) const +{ + StreamStr sNew(240); + sNew << aPath; + StreamStr::size_type + nStartFilename = sNew.tellp(); + + StreamStr sFilter(200); + sFilter << StrPath() + << "\\" + << i_sFilter; + + struct _finddata_t + aEntry; + long hFile = _findfirst( sFilter.c_str(), &aEntry ); + for ( int bFindMore = (hFile == -1 ? 1 : 0); + bFindMore == 0; + bFindMore = _findnext( hFile, &aEntry ) ) + { + sNew.seekp(nStartFilename); + sNew << aEntry.name; + String sNewAsString( sNew.c_str() ); + o_rResult.push_back(sNewAsString); + } // end for + + _findclose(hFile); + if ( i_eRecursivity == flat ) + return; + + // gathering from subdirectories: + StringVector aSubDirectories; + GetContainedDirectories( aSubDirectories ); + + StringVector::const_iterator dEnd = aSubDirectories.end(); + for ( StringVector::const_iterator d = aSubDirectories.begin(); + d != dEnd; + ++d ) + { + Directory aSub(*this); + aSub += *d; + aSub.GetContainedFiles( o_rResult, + i_sFilter, + i_eRecursivity ); + } +} + +} // namespace ploc +} // namespace csv + + +#elif defined(UNX) +#include <sys/types.h> +#include <sys/stat.h> +#include <dirent.h> + +namespace csv +{ +namespace ploc +{ + +bool +Directory::PhysicalCreate_Dir( const char * i_sStr ) const +{ + return mkdir( i_sStr, 00777 ) == 0; +} + +void +Directory::GetContainedDirectories( StringVector & o_rResult ) const +{ + StreamStr sNew(240); + sNew << aPath; + StreamStr::size_type + nStartFilename = sNew.tellp(); + + DIR * pDir = opendir( StrPath() ); + dirent * pEntry = 0; + struct stat aEntryStatus; + + while ( (pEntry = readdir(pDir)) != 0 ) + { + sNew.seekp(nStartFilename); + sNew << pEntry->d_name; + + stat(sNew.c_str(), &aEntryStatus); + if ( (aEntryStatus.st_mode & S_IFDIR) == S_IFDIR + AND *pEntry->d_name != '.' ) + { + String sNew2(pEntry->d_name); + o_rResult.push_back(sNew2); + } // endif (aEntry.attrib == _A_SUBDIR) + } // end while + closedir( pDir ); +} + +void +Directory::GetContainedFiles( StringVector & o_rResult, + const char * i_sFilter, + E_Recursivity i_eRecursivity ) const +{ + StreamStr sNew(240); + sNew << aPath; + StreamStr::size_type + nStartFilename = sNew.tellp(); + + bool bUseFilter = strcmp( i_sFilter, "*.*" ) != 0 + AND strncmp( i_sFilter, "*.", 2) == 0; + + DIR * pDir = opendir( StrPath() ); + dirent * pEntry = 0; + struct stat aEntryStatus; + + while ( (pEntry = readdir(pDir)) != 0 ) + { + sNew.seekp(nStartFilename); + sNew << pEntry->d_name; + + stat(sNew.c_str(), &aEntryStatus); + if ( (aEntryStatus.st_mode & S_IFDIR) == S_IFDIR ) + continue; // Don't gather directories. + + if ( bUseFilter ) + { + const char * pEnding = strrchr(pEntry->d_name,'.'); + if (pEnding == 0) + continue; + if ( strcasecmp( pEnding + 1, i_sFilter + 2 ) != 0 ) + continue; + } + + sNew.seekp(nStartFilename); + sNew << pEntry->d_name; + String sNewAsString( sNew.c_str() ); + o_rResult.push_back(sNewAsString); + } // end while + + closedir( pDir ); + if ( i_eRecursivity == flat ) + return; + + // gathering from subdirectories: + StringVector aSubDirectories; + GetContainedDirectories( aSubDirectories ); + + StringVector::const_iterator dEnd = aSubDirectories.end(); + for ( StringVector::const_iterator d = aSubDirectories.begin(); + d != dEnd; + ++d ) + { + Directory aSub(*this); + aSub += *d; + aSub.GetContainedFiles( o_rResult, + i_sFilter, + i_eRecursivity ); + } +} + +} // namespace ploc +} // namespace csv + + +#else +#error For using csv::ploc there has to be defined: WNT or UNX. +#endif + + +namespace csv +{ +namespace ploc +{ + +const Path & +Directory::inq_MyPath() const +{ + return aPath; +} + + + +} // namespace ploc +} // namespace csv + + + diff --git a/cosv/source/storage/plocroot.cxx b/cosv/source/storage/plocroot.cxx new file mode 100644 index 000000000000..441a241b3a63 --- /dev/null +++ b/cosv/source/storage/plocroot.cxx @@ -0,0 +1,525 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * 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. + * + * 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). + * + * 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. + * + ************************************************************************/ + +#include <precomp.h> +#include <cosv/ploc.hxx> + +// NOT FULLY DECLARED SERVICES +#include <ctype.h> +#include <cosv/bstream.hxx> +#include <cosv/csv_ostream.hxx> + + +namespace csv +{ +namespace ploc +{ + + +class UnixRootDir : public Root +{ + public: + UnixRootDir(); + + virtual void Get( + ostream & o_rPath ) const; + virtual void Get( + bostream & o_rPath ) const; + virtual DYN Root * CreateCopy() const; + virtual const char * + OwnDelimiter() const; +}; + +class WorkingDir : public Root +{ + public: + WorkingDir( + const char * i_sDelimiter = Delimiter() ); + + virtual void Get( + ostream & o_rPath ) const; + virtual void Get( + bostream & o_rPath ) const; + virtual DYN Root * CreateCopy() const; + virtual const char * + OwnDelimiter() const; + private: + String sOwnDelimiter; +}; + +class WinRootDir : public Root +{ + public: + WinRootDir(); + + virtual void Get( + ostream & o_rPath ) const; + virtual void Get( + bostream & o_rPath ) const; + virtual DYN Root * CreateCopy() const; + virtual const char * + OwnDelimiter() const; +}; + +class WinDrive : public Root +{ + public: + WinDrive( + char i_cDrive ); + virtual void Get( + ostream & o_rPath ) const; + virtual void Get( + bostream & o_rPath ) const; + virtual DYN Root * CreateCopy() const; + virtual const char * + OwnDelimiter() const; + private: + char cDrive; +}; + +class WinDriveRootDir : public Root +{ + public: + WinDriveRootDir( + const char * i_sPath ); + WinDriveRootDir( + char i_cDrive ); + + virtual void Get( + ostream & o_rPath ) const; + virtual void Get( + bostream & o_rPath ) const; + virtual DYN Root * CreateCopy() const; + virtual const char * + OwnDelimiter() const; + private: + char cDrive; +}; + +class UNCRoot : public Root +{ + public: + UNCRoot( + const char * i_sPath ); + UNCRoot( + const String & i_sComputer, + const String & i_sEntryPt ); + + virtual void Get( + ostream & o_rPath ) const; + virtual void Get( + bostream & o_rPath ) const; + virtual DYN Root * CreateCopy() const; + virtual const char * + OwnDelimiter() const; + private: + String sComputer; + String sEntryPt; +}; + +class InvalidRoot : public Root +{ + public: + virtual void Get( + ostream & o_rPath ) const; + virtual void Get( + bostream & o_rPath ) const; + virtual DYN Root * CreateCopy() const; + virtual const char * + OwnDelimiter() const; +}; + + +DYN Root * +Create_WindowsRoot( const char * & o_sPathAfterRoot, + const char * i_sPath ) +{ + if (i_sPath[0] == '\\') + { + if (i_sPath[1] == '\\') + { // UNC path name + o_sPathAfterRoot = strchr(i_sPath+2,'\\'); + if (o_sPathAfterRoot != 0) + { + o_sPathAfterRoot = strchr(o_sPathAfterRoot+1,'\\'); + if (o_sPathAfterRoot != 0) + ++o_sPathAfterRoot; + return new UNCRoot(i_sPath); + } + return new InvalidRoot; // Incomplete UNC root. + } + else + { + o_sPathAfterRoot = i_sPath+1; + return new WinRootDir; + } + } + else if (i_sPath[1] == ':') + { + if ( i_sPath[2] == '\\') + { + o_sPathAfterRoot = i_sPath + 3; + return new WinDriveRootDir(i_sPath); + } + else + { + o_sPathAfterRoot = i_sPath + 2; + return new WinDrive(*i_sPath); + } + } + else + { + o_sPathAfterRoot = i_sPath; + return new WorkingDir("\\"); + } +} + +DYN Root * +Create_UnixRoot( const char * & o_sPathAfterRoot, + const char * i_sPath ) +{ + if (*i_sPath == '/') + { + o_sPathAfterRoot = i_sPath + 1; + return new UnixRootDir; + } + else // + { + o_sPathAfterRoot = i_sPath; + return new WorkingDir("/"); + } // endif +} + + +//********************** Root ****************************// + +Root::~Root() +{ + +} + +DYN Root * +Root::Create_( const char * & o_sPathAfterRoot, + const char * i_sPath, + const char * i_sDelimiter ) +{ + if (i_sPath[0] == '.') + { + switch ( i_sPath[1] ) + { + case '\0': o_sPathAfterRoot = i_sPath + 1; + break; + case '\\': o_sPathAfterRoot = i_sPath + 2; + break; + case '/': o_sPathAfterRoot = i_sPath + 2; + break; + case '.': o_sPathAfterRoot = i_sPath; + break; + default: + o_sPathAfterRoot = 0; + return new InvalidRoot; + } // end switch (i_sPath[1]) + + return new WorkingDir; + } // end if (i_sPath[0] == '.') + + switch (*i_sDelimiter) + { + case '\\': return Create_WindowsRoot(o_sPathAfterRoot, i_sPath); + case '/': return Create_UnixRoot(o_sPathAfterRoot, i_sPath); + } + + o_sPathAfterRoot = 0; + return new InvalidRoot; +} + + + +//********************** UnixRootDir ****************************// + + +UnixRootDir::UnixRootDir() +{ +} + +void +UnixRootDir::Get( ostream & o_rPath ) const +{ + o_rPath << '/'; +} + +void +UnixRootDir::Get( bostream & o_rPath ) const +{ + o_rPath.write( "/", 1 ); +} + +DYN Root * +UnixRootDir::CreateCopy() const +{ + return new UnixRootDir; +} + +const char * +UnixRootDir::OwnDelimiter() const +{ + return "/"; +} + + +//********************** WorkingDir ****************************// + +WorkingDir::WorkingDir( const char * i_sDelimiter ) + : sOwnDelimiter(i_sDelimiter) +{ +} + +void +WorkingDir::Get( ostream & o_rPath ) const +{ + o_rPath << '.' << sOwnDelimiter; +} + +void +WorkingDir::Get( bostream & o_rPath ) const +{ + o_rPath.write( ".", 1 ); + o_rPath.write( sOwnDelimiter ); +} + +DYN Root * +WorkingDir::CreateCopy() const +{ + return new WorkingDir(sOwnDelimiter); +} + +const char * +WorkingDir::OwnDelimiter() const +{ + return sOwnDelimiter; +} + + +//********************** WinRootDir ****************************// + +WinRootDir::WinRootDir() +{ +} + +void +WinRootDir::Get( ostream & o_rPath ) const +{ + o_rPath << '\\'; +} + +void +WinRootDir::Get( bostream & o_rPath ) const +{ + o_rPath.write( "\\", 1 ); +} + +DYN Root * +WinRootDir::CreateCopy() const +{ + return new WinRootDir; +} + +const char * +WinRootDir::OwnDelimiter() const +{ + return "\\"; +} + + +//********************** WinDrive ****************************// + +WinDrive::WinDrive( char i_cDrive ) + : cDrive(static_cast< char >(toupper(i_cDrive))) +{ +} + +void +WinDrive::Get( ostream & o_rPath ) const +{ + o_rPath << cDrive << ':'; +} + +void +WinDrive::Get( bostream & o_rPath ) const +{ + static char buf_[3] = " :"; + buf_[0] = cDrive; + o_rPath.write( &buf_[0], 2 ); +} + +DYN Root * +WinDrive::CreateCopy() const +{ + return new WinDrive(cDrive); +} + +const char * +WinDrive::OwnDelimiter() const +{ + return "\\"; +} + + +//********************** WinDriveRootDir ****************************// + +WinDriveRootDir::WinDriveRootDir( const char * i_sPath ) + : cDrive(static_cast< char >(toupper(*i_sPath))) +{ + if ( 'A' > cDrive OR 'Z' < cDrive ) + cDrive = 0; +} + +WinDriveRootDir::WinDriveRootDir( char i_cDrive ) + : cDrive(i_cDrive) +{ +} + +void +WinDriveRootDir::Get( ostream & o_rPath ) const +{ + o_rPath << cDrive << ":\\"; +} + +void +WinDriveRootDir::Get( bostream & o_rPath ) const +{ + static char buf_[4] = " :\\"; + buf_[0] = cDrive; + o_rPath.write( &buf_[0], 3 ); +} + +DYN Root * +WinDriveRootDir::CreateCopy() const +{ + return new WinDriveRootDir(cDrive); +} + +const char * +WinDriveRootDir::OwnDelimiter() const +{ + return "\\"; +} + + +//********************** UNCRoot ****************************// + +UNCRoot::UNCRoot( const char * i_sPath ) +// : // sComputer, + // sEntryPt +{ + const char * pRestPath = i_sPath + 2; + const char * pDirEnd = strchr(pRestPath, '\\'); + csv_assert(pDirEnd != 0); + + sComputer = String(pRestPath, pDirEnd - pRestPath); + pRestPath = pDirEnd+1; + pDirEnd = strchr(pRestPath, '\\'); + + if ( pDirEnd != 0 ) + { + sEntryPt = String(pRestPath, pDirEnd - pRestPath); + } + else + { + sEntryPt = pRestPath; + } +} + +UNCRoot::UNCRoot( const String & i_sComputer, + const String & i_sEntryPt ) + : sComputer(i_sComputer), + sEntryPt(i_sEntryPt) +{ +} + +void +UNCRoot::Get( ostream & o_rPath ) const +{ + o_rPath << "\\\\" << sComputer << '\\' << sEntryPt << "\\"; +} + +void +UNCRoot::Get( bostream & o_rPath ) const +{ + o_rPath.write( "\\\\", 2 ); + o_rPath.write( sComputer ); + o_rPath.write( "\\", 1 ); + o_rPath.write( sEntryPt ); + o_rPath.write( "\\", 1 ); +} + +DYN Root * +UNCRoot::CreateCopy() const +{ + return new UNCRoot(sComputer,sEntryPt); +} + +const char * +UNCRoot::OwnDelimiter() const +{ + return "\\"; +} + + + +//********************** InvalidRoot ****************************// + +void +InvalidRoot::Get( ostream & ) const +{ +} + +void +InvalidRoot::Get( bostream & ) const +{ +} + +DYN Root * +InvalidRoot::CreateCopy() const +{ + return new InvalidRoot; +} + +const char * +InvalidRoot::OwnDelimiter() const +{ + return 0; +} + + + + +} // namespace ploc +} // namespace csv + + + |