Merged enterprise features

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@2746 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Paul Holmes-Higgin
2006-05-03 18:34:13 +00:00
parent 405c00bd8e
commit c37ff8805c
83 changed files with 15809 additions and 9 deletions

View File

@@ -0,0 +1,420 @@
#include <iostream>
#include "alfresco\Alfresco.hpp"
#include "util\String.h"
#include "util\DataBuffer.h"
#include "util\FileName.h"
#include <shellapi.h>
using namespace std;
using namespace JLAN;
// Function prototypes
bool doFolderStatus( Alfresco& alfresco, const wchar_t* fileSpec = L"*.*");
bool doCheckInOut( Alfresco& alfresco, StringList& files);
bool doCheckIn( Alfresco& alfresco, PTR_AlfrescoFileInfo& fileInfo);
bool doCheckOut( Alfresco& alfresco, PTR_AlfrescoFileInfo& fileInfo);
/**
* Alfresco Windows Drag And Drop Application
*
* @author GKSpencer
*/
int wmain( int argc, wchar_t* argv[], wchar_t* envp[]) {
// Output a startup banner
wcout << L"Alfresco Drag And Drop Application" << endl;
wcout << L"----------------------------------" << endl;
// Check if the app is running from a network drive
String appPath(argv[0]);
// String appPath("\\\\StarlaA\\Alfresco\\Garys Space\\AlfrescoApp.exe");
// String appPath("Z:\\Garys Space\\AlfrescoApp.exe");
// argc = 2;
// Looks like a UNC path, trim off the application name
int pos = appPath.lastIndexOf(PathSeperator);
if ( pos < 0) {
wcout << L"%% Invalid application path, " << appPath << endl;
return 1;
}
// Get the path to the folder containing the application
String folderPath = appPath.substring(0, pos);
// Create the Alfresco interface
Alfresco alfresco(folderPath);
if ( alfresco.isAlfrescoFolder()) {
// If there are no file paths on the command line then display a status page for the files
// in the Alfresco folder
if ( argc == 1) {
// Display status for the files in the Alfresco folder
doFolderStatus( alfresco);
}
else {
// Build a list of the file names
StringList fileList;
for ( int i = 1; i < argc; i++)
fileList.addString( String(argv[i]));
// fileList.addString(L"N:\\testArea\\msword\\CIFSLOG.doc");
// fileList.addString(L"\\\\StarlaA\\Alfresco\\Garys Space\\CIFSLOG.doc");
// Process the file list and check in or out each file
doCheckInOut( alfresco, fileList);
}
}
else {
wcout << L"%% Not a valid Alfresco CIFS folder, " << folderPath << endl;
return 1;
}
// Wait for user input
wcout << L"Press <Enter> to continue ..." << flush;
getchar();
}
/**
* Display file status of the files in the target Alfresco folder
*
* @param Alfresco& alfresco
* @param const wchar_t* fileSpec
* @return bool
*/
bool doFolderStatus( Alfresco& alfresco, const wchar_t* fileSpec) {
// Get the base UNC path
String uncPath = alfresco.getUNCPath();
uncPath.append(PathSeperator);
// Search the Alfresco folder
WIN32_FIND_DATA findData;
String searchPath = uncPath;
searchPath.append( fileSpec);
bool sts = false;
HANDLE fHandle = FindFirstFile( searchPath, &findData);
if ( fHandle != INVALID_HANDLE_VALUE) {
// Loop until all files have been returned
PTR_AlfrescoFileInfo pFileInfo;
sts = true;
while ( fHandle != INVALID_HANDLE_VALUE) {
// Get the file name, ignore the '.' and '..' files
String fName = findData.cFileName;
if ( fName.equals(L".") || fName.equals(L"..")) {
// Get the next file/folder name in the search
if ( FindNextFile( fHandle, &findData) == 0)
fHandle = INVALID_HANDLE_VALUE;
continue;
}
// Get the file information for the current file folder
pFileInfo = alfresco.getFileInformation( findData.cFileName);
if ( pFileInfo.get() != NULL) {
// Output the file details
wcout << pFileInfo->getName() << endl;
if ( pFileInfo->isType() == TypeFolder)
wcout << L" [Folder]" << endl;
if ( pFileInfo->isWorkingCopy())
wcout << L" [Work: " << pFileInfo->getCopyOwner() << L", " << pFileInfo->getCopiedFrom() << L"]" << endl;
if ( pFileInfo->getLockType() != LockNone)
wcout << L" [Lock: " << (pFileInfo->getLockType() == LockRead ? L"READ" : L"WRITE") << L", " <<
pFileInfo->getLockOwner() << L"]" << endl;
if ( pFileInfo->hasContent())
wcout << L" [Content: " << pFileInfo->getContentLength() << L", " << pFileInfo->getContentType() << L"]" << endl;;
// Get the next file/folder name in the search
if ( FindNextFile( fHandle, &findData) == 0)
fHandle = INVALID_HANDLE_VALUE;
}
else {
sts = false;
fHandle = INVALID_HANDLE_VALUE;
}
}
}
// Return status
return sts;
}
/**
* Process the list of files and check in or out each file
*
* @param alfresco Alfresco&
* @param files StringList&
*/
bool doCheckInOut( Alfresco& alfresco, StringList& files) {
// Process the list of files and either check in the file if it is a working copy or check out
// the file
for ( unsigned int i = 0; i < files.numberOfStrings(); i++) {
// Get the current file name
String curFile = files.getStringAt( i);
// Check if the path is on an Alfresco mapped drive
if ( alfresco.isMappedDrive() && curFile.startsWithIgnoreCase( alfresco.getDrivePath())) {
// Convert the path to a UNC path
String uncPath = alfresco.getRootPath();
uncPath.append( curFile.substring(2));
curFile = uncPath;
}
// Check that the path is to a file
bool copyFile = false;
DWORD attr = GetFileAttributes( curFile);
if ( attr != INVALID_FILE_ATTRIBUTES && (attr & FILE_ATTRIBUTE_DIRECTORY) == 0) {
// Get the file name from the path
StringList nameParts = FileName::splitPath( curFile);
String curName = nameParts.getStringAt( 1);
// Get the Alfresco file status information
PTR_AlfrescoFileInfo pFileInfo = alfresco.getFileInformation( curName);
// If the path is to a file that is not on the Alfresco share the file will need to be copied,
// after checking the status of a matching file in the Alfresco folder
if ( curFile.length() >= 3 && curFile.substring(1,3).equals( L":\\")) {
// Check if there is an existing file with the same name
if ( pFileInfo.get() != NULL) {
// Check if the file is a working copy
if ( pFileInfo->isWorkingCopy()) {
// Local file matches a working copy file in the Alfresco folder
wcout << L"Found matching working copy for local file " << curName << endl;
}
else if ( pFileInfo->getLockType() != LockNone) {
// File is locked, may be the original document
wcout << L"%% Destination file " << curName << L" is locked" << endl;
return false;
}
else {
// Indicate that we have copied a new file to the Alfresco share, do not check in/out
copyFile = true;
}
}
else {
// Indicate that we have copied a new file to the Alfresco share, do not check in/out
copyFile = true;
}
// Build the from/to paths, must be double null terminated
wchar_t fromPath[MAX_PATH + 1];
wchar_t toPath[MAX_PATH + 1];
memset( fromPath, 0, sizeof( fromPath));
memset( toPath, 0, sizeof( toPath));
wcscpy( fromPath, curFile.data());
wcscpy( toPath, alfresco.getUNCPath());
// Copy the local file to the Alfresco folder
SHFILEOPSTRUCT fileOpStruct;
memset( &fileOpStruct, 0, sizeof(SHFILEOPSTRUCT));
fileOpStruct.hwnd = HWND_DESKTOP;
fileOpStruct.wFunc = FO_COPY;
fileOpStruct.pFrom = fromPath;
fileOpStruct.pTo = toPath;
fileOpStruct.fFlags= 0;
fileOpStruct.fAnyOperationsAborted =false;
// Copy the file to the Alfresco folder
if ( SHFileOperation( &fileOpStruct) != 0) {
// File copy failed
wcout << L"%% Failed to copy file " << curFile << endl;
return false;
}
else if ( fileOpStruct.fAnyOperationsAborted) {
// User aborted the file copy
wcout << L"%% Copy aborted for " << curFile << endl;
return false;
}
// Get the file information for the copied file
pFileInfo = alfresco.getFileInformation( curName);
}
// Check in or check out the file
if ( pFileInfo.get() != NULL) {
// Check if the file should be checked in/out
if ( copyFile == false) {
// Check if the file is a working copy, if so then check it in
if ( pFileInfo->isWorkingCopy()) {
// Check in the file
doCheckIn( alfresco, pFileInfo);
}
else if ( pFileInfo->getLockType() == LockNone) {
// Check out the file
doCheckOut( alfresco, pFileInfo);
}
else {
// File is locked, may already be checked out
wcout << L"%% File " << curFile << L" is locked" << endl;
}
}
else {
// No existing file to link the copied file to
wcout << L"Copied file " << curFile << L" to Alfresco share" << endl;
}
}
else
wcout << L"%% Failed to get file status for " << curFile << endl;
}
else
wcout << L"%% Path " << curFile << L" is a folder, ignored" << endl;
}
// Return status
return true;
}
/**
* Check in the specified file
*
* @param alfresco Alfresco&
* @param pFileInfo PTR_AlfrescoFileInfo&
* @return bool
*/
bool doCheckIn( Alfresco& alfresco, PTR_AlfrescoFileInfo& pFileInfo) {
bool checkedIn = false;
try {
// Check in the specified file
alfresco.checkIn( pFileInfo->getName());
wcout << L"Checked in file " << pFileInfo->getName() << endl;
// Indicate that the check in was successful
checkedIn = true;
}
catch (Exception ex) {
wcerr << L"%% Error checking in file " << pFileInfo->getName() << endl;
wcerr << L" " << ex.getMessage() << endl;
}
// Return the check in status
return checkedIn;
}
/**
* Check out the specified file
*
* @param alfresco Alfresco&
* @param pFileInfo PTR_AlfrescoFileInfo&
* @return bool
*/
bool doCheckOut( Alfresco& alfresco, PTR_AlfrescoFileInfo& pFileInfo) {
bool checkedOut = false;
try {
// Check out the specified file
String workingCopy;
alfresco.checkOut( pFileInfo->getName(), workingCopy);
wcout << L"Checked out file " << pFileInfo->getName() << " to " << workingCopy << endl;
// Indicate that the check out was successful
checkedOut = true;
}
catch (Exception ex) {
wcerr << L"%% Error checking out file " << pFileInfo->getName() << endl;
wcerr << L" " << ex.getMessage() << endl;
}
// Return the check out status
return checkedOut;
}

View File

@@ -0,0 +1,448 @@
/*
* Copyright (C) 2005 Alfresco, Inc.
*
* Licensed under the Alfresco Network License. You may obtain a
* copy of the License at
*
* http://www.alfrescosoftware.com/legal/
*
* Please view the license relevant to your network subscription.
*
* BY CLICKING THE "I UNDERSTAND AND ACCEPT" BOX, OR INSTALLING,
* READING OR USING ALFRESCO'S Network SOFTWARE (THE "SOFTWARE"),
* YOU ARE AGREEING ON BEHALF OF THE ENTITY LICENSING THE SOFTWARE
* ("COMPANY") THAT COMPANY WILL BE BOUND BY AND IS BECOMING A PARTY TO
* THIS ALFRESCO NETWORK AGREEMENT ("AGREEMENT") AND THAT YOU HAVE THE
* AUTHORITY TO BIND COMPANY. IF COMPANY DOES NOT AGREE TO ALL OF THE
* TERMS OF THIS AGREEMENT, DO NOT SELECT THE "I UNDERSTAND AND AGREE"
* BOX AND DO NOT INSTALL THE SOFTWARE OR VIEW THE SOURCE CODE. COMPANY
* HAS NOT BECOME A LICENSEE OF, AND IS NOT AUTHORIZED TO USE THE
* SOFTWARE UNLESS AND UNTIL IT HAS AGREED TO BE BOUND BY THESE LICENSE
* TERMS. THE "EFFECTIVE DATE" FOR THIS AGREEMENT SHALL BE THE DAY YOU
* CHECK THE "I UNDERSTAND AND ACCEPT" BOX.
*/
#include "alfresco\Alfresco.hpp"
#include "util\DataBuffer.h"
#include "util\Exception.h"
#include "util\Integer.h"
#include <WinNetWk.h>
using namespace Alfresco;
using namespace std;
// Define exceptions
EXCEPTION_CLASS(Alfresco, BadInterfaceException);
/**
* Class constructor
*
* @param path UNC or mapped drive path to an Alfresco folder on CIFS server
*/
AlfrescoInterface::AlfrescoInterface(String& path) {
// Clear the file handle
m_handle = INVALID_HANDLE_VALUE;
// Check if the path is to a mapped drive
String alfPath = path;
if ( alfPath.length() >= 3 && alfPath.substring(1,3).equals( L":\\")) {
// Try and convert the local path to a UNC path
m_mappedDrive = alfPath.substring(0, 2);
wchar_t remPath[MAX_PATH];
DWORD remPathLen = MAX_PATH;
DWORD sts = WNetGetConnection(( LPWSTR) m_mappedDrive.data(), remPath, &remPathLen);
if ( sts != NO_ERROR)
return;
// Build the UNC path to the folder
alfPath = remPath;
if ( alfPath.endsWith( PathSeperator) == false)
alfPath.append( PathSeperator);
if ( path.length() > 3)
alfPath.append( path.substring( 3));
}
// Save the UNC path
m_uncPath = alfPath;
// Check if the UNC path is valid
if ( m_uncPath.startsWith(UNCPathPrefix)) {
// Strip any trailing separator from the path
if ( m_uncPath.endsWith(PathSeperator))
m_uncPath = m_uncPath.substring(0, m_uncPath.length() - 1);
// Make sure the path is to a folder
DWORD attr = GetFileAttributes(m_uncPath);
if ( attr != INVALID_FILE_ATTRIBUTES && (attr & FILE_ATTRIBUTE_DIRECTORY)) {
// Open the path
m_handle = CreateFile(m_uncPath, FILE_WRITE_DATA, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
}
// Set the root path
int pos = m_uncPath.indexOf( PathSeperator, 2);
if ( pos != -1) {
pos = m_uncPath.indexOf( PathSeperator, pos + 1);
if ( pos == -1)
m_rootPath = m_uncPath;
else
m_rootPath = m_uncPath.substring(0, pos);
}
}
}
/**
* Class destructor
*/
AlfrescoInterface::~AlfrescoInterface() {
// Close the folder
if ( m_handle != INVALID_HANDLE_VALUE)
CloseHandle(m_handle);
}
/**
* Check if the path is a folder on an Alfresco CIFS server
*
* @return bool
*/
bool AlfrescoInterface::isAlfrescoFolder( void) {
// Check if the handle is valid, if not then the path is not valid
if ( m_handle == INVALID_HANDLE_VALUE)
return false;
// Send a special I/O control to the Alfresco share to check that it is an Alfresco CIFS server
DataBuffer reqbuf(16);
DataBuffer respbuf(256);
reqbuf.putFixedString(IOSignature, IOSignatureLen);
bool alfFolder = false;
try {
sendIOControl( FSCTL_ALFRESCO_PROBE, reqbuf, respbuf);
alfFolder = true;
}
catch ( Exception ex) {
}
// If the folder is not an Alfresco CIFS folder then close the folder
if ( alfFolder == false) {
CloseHandle(m_handle);
m_handle = INVALID_HANDLE_VALUE;
}
// Return the folder status
return alfFolder;
}
/**
* Return Alfresco file information for the specified file/folder
*
* @param fileName const wchar_t*
* @return PTR_AlfrescoFileInfo
*/
PTR_AlfrescoFileInfo AlfrescoInterface::getFileInformation( const wchar_t* fileName) {
// Check if the folder handle is valid
if ( m_handle == INVALID_HANDLE_VALUE)
throw BadInterfaceException();
// Build the file information I/O control request
DataBuffer reqbuf( 256);
DataBuffer respbuf( 512);
reqbuf.putFixedString( IOSignature, IOSignatureLen);
reqbuf.putString( fileName);
sendIOControl( FSCTL_ALFRESCO_FILESTS, reqbuf, respbuf);
// Unpack the request status
PTR_AlfrescoFileInfo pFileInfo;
unsigned int reqSts = respbuf.getInt();
if ( reqSts == StsSuccess) {
// Create the file information
pFileInfo.reset( new AlfrescoFileInfo( fileName));
// Unpack the file details
pFileInfo->setType( respbuf.getInt());
if ( pFileInfo->isType() == TypeFile) {
// Unpack the working copy details
if ( respbuf.getInt() == True) {
String workOwner = respbuf.getString();
String workFrom = respbuf.getString();
pFileInfo->setWorkingCopy( workOwner, workFrom);
}
// Unpack the lock details
unsigned int lockType = respbuf.getInt();
String lockOwner;
if ( lockType != LockNone)
lockOwner = respbuf.getString();
pFileInfo->setLockType( lockType, lockOwner);
// Unpack the content details
if ( respbuf.getInt() == True) {
LONG64 siz = respbuf.getLong();
String mimeType = respbuf.getString();
pFileInfo->setContent( siz, mimeType);
}
}
}
// Return the file information
return pFileInfo;
}
/**
* Check in a working copy file
*
* @param fileName const wchar_t*
* @param keepCheckedOut bool
*/
void AlfrescoInterface::checkIn( const wchar_t* fileName, bool keepCheckedOut) {
// Check if the folder handle is valid
if ( m_handle == INVALID_HANDLE_VALUE)
throw BadInterfaceException();
// Build the file information I/O control request
DataBuffer reqbuf( 256);
DataBuffer respbuf( 128);
reqbuf.putFixedString( IOSignature, IOSignatureLen);
reqbuf.putString( fileName);
reqbuf.putInt( keepCheckedOut ? True : False);
sendIOControl( FSCTL_ALFRESCO_CHECKIN, reqbuf, respbuf);
// Get the status code
unsigned int stsCode = respbuf.getInt();
if ( stsCode == StsSuccess)
return;
else {
// Get the error message, if available
String errMsg;
if ( respbuf.getAvailableLength() > 0)
errMsg = respbuf.getString();
else {
errMsg = "Error code ";
errMsg.append( Integer::toString( stsCode));
}
// Throw an exception
throw Exception( errMsg);
}
}
/**
* Check out a file and return the working copy file name
*
* @param fileName const wchar_t*
* @param workingCopy String&
*/
void AlfrescoInterface::checkOut( const wchar_t* fileName, String& workingCopy) {
// Check if the folder handle is valid
if ( m_handle == INVALID_HANDLE_VALUE)
throw BadInterfaceException();
// Build the file information I/O control request
DataBuffer reqbuf( 256);
DataBuffer respbuf( 256);
reqbuf.putFixedString( IOSignature, IOSignatureLen);
reqbuf.putString( fileName);
sendIOControl( FSCTL_ALFRESCO_CHECKOUT, reqbuf, respbuf);
// Get the status code
unsigned int stsCode = respbuf.getInt();
if ( stsCode == StsSuccess) {
// Get the working copy file name
workingCopy = respbuf.getString();
}
else {
// Get the error message, if available
String errMsg;
if ( respbuf.getAvailableLength() > 0)
errMsg = respbuf.getString();
else {
errMsg = "Error code ";
errMsg.append( Integer::toString( stsCode));
}
// Throw an exception
throw Exception( errMsg);
}
}
/**
* Send an I/O control request to the Alfresco CIFS server, receive and validate the response
*
* @param ctlCode const unsigned int
* @param reqbuf DataBuffer&
* @param respbuf DataBuffer&
*/
void AlfrescoInterface::sendIOControl( const unsigned int ctlCode, DataBuffer& reqbuf, DataBuffer& respbuf) {
// Send the I/O control request, receive the response
DWORD retLen = 0;
BOOL res = DeviceIoControl( m_handle, ctlCode, reqbuf.getBuffer(), reqbuf.getLength(),
respbuf.getBuffer(), respbuf.getBufferLength(), &retLen, (LPOVERLAPPED) NULL);
if ( res) {
// Validate the reply signature
if ( retLen >= IOSignatureLen) {
respbuf.setLength(retLen);
respbuf.setEndOfBuffer();
String sig = respbuf.getString(IOSignatureLen, false);
if ( sig.equals(IOSignature) == false)
throw Exception( L"Invalid I/O control signature received");
}
}
else
throw Exception( L"Send I/O control error", Integer::toString( GetLastError()));
}
/**
* Class constructor
*
* @param fileName const wchar_t*
*/
AlfrescoFileInfo::AlfrescoFileInfo(const wchar_t* fileName) {
m_name = fileName;
m_workingCopy = false;
m_lockType = LockNone;
m_hasContent = false;
m_contentLen = 0L;
}
/**
* Set the working copy owner and copied from document path
*
* @param owner const wchar_t*
* @param copiedFrom const wchar_t*
*/
void AlfrescoFileInfo::setWorkingCopy( const wchar_t* owner, const wchar_t* copiedFrom) {
m_workingCopy = false;
m_workOwner = L"";
m_copiedFrom = L"";
if ( owner != NULL) {
m_workingCopy = true;
m_workOwner = owner;
if ( copiedFrom != NULL)
m_copiedFrom = copiedFrom;
}
}
/**
* Set the lock type and owner
*
* @param typ unsigned int
* @param owner const wchar_t*
*/
void AlfrescoFileInfo::setLockType( unsigned int typ, const wchar_t* owner) {
m_lockType = typ;
m_lockOwner = owner;
}
/**
* Set the lock type and owner
*
* @param siz LONG64
* @param mimeType const wchar_t*
*/
void AlfrescoFileInfo::setContent( LONG64 siz, const wchar_t* mimeType) {
m_hasContent = true;
m_contentLen = siz;
m_contentMimeType = mimeType;
}
/**
* Equality operator
*
* @return bool
*/
bool AlfrescoFileInfo::operator==( const AlfrescoFileInfo& finfo) {
if ( getName().equals( finfo.getName()))
return true;
return false;
}
/**
* Less than operator
*
* @return bool
*/
bool AlfrescoFileInfo::operator<( const AlfrescoFileInfo& finfo) {
if ( finfo.getName().compareTo( getName()) < 0)
return true;
return false;
}

View File

@@ -0,0 +1,232 @@
/*
* Copyright (C) 2005 Alfresco, Inc.
*
* Licensed under the Alfresco Network License. You may obtain a
* copy of the License at
*
* http://www.alfrescosoftware.com/legal/
*
* Please view the license relevant to your network subscription.
*
* BY CLICKING THE "I UNDERSTAND AND ACCEPT" BOX, OR INSTALLING,
* READING OR USING ALFRESCO'S Network SOFTWARE (THE "SOFTWARE"),
* YOU ARE AGREEING ON BEHALF OF THE ENTITY LICENSING THE SOFTWARE
* ("COMPANY") THAT COMPANY WILL BE BOUND BY AND IS BECOMING A PARTY TO
* THIS ALFRESCO NETWORK AGREEMENT ("AGREEMENT") AND THAT YOU HAVE THE
* AUTHORITY TO BIND COMPANY. IF COMPANY DOES NOT AGREE TO ALL OF THE
* TERMS OF THIS AGREEMENT, DO NOT SELECT THE "I UNDERSTAND AND AGREE"
* BOX AND DO NOT INSTALL THE SOFTWARE OR VIEW THE SOURCE CODE. COMPANY
* HAS NOT BECOME A LICENSEE OF, AND IS NOT AUTHORIZED TO USE THE
* SOFTWARE UNLESS AND UNTIL IT HAS AGREED TO BE BOUND BY THESE LICENSE
* TERMS. THE "EFFECTIVE DATE" FOR THIS AGREEMENT SHALL BE THE DAY YOU
* CHECK THE "I UNDERSTAND AND ACCEPT" BOX.
*/
#include "util\ByteArray.h"
#include <memory.h>
using namespace Alfresco;
/**
* Class constructor
*
* @param len BUFLEN
* @param clearMem bool
*/
ByteArray::ByteArray( BUFLEN len, bool clearMem) {
// Allocate a byte array of the specified size
if ( len > 0) {
m_data = new unsigned char[ len];
if ( clearMem)
memset( m_data, 0, len);
}
else
m_data = NULL;
m_length = len;
}
/**
* Class constructor
*
* @param data CBUFPTR
* @param len BUFLEN
*/
ByteArray::ByteArray( CBUFPTR data, BUFLEN len) {
m_data = NULL;
m_length = 0;
setData( data, len);
}
/**
* Class constructor
*
* @param data const char*
* @param len BUFLEN
*/
ByteArray::ByteArray( const char* data, BUFLEN len) {
m_data = NULL;
m_length = 0;
setData(( CBUFPTR) data, len);
}
/**
* Copy constructor
*
* @param byts const ByteArray&
*/
ByteArray::ByteArray( const ByteArray& byts) {
m_data = NULL;
m_length = 0;
setData( byts.getData(), byts.getLength());
}
/**
* Class destructor
*
* @return
*/
ByteArray::~ByteArray() {
if ( m_data != NULL)
delete[] m_data;
}
/**
* Subscript operator
*
* @param idx const unsigned int
* @return unsigned char&
*/
unsigned char& ByteArray::operator [](const unsigned int idx) {
return m_data[ idx];
}
/**
* Assignment operator
*
* @param byts const ByteArray&
* @return ByteArray&
*/
ByteArray& ByteArray::operator = (const ByteArray& byts) {
if ( byts.getLength() > 0)
setData( byts.getData(), byts.getLength());
else
m_length = 0;
return *this;
}
/**
* Assignment operator
*
* @param byts std::string&
* @return ByteArray&
*/
ByteArray& ByteArray::operator = ( std::string& byts) {
if ( byts.length() > 0)
setData(( CBUFPTR) byts.data(), ( BUFLEN) byts.length());
else
m_length = 0;
return *this;
}
/**
* Equality operator
*
* @param byts const ByteArray&
* @return bool
*/
bool ByteArray::operator== ( const ByteArray& byts) {
// Check if the arrays are the same length
if ( getLength() != byts.getLength())
return false;
// Check if the array is empty
if (getLength() == 0)
return true;
// Check if the array bytes are equal
if ( memcmp( getData(), byts.getData(), getLength()) == 0)
return true;
return false;
}
/**
* Set the array length, and optionally clear the memory
*
* @param len BUFLEN
* @param clearMem bool
*/
void ByteArray::setLength( BUFLEN len, bool clearMem) {
// Check if the current block is the correct length
if ( m_length != len) {
// Delete the current array
if ( m_data != NULL)
delete[] m_data;
// Allocate the new array
if ( len > 0)
m_data = new unsigned char[len];
else
m_data = NULL;
m_length = len;
}
// Check if the memory should be cleared
if ( clearMem && m_data != NULL)
memset( m_data, 0, m_length);
}
/**
* Set the data and length
*
* @param data CBUFPTR
* @param len BUFLEN
*/
void ByteArray::setData( CBUFPTR data, BUFLEN len) {
// Delete the existing data
if ( m_data != NULL)
delete[] m_data;
// Allocate a byte array of the specified size
if ( data != NULL && len > 0) {
// Allocate a buffer and copy the data
m_data = new unsigned char[ len];
memcpy( m_data, data, len);
}
else
m_data = NULL;
m_length = len;
}
/**
* Set a byte value
*
* @param idx unsigned int
* @param val unsigned char
*/
void ByteArray::setByte( unsigned int idx, unsigned char val) {
if ( idx < getLength())
m_data[idx] = val;
}

View File

@@ -0,0 +1,732 @@
/*
* Copyright (C) 2005 Alfresco, Inc.
*
* Licensed under the Alfresco Network License. You may obtain a
* copy of the License at
*
* http://www.alfrescosoftware.com/legal/
*
* Please view the license relevant to your network subscription.
*
* BY CLICKING THE "I UNDERSTAND AND ACCEPT" BOX, OR INSTALLING,
* READING OR USING ALFRESCO'S Network SOFTWARE (THE "SOFTWARE"),
* YOU ARE AGREEING ON BEHALF OF THE ENTITY LICENSING THE SOFTWARE
* ("COMPANY") THAT COMPANY WILL BE BOUND BY AND IS BECOMING A PARTY TO
* THIS ALFRESCO NETWORK AGREEMENT ("AGREEMENT") AND THAT YOU HAVE THE
* AUTHORITY TO BIND COMPANY. IF COMPANY DOES NOT AGREE TO ALL OF THE
* TERMS OF THIS AGREEMENT, DO NOT SELECT THE "I UNDERSTAND AND AGREE"
* BOX AND DO NOT INSTALL THE SOFTWARE OR VIEW THE SOURCE CODE. COMPANY
* HAS NOT BECOME A LICENSEE OF, AND IS NOT AUTHORIZED TO USE THE
* SOFTWARE UNLESS AND UNTIL IT HAS AGREED TO BE BOUND BY THESE LICENSE
* TERMS. THE "EFFECTIVE DATE" FOR THIS AGREEMENT SHALL BE THE DAY YOU
* CHECK THE "I UNDERSTAND AND ACCEPT" BOX.
*/
#include "util\DataBuffer.h"
#include "util\Exception.h"
using namespace Alfresco;
using namespace std;
// Use a macro for buffer overflow checks
#define CHECK_BUFFER(sz) {if ((( m_buflen + m_offset) - m_pos) < sz) throw ArrayIndexOutOfBoundsException(__FILE__, __LINE__, L"DataBuffer overflow"); }
#define CHECK_BUFFER_POS(pos,sz) {if ((( m_buflen + m_offset) - pos) < sz) throw ArrayIndexOutOfBoundsException(__FILE__, __LINE__, L"DataBuffer overflow"); }
#define EXTEND_CHECK(sz) {if ((( m_buflen + m_offset) - m_pos) < sz) extendBuffer(); }
#define EXTEND_CHECK_POS(pos,sz) {if ((( m_buflen + m_offset) - pos) < sz) extendBuffer(); }
/**
* Class constructor
*
* @param siz unsigned int
*/
DataBuffer::DataBuffer( unsigned int siz) {
m_buf = new unsigned char[siz];
m_buflen = siz;
m_owner = true;
m_pos = 0;
m_endpos = 0;
m_offset = 0;
}
/**
* Class constructor
*
* @param buf BUFPTR
* @param off BUFPOS
* @param len BUFLEN
*/
DataBuffer::DataBuffer( BUFPTR buf, BUFPOS off, BUFLEN len) {
m_buf = buf;
m_buflen = len;
m_owner = false;
m_pos = off;
m_offset = off;
m_endpos = off + len;
}
/**
* Class destructor
*/
DataBuffer::~DataBuffer() {
// Delete the buffer, if owned by this object
if ( m_owner == true && m_buf != NULL)
delete[] m_buf;
}
/**
* Return the buffer length
*
* @return BUFLEN
*/
BUFLEN DataBuffer::getLength( void) const {
if ( m_endpos != 0)
return m_endpos - m_offset;
return m_pos - m_offset;
}
/**
* Return the length in words
*
* @return unsigned int
*/
unsigned int DataBuffer::getLengthInWords( void) const {
return getLength() / 2;
}
/**
* Return the available buffer length
*
* @return BUFLEN
*/
BUFLEN DataBuffer::getAvailableLength( void) const {
if ( m_endpos == 0)
return 0;
return m_endpos - m_pos;
}
/**
* Get a byte from the buffer and advance the buffer pointer
*
* @return unsigned char
*/
unsigned char DataBuffer::getByte( void) {
// Check if there is enough space in the buffer for the data
CHECK_BUFFER(1);
// Return the data
return (unsigned int) m_buf[m_pos++];
}
/**
* Get a short/16bit value from the buffer and advance the buffer pointer
*
* @return unsigned int
*/
unsigned int DataBuffer::getShort( void) {
// Check if there is enough space in the buffer for the data
CHECK_BUFFER(2);
// Get a short value from the buffer
unsigned int sval = DataPacker::getIntelShort( m_buf, m_pos);
m_pos += 2;
return sval;
}
/**
* Get an integer from the buffer and advance the buffer pointer
*
* @return unsigned int
*/
unsigned int DataBuffer::getInt( void) {
// Check if there is enough space in the buffer for the data
CHECK_BUFFER(4);
// Get a short value from the buffer
unsigned int ival = DataPacker::getIntelInt( m_buf, m_pos);
m_pos += 4;
return ival;
}
/**
* Get a long from the buffer and advance the buffer pointer
*
* @return LONG64
*/
LONG64 DataBuffer::getLong( void) {
// Check if there is enough space in the buffer for the data
CHECK_BUFFER(8);
// Get a long value from the buffer
LONG64 lval = DataPacker::getIntelLong( m_buf, m_pos);
m_pos += 8;
return lval;
}
/**
* Get a string from the buffer and advance the buffer pointer
*
* @param uni bool
* @return String
*/
String DataBuffer::getString( bool uni) {
return getString( 255, uni);
}
/**
* Get a string from the buffer and advance the buffer pointer
*
* @param maxlen unsigned int
* @param uni bool
* @return String
*/
String DataBuffer::getString( unsigned int maxlen, bool uni) {
// Check for Unicode or ASCII
String ret;
unsigned int availLen = 0;
if ( uni) {
// Word align the current buffer position, calculate the available length
m_pos = DataPacker::wordAlign(m_pos);
availLen = (m_endpos - m_pos) / 2;
if ( availLen < maxlen)
maxlen = availLen;
ret = DataPacker::getUnicodeString(m_buf, m_pos, maxlen);
if ( ret.length() < maxlen)
m_pos += (ret.length() * 2) + 2;
else
m_pos += maxlen * 2;
}
else {
// Calculate the available length
availLen = m_endpos - m_pos;
if ( availLen < maxlen)
maxlen = availLen;
// Unpack the ASCII string
ret = DataPacker::getString(m_buf, m_pos, maxlen);
if ( ret.length() < maxlen)
m_pos += ret.length() + 1;
else
m_pos += maxlen;
}
// Return the string
return ret;
}
/**
* Get a short value at the specified buffer position
*
* @param idx unsigned int
* @return unsigned int
*/
unsigned int DataBuffer::getShortAt( unsigned int idx) {
// Check if there is enough data in the buffer
BUFPOS pos = m_offset + (idx * 2);
CHECK_BUFFER_POS(pos, 2);
// Unpack the short value
return DataPacker::getIntelShort(m_buf, pos);
}
/**
* Get an integer value at the specified buffer position
*
* @param idx unsigned int
* @return unsigned int
*/
unsigned int DataBuffer::getIntAt( unsigned int idx) {
// Check if there is enough data in the buffer
BUFPOS pos = m_offset + (idx * 4);
CHECK_BUFFER_POS(pos, 4);
// Unpack the integer value
return DataPacker::getIntelInt(m_buf, pos);
}
/**
* Get a long value at the specified buffer position
*
* @param idx unsigned int
* @return LONG64
*/
LONG64 DataBuffer::getLongAt( unsigned int idx) {
// Check if there is enough data in the buffer
BUFPOS pos = m_offset + (idx * 8);
CHECK_BUFFER_POS(pos, 8);
// Unpack the long value
return DataPacker::getIntelLong(m_buf, pos);
}
/**
* Append a byte to the buffer and advance the buffer pointer
*
* @param byt unsigned char
*/
void DataBuffer::putByte( unsigned char byt) {
// Check if the buffer needs extending
EXTEND_CHECK(1);
// Pack the data, update the buffer pointer
m_buf[m_pos++] = byt;
}
/**
* Append a short to the buffer and advance the buffer pointer
*
* @param sval unsigned int
*/
void DataBuffer::putShort( unsigned int sval) {
// Check if the buffer needs extending
EXTEND_CHECK(2);
// Pack the data, update the buffer pointer
DataPacker::putIntelShort( sval, m_buf, m_pos);
m_pos += 2;
}
/**
* Append an integer to the buffer and advance the buffer pointer
*
* @param ival unsigned int
*/
void DataBuffer::putInt( unsigned int ival) {
// Check if the buffer needs extending
EXTEND_CHECK(4);
// Pack the data, update the buffer pointer
DataPacker::putIntelInt( ival, m_buf, m_pos);
m_pos += 4;
}
/**
* Append a long to the buffer and advance the buffer pointer
*
* @param lval LONG64
*/
void DataBuffer::putLong( LONG64 lval) {
// Check if the buffer needs extending
EXTEND_CHECK(8);
// Pack the data, update the buffer pointer
DataPacker::putIntelLong( lval, m_buf, m_pos);
m_pos += 8;
}
/**
* Put a short value into the buffer at the specified position
*
* @param idx unsigned int
* @param sval unsigned int
*/
void DataBuffer::putShortAt( unsigned int idx, unsigned int sval) {
// Check if there is enough space in the buffer
BUFPOS pos = m_offset + (idx * 2);
EXTEND_CHECK_POS(pos,2);
// Pack the short value
DataPacker::putIntelShort(sval, m_buf, pos);
}
/**
* Put an integer value into the buffer at the specified position
*
* @param idx unsigned int
* @param ival unsigned int
*/
void DataBuffer::putIntAt( unsigned int idx, unsigned int ival) {
// Check if there is enough space in the buffer
BUFPOS pos = m_offset + (idx * 4);
EXTEND_CHECK_POS(pos,4);
// Pack the integer value
DataPacker::putIntelInt(ival, m_buf, pos);
}
/**
* Put a long value into the buffer at the specified position
*
* @param idx unsigned int
* @param lval LONG64
*/
void DataBuffer::putLongAt( unsigned int idx, LONG64 lval) {
// Check if there is enough space in the buffer
BUFPOS pos = m_offset + (idx * 8);
EXTEND_CHECK_POS(pos,8);
// Pack the long value
DataPacker::putIntelLong(lval, m_buf, pos);
}
/**
* Append a string to the buffer and advance the buffer pointer
*
* @param str const String&
* @param uni bool
* @param nulTerm bool
*/
void DataBuffer::putString( const String& str, bool uni, bool nulTerm) {
// Check for Unicode or ASCII
if ( uni) {
// Check if there is enough space in the buffer
unsigned int bytLen = str.length() * 2;
if ( m_buflen - m_pos < bytLen)
extendBuffer(bytLen + 4);
// Word align the buffer position, pack the Unicode string
m_pos = DataPacker::wordAlign(m_pos);
DataPacker::putString(str, m_buf, m_pos, nulTerm, true);
m_pos += (str.length() * 2);
if ( nulTerm)
m_pos += 2;
}
else {
// Check if there is enough space in the buffer
if ( m_buflen - m_pos < str.length())
extendBuffer(str.length() + 2);
// Pack the ASCII string
DataPacker::putString(str, m_buf, m_pos, nulTerm);
m_pos += str.length();
if ( nulTerm)
m_pos++;
}
}
/**
* Append a fixed length string to the buffer and advance the buffer pointer
*
* @param str const String&
* @param len unsigned int
*/
void DataBuffer::putFixedString( const String& str, unsigned int len) {
// Check if there is enough space in the buffer
if ( m_buflen - m_pos < str.length())
extendBuffer(str.length() + 2);
// Pack the ASCII string
DataPacker::putString(str, m_buf, m_pos);
m_pos += len;
// Pad the string to the required length
while ( len > str.length()) {
m_buf[m_pos++] = 0;
len--;
}
}
/**
* Put a string into the buffer at the specified position
*
* @param str const String&
* @param pos BUFPOS
* @param uni bool
* @param nulTerm bool
* @return BUFPOS
*/
BUFPOS DataBuffer::putStringAt( const String& str, BUFPOS pos, bool uni, bool nulTerm) {
// Check for Unicode or ASCII
BUFPOS retPos = 0;
if ( uni) {
// Check if there is enough space in the buffer
unsigned int bytLen = str.length() * 2;
if ( m_buflen - pos < bytLen)
extendBuffer(bytLen + 4);
// Word align the buffer position, pack the Unicode string
pos = DataPacker::wordAlign(pos);
retPos = DataPacker::putString(str, m_buf, pos, nulTerm);
}
else {
// Check if there is enough space in the buffer
if ( m_buflen - pos < str.length())
extendBuffer(str.length() + 2);
// Pack the ASCII string
retPos = DataPacker::putString(str, m_buf, pos, nulTerm);
}
// Return the end of string buffer position
return retPos;
}
/**
* Put a fixed length string into the buffer at the specified position
*
* @param str const String&
* @param len unsigned int
* @param pos BUFPOS
* @return BUFPOS
*/
BUFPOS DataBuffer::putFixedStringAt( const String& str, unsigned int len, BUFPOS pos) {
// Check if there is enough space in the buffer
if ( m_buflen - pos < str.length())
extendBuffer(str.length() + 2);
// Pack the ASCII string
pos = DataPacker::putString(str, m_buf, pos);
// Pad the string
while ( len > str.length()) {
m_buf[pos++] = 0;
len--;
}
// Return the end of string buffer position
return pos;
}
/**
* Put a string pointer into the buffer
*
* @param off unsigned int
*/
void DataBuffer::putStringPointer( unsigned int off) {
// Calculate the offset from the start of the data buffer to the string position
DataPacker::putIntelInt(off - m_offset, m_buf, m_pos);
m_pos += 4;
}
/**
* Append a block of nulls to the buffer and advance the buffer pointer
*
* @param cnt unsigned int
*/
void DataBuffer::putZeros( unsigned int cnt) {
// Check if there is enough space in the buffer
if ( m_buflen - m_pos < cnt)
extendBuffer(cnt);
// Pack the zero bytes
for ( unsigned int i = 0; i < cnt; i++)
m_buf[m_pos++] = 0;
}
/**
* Word align the buffer pointer
*
*/
void DataBuffer::wordAlign( void) {
m_pos = DataPacker::wordAlign(m_pos);
}
/**
* Longword align the buffer pointer
*
*/
void DataBuffer::longwordAlign( void) {
m_pos = DataPacker::longwordAlign(m_pos);
}
/**
* Append a block of byte data to the buffer and advance the buffer pointer
*
* @param buf BUFPTR
* @param off BUFPOS
* @param len BUFLEN
*/
void DataBuffer::appendData( BUFPTR buf, BUFPOS off, BUFLEN len) {
// Check if there is enough space in the buffer
if ( m_buflen - m_pos < len)
extendBuffer(len);
// Copy the data to the buffer and update the current write position
memcpy( m_buf + m_pos, buf + off, len);
m_pos += len;
}
/**
* Copy data to the user buffer and advance the buffer pointer
*
* @param buf BUFPTR
* @param pos BUFPOS
* @param cnt unsigned int
*/
unsigned int DataBuffer::copyData( BUFPTR buf, BUFPOS pos, unsigned int cnt) {
// Check if there is any more data to copy
if ( m_pos == m_endpos)
return 0;
// Calculate the amount of data to copy
unsigned int siz = m_endpos - m_pos;
if ( siz > cnt)
siz = cnt;
// Copy the data to the user buffer and update the current read position
memcpy( buf + pos, m_buf + m_pos, siz);
m_pos += siz;
// Return the amount of data copied
return siz;
}
/**
* Advance the buffer pointer by the specified amount
*
* @param len unsigned int
*/
void DataBuffer::skipBytes( unsigned int len) {
// Check if there is enough data in the buffer
CHECK_BUFFER(len);
// Skip bytes
m_pos += len;
}
/**
* Set the end of buffer position
*/
void DataBuffer::setEndOfBuffer( void) {
m_endpos = m_pos;
m_pos = m_offset;
}
/**
* Set the buffer length
*
* @param len BUFLEN
*/
void DataBuffer::setLength( BUFLEN len) {
m_pos = m_offset + len;
}
/**
* Extend the buffer by the specified amount by reallocating the buffer and copying the existing
* data to the new buffer
*
* @param ext BUFLEN
*/
void DataBuffer::extendBuffer( BUFLEN ext) {
// Create a new buffer of the required size
BUFLEN newlen = m_buflen + ext;
BUFPTR newBuf = new unsigned char[newlen];
// Copy the data from the current buffer to the new buffer
memcpy( newBuf, m_buf, m_buflen);
// Check if the previous buffer was owned by this object
if ( m_owner)
delete[] m_buf;
// Set the new buffer to be the main buffer
m_buf = newBuf;
m_buflen = newlen;
m_owner = true;
}
/**
* Extend the buffer doubling the current size by reallocating the buffer and copying the existing
* data to the new buffer
*
*/
void DataBuffer::extendBuffer( void) {
extendBuffer( m_buflen * 2);
}

View File

@@ -0,0 +1,436 @@
/*
* Copyright (C) 2005 Alfresco, Inc.
*
* Licensed under the Alfresco Network License. You may obtain a
* copy of the License at
*
* http://www.alfrescosoftware.com/legal/
*
* Please view the license relevant to your network subscription.
*
* BY CLICKING THE "I UNDERSTAND AND ACCEPT" BOX, OR INSTALLING,
* READING OR USING ALFRESCO'S Network SOFTWARE (THE "SOFTWARE"),
* YOU ARE AGREEING ON BEHALF OF THE ENTITY LICENSING THE SOFTWARE
* ("COMPANY") THAT COMPANY WILL BE BOUND BY AND IS BECOMING A PARTY TO
* THIS ALFRESCO NETWORK AGREEMENT ("AGREEMENT") AND THAT YOU HAVE THE
* AUTHORITY TO BIND COMPANY. IF COMPANY DOES NOT AGREE TO ALL OF THE
* TERMS OF THIS AGREEMENT, DO NOT SELECT THE "I UNDERSTAND AND AGREE"
* BOX AND DO NOT INSTALL THE SOFTWARE OR VIEW THE SOURCE CODE. COMPANY
* HAS NOT BECOME A LICENSEE OF, AND IS NOT AUTHORIZED TO USE THE
* SOFTWARE UNLESS AND UNTIL IT HAS AGREED TO BE BOUND BY THESE LICENSE
* TERMS. THE "EFFECTIVE DATE" FOR THIS AGREEMENT SHALL BE THE DAY YOU
* CHECK THE "I UNDERSTAND AND ACCEPT" BOX.
*/
#include <string>
#include "util\DataPacker.h"
#include "util\ByteArray.h"
using namespace Alfresco;
/**
* Unpack a short/16 bit value from the buffer.
*
* @param buf CBUFPTR
* @param pos BUFPOS
* @return int
*/
int DataPacker::getShort(CBUFPTR buf, BUFPOS pos) {
int sval = ( buf[pos] << 8) + buf[pos+1];
return sval;
}
/**
* Unpack an int/32 bit value from the buffer.
*
* @param buf CBUFPTR
* @param pos BUFPOS
* @return int
*/
int DataPacker::getInt(CBUFPTR buf, BUFPOS pos) {
int ival = (buf[pos] << 24) + (buf[pos+1] << 16) + (buf[pos+2] << 8) + buf[pos+3];
return ival;
}
/**
* Unpack a long/64 bit value from the buffer.
*
* @param buf CBUFPTR
* @param pos BUFPOS
* @return LONG64
*/
LONG64 DataPacker::getLong(CBUFPTR buf, BUFPOS pos) {
LONG64 lval = 0;
BUFPTR pLval = (BUFPTR) &lval;
for ( unsigned int i = 0; i < 8; i++) {
pLval[7 - i] = buf[pos + i];
}
return lval;
}
/**
* Unpack a short/16 bit value in Intel format from the buffer.
*
* @param buf CBUFPTR
* @param pos BUFPOS
* @return int
*/
int DataPacker::getIntelShort(CBUFPTR buf, BUFPOS pos) {
int sval = ( buf[pos+1] << 8) + buf[pos];
return sval;
}
/**
* Unpack an int/32 bit value in Intel format from the buffer.
*
* @param buf CBUFPTR
* @param pos BUFPOS
* @return int
*/
int DataPacker::getIntelInt(CBUFPTR buf, BUFPOS pos) {
int ival = (buf[pos+3] << 24) + (buf[pos+2] << 16) + (buf[pos+1] << 8) + buf[pos];
return ival;
}
/**
* Unpack a long/64 bit value in Intel format from the buffer.
*
* @param buf CBUFPTR
* @param pos BUFPOS
* @return LONG64
*/
LONG64 DataPacker::getIntelLong(CBUFPTR buf, BUFPOS pos) {
LONG64 lval = 0;
BUFPTR pLval = (BUFPTR) &lval;
for ( unsigned int i = 0; i < 8; i++) {
pLval[i] = buf[pos + i];
}
return lval;
}
/**
* Unpack a string from the buffer.
*
* @param buf CBUFPTR
* @param pos BUFPOS
* @param maxLen const unsigned int
* @param isUni const bool
* @return String
*/
String DataPacker::getString(CBUFPTR buf, BUFPOS pos, const unsigned int maxLen, const bool isUni) {
// Check for a Unicode string
if ( isUni)
return getUnicodeString( buf, pos, maxLen);
// Search for the trailing null
unsigned int maxpos = pos + maxLen;
unsigned int endpos = pos;
while (buf[endpos] != '\0' && endpos < maxpos)
endpos++;
return String((const char*) buf, pos, endpos - pos);
}
/**
* Unpack a Unicode string from the buffer.
*
* @param buf CBUFPTR
* @param pos BUFPOS
* @param maxLen const unsigned int
* @return String
*/
String DataPacker::getUnicodeString(CBUFPTR buf, BUFPOS pos, const unsigned int maxLen) {
// Check for an empty string
if ( maxLen == 0)
return String();
// Word align the position
pos = wordAlign( pos);
// Search for the trailing null
int maxpos = pos + (maxLen * 2);
int endpos = pos;
std::wstring str;
int cpos = 0;
wchar_t curChar;
do {
// Get a Unicode character from the buffer
curChar = (wchar_t) DataPacker::getIntelShort(buf, endpos);
// Add the character to the string
if ( curChar != 0)
str += curChar;
// Update the buffer pointer
endpos += 2;
} while (curChar != 0 && endpos < maxpos);
// Return the string
return String(str);
}
/**
* Pack a short/16 bit value into the buffer.
*
* @param val const int
* @param buf BUFPTR
* @param pos BUFPOS
*/
void DataPacker::putShort(const int val, BUFPTR buf, BUFPOS pos) {
buf[pos] = (unsigned char) (val >> 8) & 0xFF;
buf[pos+1] = (unsigned char) (val & 0xFF);
}
/**
* Pack an int/32 bit value into the buffer.
*
* @param val const int
* @param buf BUFPTR
* @param pos BUFPOS
*/
void DataPacker::putInt(const int val, BUFPTR buf, BUFPOS pos) {
buf[pos] = (unsigned char) (val >> 24) & 0xFF;
buf[pos+1] = (unsigned char) (val >> 16) & 0xFF;
buf[pos+2] = (unsigned char) (val >> 8) & 0xFF;
buf[pos+3] = (unsigned char) (val & 0xFF);
}
/**
* Pack a long/64 bit value into the buffer.
*
* @param val const LONG64
* @param buf BUFPTR
* @param pos BUFPOS
*/
void DataPacker::putLong(const LONG64 val, BUFPTR buf, BUFPOS pos) {
BUFPTR pLval = (BUFPTR) &val;
buf[pos] = pLval[7];
buf[pos+1] = pLval[6];
buf[pos+2] = pLval[5];
buf[pos+3] = pLval[4];
buf[pos+4] = pLval[3];
buf[pos+5] = pLval[2];
buf[pos+6] = pLval[1];
buf[pos+7] = pLval[0];
}
/**
* Pack a short/16 bit value in Intel format into the buffer.
*
* @param val const int
* @param buf BUFPTR
* @param pos BUFPOS
*/
void DataPacker::putIntelShort(const int val, BUFPTR buf, BUFPOS pos) {
buf[pos+1] = (unsigned char) (val >> 8) & 0xFF;
buf[pos] = (unsigned char) (val & 0xFF);
}
/**
* Pack an int/32 bit value in Intel format into the buffer.
*
* @param val const int
* @param buf BUFPTR
* @param pos BUFPOS
*/
void DataPacker::putIntelInt(const int val, BUFPTR buf, BUFPOS pos) {
buf[pos+3] = (unsigned char) (val >> 24) & 0xFF;
buf[pos+2] = (unsigned char) (val >> 16) & 0xFF;
buf[pos+1] = (unsigned char) (val >> 8) & 0xFF;
buf[pos] = (unsigned char) (val & 0xFF);
}
/**
* Pack a long/64 bit value in Intel format into the buffer.
*
* @param val const LONG64
* @param buf BUFPTR
* @param pos BUFPOS
*/
void DataPacker::putIntelLong(const LONG64 val, BUFPTR buf, BUFPOS pos) {
BUFPTR pLval = (BUFPTR) &val;
buf[pos+7] = pLval[7];
buf[pos+6] = pLval[6];
buf[pos+5] = pLval[5];
buf[pos+4] = pLval[4];
buf[pos+3] = pLval[3];
buf[pos+2] = pLval[2];
buf[pos+1] = pLval[1];
buf[pos] = pLval[0];
}
/**
* Pack a string into the buffer.
*
* @param str const String&
* @param buf BUFPTR
* @param pos BUFPOS
* @param nullTerm const bool
* @param isUni const bool
* @return int
*/
unsigned int DataPacker::putString(const String& str, BUFPTR buf, BUFPOS pos, const bool nullTerm, const bool isUni) {
// Check if the string should be packed as Unicode or ASCII
unsigned int newPos = pos;
if ( isUni == true) {
// Pack the characters
for ( unsigned int i = 0; i < str.length(); i++) {
wchar_t ch = str.charAt(i);
buf[newPos++] = (unsigned char) (ch & 0xFF);
buf[newPos++] = (unsigned char) (ch >> 8) & 0xFF;
}
// Add a null terminator, if required
if ( nullTerm == true) {
buf[newPos++] = '\0';
buf[newPos++] = '\0';
}
}
else {
// Get the string as ASCII characters
ByteArray byts = str.getBytes();
// Pack the characters
for ( unsigned int i = 0; i < str.length(); i++)
buf[newPos++] = byts[i];
// Add a null terminator, if required
if ( nullTerm == true)
buf[newPos++] = '\0';
}
// Return the new buffer position
return newPos;
}
/**
* Pack an ASCII string into the buffer
*
* @param str const char*
* @param buf BUFPTR
* @param pos BUFPOS
* @param nullTerm bool
* @return unsigned int
*/
unsigned int DataPacker::putString(const char* str, BUFLEN len, BUFPTR buf, BUFPOS pos, bool nullTerm) {
// Copy the ASCII string to the buffer
memcpy(buf + pos, str, len);
BUFPOS endPos = pos + len;
if ( nullTerm == true)
buf[endPos] = '\0';
// Return the new buffer position
return endPos;
}
/**
* Pack a Unicode string into the buffer
*
* @param str const wchar_t*
* @param buf BUFPTR
* @param pos BUFPOS
* @param nullTerm bool
* @return unsigned int
*/
unsigned int DataPacker::putString(const wchar_t* str, BUFLEN len, BUFPTR buf, BUFPOS pos, bool nullTerm) {
// Copy the Unicode string to the buffer
BUFLEN uniLen = len * 2;
BUFPOS endPos = pos + uniLen;
memcpy(buf + pos, str, uniLen);
if ( nullTerm == true) {
buf[pos + uniLen + 1] = '\0';
buf[pos + uniLen + 2] = '\0';
endPos += 2;
}
// Return the new buffer position
return endPos;
}
/**
* Pack a number of zero bytes into the buffer.
*
* @param buf BUFPTR
* @param pos BUFPOS
* @param count const unsigned int
*/
void DataPacker::putZeros(BUFPTR buf, BUFPOS pos, const unsigned int count) {
for (unsigned int i = 0; i < count; i++)
buf[pos + i] = (unsigned char) 0;
}
/**
* Determine the amount of buffer space required to pack the string with the specified settings.
*
* @param str const String&
* @param isUni const bool
* @param nulTerm const bool
* @return unsigned int
*/
unsigned int DataPacker::getStringLength(const String& str, const bool isUni, const bool nulTerm) {
int len = str.length();
if ( nulTerm == true)
len += 1;
if ( isUni == true)
len *= 2;
return len;
}
/**
* Calculate the buffer position after packing the string with the specified settings.
*
* @param pos BUFPOS
* @param str const String&
* @param isUni const bool
* @param nulTerm const bool
* @return unsigned int
*/
unsigned int DataPacker::getBufferPosition(BUFPOS pos, const String& str, const bool isUni, const bool nulTerm) {
unsigned int len = str.length();
if ( nulTerm == true)
len += 1;
if ( isUni == true)
len *= 2;
return pos + len;
}

View File

@@ -0,0 +1,137 @@
/*
* Copyright (C) 2005 Alfresco, Inc.
*
* Licensed under the Alfresco Network License. You may obtain a
* copy of the License at
*
* http://www.alfrescosoftware.com/legal/
*
* Please view the license relevant to your network subscription.
*
* BY CLICKING THE "I UNDERSTAND AND ACCEPT" BOX, OR INSTALLING,
* READING OR USING ALFRESCO'S Network SOFTWARE (THE "SOFTWARE"),
* YOU ARE AGREEING ON BEHALF OF THE ENTITY LICENSING THE SOFTWARE
* ("COMPANY") THAT COMPANY WILL BE BOUND BY AND IS BECOMING A PARTY TO
* THIS ALFRESCO NETWORK AGREEMENT ("AGREEMENT") AND THAT YOU HAVE THE
* AUTHORITY TO BIND COMPANY. IF COMPANY DOES NOT AGREE TO ALL OF THE
* TERMS OF THIS AGREEMENT, DO NOT SELECT THE "I UNDERSTAND AND AGREE"
* BOX AND DO NOT INSTALL THE SOFTWARE OR VIEW THE SOURCE CODE. COMPANY
* HAS NOT BECOME A LICENSEE OF, AND IS NOT AUTHORIZED TO USE THE
* SOFTWARE UNLESS AND UNTIL IT HAS AGREED TO BE BOUND BY THESE LICENSE
* TERMS. THE "EFFECTIVE DATE" FOR THIS AGREEMENT SHALL BE THE DAY YOU
* CHECK THE "I UNDERSTAND AND ACCEPT" BOX.
*/
#include "util\Exception.h"
using namespace Alfresco;
using namespace std;
// Define standard Java-like exceptions
EXCEPTION_CLASS(Alfresco, IOException);
EXCEPTION_CLASS(Alfresco, NullPointerException);
EXCEPTION_CLASS(Alfresco, ArrayIndexOutOfBoundsException);
EXCEPTION_CLASS(Alfresco, NumberFormatException);
/**
* Class constructor
*
* @param moduleName const char*
* @param lineNum unsigned int
* @param msg const wchar_t*
* @param msg2 const wchar_t*
* @param msg3 const wchar_t*
* @param msg4 const wchar_t*
* @param msg5 const wchar_t*
*/
Exception::Exception( const char* moduleName, unsigned int lineNum, const wchar_t* msg, const wchar_t* msg2,
const wchar_t* msg3, const wchar_t* msg4, const wchar_t* msg5) {
// Prefix the message string with the module name and line number
m_msg = moduleName;
if ( lineNum != 0) {
m_msg += " (";
m_msg += lineNum;
m_msg += ")";
}
m_msg += ": ";
// Add the messages parts
if ( msg)
m_msg += msg;
if ( msg2) {
m_msg += " ";
m_msg += msg2;
}
if ( msg3) {
m_msg += " ";
m_msg += msg3;
}
if ( msg4) {
m_msg += " ";
m_msg += msg4;
}
if ( msg5) {
m_msg += " ";
m_msg += msg5;
}
}
/**
* Class constructor
*
* @param msg const wchar_t*
* @param msg2 const wchar_t*
* @param msg3 const wchar_t*
* @param msg4 const wchar_t*
* @param msg5 const wchar_t*
*/
Exception::Exception( const wchar_t* msg, const wchar_t* msg2, const wchar_t* msg3, const wchar_t* msg4, const wchar_t* msg5) {
if ( msg)
m_msg = msg;
if ( msg2) {
m_msg += " ";
m_msg += msg2;
}
if ( msg3) {
m_msg += " ";
m_msg += msg3;
}
if ( msg4) {
m_msg += " ";
m_msg += msg4;
}
if ( msg5) {
m_msg += " ";
m_msg += msg5;
}
}
/**
* Copy constructor
*
* @param ex const Exception&
*/
Exception::Exception( const Exception& ex) {
m_msg = ex.getMessage();
}
/**
* Class destructor
*
*/
Exception::~Exception() {
}

View File

@@ -0,0 +1,390 @@
/*
* Copyright (C) 2005 Alfresco, Inc.
*
* Licensed under the Alfresco Network License. You may obtain a
* copy of the License at
*
* http://www.alfrescosoftware.com/legal/
*
* Please view the license relevant to your network subscription.
*
* BY CLICKING THE "I UNDERSTAND AND ACCEPT" BOX, OR INSTALLING,
* READING OR USING ALFRESCO'S Network SOFTWARE (THE "SOFTWARE"),
* YOU ARE AGREEING ON BEHALF OF THE ENTITY LICENSING THE SOFTWARE
* ("COMPANY") THAT COMPANY WILL BE BOUND BY AND IS BECOMING A PARTY TO
* THIS ALFRESCO NETWORK AGREEMENT ("AGREEMENT") AND THAT YOU HAVE THE
* AUTHORITY TO BIND COMPANY. IF COMPANY DOES NOT AGREE TO ALL OF THE
* TERMS OF THIS AGREEMENT, DO NOT SELECT THE "I UNDERSTAND AND AGREE"
* BOX AND DO NOT INSTALL THE SOFTWARE OR VIEW THE SOURCE CODE. COMPANY
* HAS NOT BECOME A LICENSEE OF, AND IS NOT AUTHORIZED TO USE THE
* SOFTWARE UNLESS AND UNTIL IT HAS AGREED TO BE BOUND BY THESE LICENSE
* TERMS. THE "EFFECTIVE DATE" FOR THIS AGREEMENT SHALL BE THE DAY YOU
* CHECK THE "I UNDERSTAND AND ACCEPT" BOX.
*/
#include "util\FileName.h"
using namespace Alfresco;
using namespace std;
// Declare the Dos separator and NTFS stream separator strings
String& Alfresco::FileName::DosSeperator = String("\\");
String& Alfresco::FileName::NTFSStreamSeperator = String(":");
wchar_t Alfresco::FileName::DOS_SEPERATOR = L'\\';
/**
* Build a path using the specified components
*
* @param dev const String&
* @param path const String&
* @param fileName const String&
* @param sep wchar_t
* @return const String
*/
const String FileName::buildPath( const String& dev, const String& path, const String& fileName, wchar_t sep) {
// Build the path string
String fullPath;
// Check for a device name
if ( dev.isNotEmpty()) {
// Add the device name
fullPath.append( dev);
// Check if the device name has a file separator
if ( dev.length() > 0 && dev.charAt( dev.length() - 1) != sep)
fullPath.append( sep);
}
// Check for a path
if ( path.isNotEmpty()) {
// Add the path
if (fullPath.length() > 0
&& (path.charAt(0) == sep || path.charAt(0) == DOS_SEPERATOR))
fullPath.append( path.substring(1));
else
fullPath.append( path);
// Add a trailing separator, if required
if (path.length() > 0
&& path.charAt(path.length() - 1) != sep
&& fileName.isNotEmpty())
fullPath.append(sep);
}
// Check for a file name
if (fileName.isNotEmpty()) {
// Add the file name
if ( fullPath.length() > 0 && ( fileName.charAt(0) == sep || fileName.charAt(0) == DOS_SEPERATOR))
fullPath.append( fileName.substring(1));
else
fullPath.append( fileName);
}
// Debug
// Debug.println ( "BuildPath: " + fullPath.toString ());
// Convert the file separator characters in the path if we are not using the normal
// DOS file separator character.
if (sep != DOS_SEPERATOR)
return convertSeperators( fullPath, sep);
return fullPath;
}
/**
* Check if a file name contains a stream name
*
* @param fileName const String&
* @return bool
*/
bool FileName::containsStreamName( const String& fileName) {
// Check if the path contains the stream name separator character
if ( fileName.indexOf( NTFSStreamSeperator) != -1)
return true;
return false;
}
/**
* Convert path separator characters
*
* @param path const String&
* @param sep wchar_t
* @return const String
*/
const String FileName::convertSeperators( const String& path, wchar_t sep) {
// Check if the path contains any DOS separators
if ( path.indexOf( DOS_SEPERATOR) == -1)
return path;
// Convert DOS path separators to the specified separator
String newPath;
unsigned int idx = 0;
while ( idx < path.length()) {
// Get the current character from the path and check if it is a DOS path
// separator character.
wchar_t ch = path.charAt(idx++);
if (ch == DOS_SEPERATOR)
newPath.append(sep);
else
newPath.append(ch);
}
// Return the new path string
return newPath;
}
/**
* Make a relative path
*
* @param basePath const String&
* @param fullPath const String&
* @return const String
*/
const String FileName::makeRelativePath( const String& basePath, const String& fullPath) {
// Check if the base path is the root path
if ( basePath.length() == 0 || basePath.equals( DosSeperator)) {
// Return the full path, strip any leading separator
if ( fullPath.length() > 0 && fullPath.charAt(0) == DOS_SEPERATOR)
return fullPath.substring(1);
return fullPath;
}
// Split the base and full paths into separate components
StringList baseNames = splitAllPaths(basePath);
StringList fullNames = splitAllPaths(fullPath);
// Check that the full path is actually within the base path tree
if ( baseNames.numberOfStrings() > 0 && fullNames.numberOfStrings() > 0 &&
baseNames.getStringAt(0).equalsIgnoreCase(fullNames.getStringAt(0)) == false)
return String();
// Match the path names
unsigned int idx = 0;
while ( idx < baseNames.numberOfStrings() && idx < fullNames.numberOfStrings() &&
baseNames.getStringAt(idx).equalsIgnoreCase(fullNames.getStringAt(idx)))
idx++;
// Build the relative path
String relPath(128);
while ( idx < fullNames.numberOfStrings()) {
relPath.append(fullNames.getStringAt(idx++));
if ( idx < fullNames.numberOfStrings())
relPath.append(DOS_SEPERATOR);
}
// Return the relative path
return relPath;
}
/**
* Map an input path to a real path
*
* @param base const String&
* @param path const String&
* @return const String
*/
const String FileName::mapPath(const String& base, const String& path) {
return String();
}
/**
* Normalize a path converting all directories to uppercase and keeping the file name as is
*
* @param path const String&
* @return const String
*/
const String FileName::normalizePath(const String& path) {
// Split the path into directories and file name, only uppercase the directories to normalize
// the path.
String normPath = path;
if ( path.length() > 3) {
// Split the path to separate the folders/file name
int pos = path.lastIndexOf( DOS_SEPERATOR);
if ( pos != -1) {
// Get the path and file name parts, normalize the path
String pathPart = path.substring(0, pos).toUpperCase();
String namePart = path.substring(pos);
// Rebuild the path string
normPath = pathPart;
normPath += namePart;
}
}
// Return the normalized path
return normPath;
}
/**
* Remove the file name from the path
*
* @param path const String&
* @return const String
*/
const String FileName::removeFileName(const String& path) {
// Find the last path separator
int pos = path.lastIndexOf(DOS_SEPERATOR);
if (pos != -1)
return path.substring(0, pos);
// Return an empty string, no path separators
return "";
}
/**
* Split the path into all the component directories and filename
*
* @param path const String&
* @return StringList
*/
StringList FileName::splitAllPaths(const String& path) {
// Check if the path is valid
StringList paths;
if ( path.length() == 0) {
paths.addString( path);
return paths;
}
// Split the path
return path.tokenize( DosSeperator);
}
/**
* Split the path into separate directory path and file name strings
*
* @param path const String&
* @param sep wchar_t
* @return StringList
*/
StringList FileName::splitPath( const String& path, wchar_t sep) {
// Create an array of strings to hold the path and file name strings
StringList pathList;
String path0, path1;
// Check if the path is valid
if ( path.length() > 0) {
// Check if the path has a trailing separator, if so then there is no
// file name.
int pos = path.lastIndexOf(sep);
if (pos == -1 || pos == (path.length() - 1)) {
// Set the path string in the returned string array
path0 = path;
}
else {
// Split the path into directory list and file name strings
path1 = path.substring(pos + 1);
if (pos == 0)
path0 = path.substring(0, pos + 1);
else
path0 = path.substring(0, pos);
}
}
// Set the path strings
pathList.addString( path0);
pathList.addString( path1);
// Return the path strings
return pathList;
}
/**
* Split a path string into directory path, file name and stream name components
*
* @param path const String&
* @return StringList
*/
StringList FileName::splitPathStream( const String& path) {
// Allocate the return list
StringList pathList;
// Split the path into directory path and file/stream name
pathList = FileName::splitPath(path, DOS_SEPERATOR);
if ( pathList[1].length() == 0)
return pathList;
// Split the file name into file and stream names
int pos = pathList[1].indexOf( NTFSStreamSeperator);
if ( pos != -1) {
// Split the file/stream name
pathList[2] = pathList[1].substring(pos);
pathList[1] = pathList[1].substring(0,pos);
}
// Return the path components list
return pathList;
}

View File

@@ -0,0 +1,76 @@
/*
* Copyright (C) 2005 Alfresco, Inc.
*
* Licensed under the Alfresco Network License. You may obtain a
* copy of the License at
*
* http://www.alfrescosoftware.com/legal/
*
* Please view the license relevant to your network subscription.
*
* BY CLICKING THE "I UNDERSTAND AND ACCEPT" BOX, OR INSTALLING,
* READING OR USING ALFRESCO'S Network SOFTWARE (THE "SOFTWARE"),
* YOU ARE AGREEING ON BEHALF OF THE ENTITY LICENSING THE SOFTWARE
* ("COMPANY") THAT COMPANY WILL BE BOUND BY AND IS BECOMING A PARTY TO
* THIS ALFRESCO NETWORK AGREEMENT ("AGREEMENT") AND THAT YOU HAVE THE
* AUTHORITY TO BIND COMPANY. IF COMPANY DOES NOT AGREE TO ALL OF THE
* TERMS OF THIS AGREEMENT, DO NOT SELECT THE "I UNDERSTAND AND AGREE"
* BOX AND DO NOT INSTALL THE SOFTWARE OR VIEW THE SOURCE CODE. COMPANY
* HAS NOT BECOME A LICENSEE OF, AND IS NOT AUTHORIZED TO USE THE
* SOFTWARE UNLESS AND UNTIL IT HAS AGREED TO BE BOUND BY THESE LICENSE
* TERMS. THE "EFFECTIVE DATE" FOR THIS AGREEMENT SHALL BE THE DAY YOU
* CHECK THE "I UNDERSTAND AND ACCEPT" BOX.
*/
#include "util\Integer.h"
using namespace Alfresco;
/**
* Convert an integer value to a hexadecimal string
*
* @param ival const unsigned int
* @return String
*/
String Integer::toHexString( const unsigned int ival) {
char buf[32];
itoa(ival, buf, 16);
return String(buf);
}
/**
* Convert an buffer pointer to a hexadecimal string
*
* @param ptr BUFPTR
* @return String
*/
String Integer::toHexString( BUFPTR ptr) {
char buf[32];
sprintf( buf, "%p", ptr);
return String(buf);
}
/**
* Convert an integer to a string
*
* @param ival unsigned int
* @param radix unsigned int
* @return String
*/
String Integer::toString( unsigned int ival, unsigned int radix) {
char buf[32];
itoa(ival, buf, radix);
return String(buf);
}
/**
* Parse a string to generate an integer value
*
* @param str const String&
* @param radix unsigned int
* @return unsigned int
*/
unsigned int Integer::parseInt( const String& str, unsigned int radix) {
wchar_t* pEndPtr = NULL;
return (unsigned int) wcstoul( str.data(), &pEndPtr, radix);
}

View File

@@ -0,0 +1,96 @@
/*
* Copyright (C) 2005 Alfresco, Inc.
*
* Licensed under the Alfresco Network License. You may obtain a
* copy of the License at
*
* http://www.alfrescosoftware.com/legal/
*
* Please view the license relevant to your network subscription.
*
* BY CLICKING THE "I UNDERSTAND AND ACCEPT" BOX, OR INSTALLING,
* READING OR USING ALFRESCO'S Network SOFTWARE (THE "SOFTWARE"),
* YOU ARE AGREEING ON BEHALF OF THE ENTITY LICENSING THE SOFTWARE
* ("COMPANY") THAT COMPANY WILL BE BOUND BY AND IS BECOMING A PARTY TO
* THIS ALFRESCO NETWORK AGREEMENT ("AGREEMENT") AND THAT YOU HAVE THE
* AUTHORITY TO BIND COMPANY. IF COMPANY DOES NOT AGREE TO ALL OF THE
* TERMS OF THIS AGREEMENT, DO NOT SELECT THE "I UNDERSTAND AND AGREE"
* BOX AND DO NOT INSTALL THE SOFTWARE OR VIEW THE SOURCE CODE. COMPANY
* HAS NOT BECOME A LICENSEE OF, AND IS NOT AUTHORIZED TO USE THE
* SOFTWARE UNLESS AND UNTIL IT HAS AGREED TO BE BOUND BY THESE LICENSE
* TERMS. THE "EFFECTIVE DATE" FOR THIS AGREEMENT SHALL BE THE DAY YOU
* CHECK THE "I UNDERSTAND AND ACCEPT" BOX.
*/
#include "util\Long.h"
using namespace Alfresco;
/**
* Convert a long/64 bit integer value to a hexadecimal string
*
* @param lval const LONG64
* @return String
*/
String Long::toHexString( const LONG64 lval) {
char buf[32];
sprintf( buf, "%I64x", lval);
return String(buf);
}
/**
* Convert a long/64 bit integer value to a decimal string
*
* @param lval const LONG64
* @return String
*/
String Long::toString( const LONG64 lval) {
char buf[32];
sprintf( buf, "%I64d", lval);
return String(buf);
}
/**
* Make a long/64bit value from the low/high 32bit values
*
* @param lowPart unsigned int
* @param highPart unsigned int
* @return LONG64
*/
LONG64 Long::makeLong( unsigned int lowPart, unsigned int highPart) {
LONG64 lVal = (LONG64) lowPart + (((LONG64) highPart) << 32);
return lVal;
}
/**
* Make a long/64bit value from the low/high 32bit values of the FILETIME structure
*
* @param fTime FILETIME
* @return LONG64
*/
LONG64 Long::makeLong( FILETIME fTime) {
LONG64 lVal = (LONG64) fTime.dwLowDateTime + (((LONG64) fTime.dwHighDateTime) << 32);
return lVal;
}
/**
* Parse a string to generate a long/64 bit integer value
*
* @param str const String&
* @param radix unsigned int
* @return LONG64
*/
LONG64 Long::parseLong( const String& str, unsigned int radix) {
wchar_t* pEndPtr = NULL;
return _wcstoui64( str.data(), &pEndPtr, radix);
}
/**
* Copy a long/64bit value to a FILETIME structure
*
* @param lval LONG64
* @param ftime FILETIME&
*/
void Long::copyTo( LONG64 lval, FILETIME& ftime) {
memcpy( &ftime, &lval, sizeof( LONG64));
}

View File

@@ -0,0 +1,889 @@
/*
* Copyright (C) 2005 Alfresco, Inc.
*
* Licensed under the Alfresco Network License. You may obtain a
* copy of the License at
*
* http://www.alfrescosoftware.com/legal/
*
* Please view the license relevant to your network subscription.
*
* BY CLICKING THE "I UNDERSTAND AND ACCEPT" BOX, OR INSTALLING,
* READING OR USING ALFRESCO'S Network SOFTWARE (THE "SOFTWARE"),
* YOU ARE AGREEING ON BEHALF OF THE ENTITY LICENSING THE SOFTWARE
* ("COMPANY") THAT COMPANY WILL BE BOUND BY AND IS BECOMING A PARTY TO
* THIS ALFRESCO NETWORK AGREEMENT ("AGREEMENT") AND THAT YOU HAVE THE
* AUTHORITY TO BIND COMPANY. IF COMPANY DOES NOT AGREE TO ALL OF THE
* TERMS OF THIS AGREEMENT, DO NOT SELECT THE "I UNDERSTAND AND AGREE"
* BOX AND DO NOT INSTALL THE SOFTWARE OR VIEW THE SOURCE CODE. COMPANY
* HAS NOT BECOME A LICENSEE OF, AND IS NOT AUTHORIZED TO USE THE
* SOFTWARE UNLESS AND UNTIL IT HAS AGREED TO BE BOUND BY THESE LICENSE
* TERMS. THE "EFFECTIVE DATE" FOR THIS AGREEMENT SHALL BE THE DAY YOU
* CHECK THE "I UNDERSTAND AND ACCEPT" BOX.
*/
#include "util\String.h"
using namespace Alfresco;
using namespace std;
/**
* Default constructor
*/
String::String() {
m_string = std::wstring();
}
/**
* Class constructor
*
* @param alloc const unsigned int
*/
String::String(const unsigned int alloc) {
m_string = std::wstring();
m_string.reserve( alloc);
}
/**
* Class constructor
*
* @param str const char*
*/
String::String(const char* str) {
// Expand the characters to wide characters and append to the string
wchar_t wch;
while ( *str != '\0') {
wch = (wchar_t) *str++;
m_string += wch;
}
}
/**
* Class constructor
*
* @param str const unsigned char*
*/
String::String(const unsigned char* str) {
// Expand the characters to wide characters and append to the string
wchar_t wch;
while ( *str != '\0') {
wch = (wchar_t) *str++;
m_string += wch;
}
}
/**
* Class constructor
*
* @param buf const char*
* @param offset const unsigned int
* @param len const unsigned int
*/
String::String(const char* buf, const unsigned int offset, const unsigned int len) {
// Expand the characters to wide characters and append to the string
wchar_t wch;
const char* str = buf + offset;
unsigned int sLen = len;
while ( sLen--) {
wch = (wchar_t) *str++;
m_string += wch;
}
}
/**
* Class constructor
*
* @param str const wchar_t*
*/
String::String(const wchar_t* str) {
m_string = std::wstring( str);
}
/**
* Class constructor
*
* @param buf const wchar_t*
* @param offset const unsigned int
* @param len const unsigned int
*/
String::String(const wchar_t* buf, const unsigned int offset, const unsigned int len) {
m_string = std::wstring(buf + offset, len);
}
/**
* Class constructor
*
* @param str const std::wstring&
*/
String::String(const std::wstring& str) {
m_string = str;
}
/**
* Copy constructor
*
* @param str const String&
*/
String::String(const String& str) {
m_string = std::wstring(str.data());
}
/**
* Class constructor
*
* @param byts ByteArray&
*/
String::String( ByteArray& byts) {
// Expand the characters to wide characters and append to the string
wchar_t wch;
for ( unsigned int idx = 0; idx < byts.getLength(); idx++) {
wch = (wchar_t) byts[idx];
m_string += wch;
}
}
/**
* Compare strings for equality
*
* @param str const wchar_t*
* @return bool
*/
bool String::equals(const wchar_t* str) const {
// Check that the string is valid
if ( str == NULL)
return false;
// Compare the strings
if ( m_string.compare(str) == 0)
return true;
return false;
}
/**
* Compare strings for equality
*
* @param str const String&
* @return bool
*/
bool String::equals(const String& str) const {
// Compare the strings
if ( m_string.compare(str.data()) == 0)
return true;
return false;
}
/**
* Compare strings for equality ignoring case
*
* @param str const wchar_t*
* @return bool
*/
bool String::equalsIgnoreCase(const wchar_t* str) const {
return _wcsicmp( str, data()) == 0 ? true : false;
}
/**
* Compare strings for equality ignoring case
*
* @param str const String&
* @return bool
*/
bool String::equalsIgnoreCase(const String& str) const {
return _wcsicmp( str.data(), data()) == 0 ? true : false;
}
/**
* Compare strings
*
* @param str const String&
* @return int
*/
int String::compareTo( const String& str) const {
return m_string.compare( str.getString());
}
/**
* Compare strings
*
* @param pStr const wchar_t*
* @return int
*/
int String::compareTo( const wchar_t* pStr) const {
return m_string.compare( pStr);
}
/**
* Convert the string to lower case returning the resulting String
*
* @return String
*/
String String::toLowerCase() const {
// Create a copy of the string then convert to lowercase
std::wstring lstr(m_string);
for ( unsigned int i = 0; i < lstr.length(); i++)
lstr[i] = tolower(lstr[i]);
return String(lstr);
}
/**
* Convert the string to upper case returning the resulting String
*
* @return String
*/
String String::toUpperCase() const {
// Create a copy of the string then convert to uppercase
std::wstring ustr(m_string);
for ( unsigned int i = 0; i < ustr.length(); i++)
ustr[i] = toupper(ustr[i]);
return String(ustr);
}
/**
* Return the index of the specified character, or -1 if not found
*
* @param ch const wchar_t
* @param startIndex int
* @return int
*/
int String::indexOf(const wchar_t ch, int startIndex) const {
return (int) m_string.find_first_of( ch, startIndex);
}
/**
* Return the index of the specified string, or -1 if not found
*
* @param str const wchar_t*
* @param startIndex int
* @return int
*/
int String::indexOf(const wchar_t* str, int startIndex) const {
return (int) m_string.find_first_of( str, startIndex);
}
/**
* Return the index of the specified string, or -1 if not found
*
* @param str const String&
* @param startIndex int
* @return int
*/
int String::indexOf(const String& str, int startIndex) const {
return (int) m_string.find_first_of( str, startIndex);
}
/**
* Return the last index of the specified character, or -1 if not found
*
* @param ch const wchar_t
* @param startIndex int
* @return int
*/
int String::lastIndexOf(const wchar_t ch, int startIndex) const {
return (int) m_string.find_last_of( ch, startIndex);
}
/**
* Return the last index of the specified string, or -1 if not found
*
* @param str const wchar_t*
* @param startIndex int
* @return int
*/
int String::lastIndexOf(const wchar_t* str, int startIndex) const {
return (int) m_string.find_last_of( str, startIndex);
}
/**
* Return the last index of the specified string, or -1 if not found
*
* @param str const String&
* @param startIndex int
* @return int
*/
int String::lastIndexOf(const String& str, int startIndex) const {
return (int) m_string.find_last_of( str, startIndex);
}
/**
* Check if this string starts with the specified string.
*
* @param str const wchar_t*
* @return bool
*/
bool String::startsWith(const wchar_t* str) const {
// Check if the string to check is valid
if ( str == NULL)
return false;
// Get the string length, if the comparison string is longer than this string
// then there is no match.
size_t len = wcslen(str);
if ( str == NULL || wcslen(str) > m_string.length())
return false;
// Check if this string starts with the specified string
if ( m_string.compare(0, len, str) == 0)
return true;
return false;
}
/**
* Check if this string starts with the specified string.
*
* @param str const String&
* @return bool
*/
bool String::startsWith(const String& str) const {
// Get the string length, if the comparison string is longer than this string
// then there is no match.
if ( str.length() > m_string.length())
return false;
// Check if this string starts with the specified string
if ( m_string.compare(0, str.length(), str.data()) == 0)
return true;
return false;
}
/**
* Check if this string starts with the specified string, ignoring case.
*
* @param str const wchar_t*
* @return bool
*/
bool String::startsWithIgnoreCase(const wchar_t* str) const {
// Check if the string to check is valid
if ( str == NULL)
return false;
// Get the string length, if the comparison string is longer than this string
// then there is no match.
size_t len = wcslen(str);
if ( str == NULL || wcslen(str) > m_string.length())
return false;
// Check if this string starts with the specified string
if ( _wcsnicmp(str, data(), len) == 0)
return true;
return false;
}
/**
* Check if this string starts with the specified string, ignoring case.
*
* @param str const String&
* @return bool
*/
bool String::startsWithIgnoreCase(const String& str) const {
// Get the string length, if the comparison string is longer than this string
// then there is no match.
if ( str.length() > m_string.length())
return false;
// Check if this string starts with the specified string
if ( _wcsnicmp( str.data(), data(), str.length()) == 0)
return true;
return false;
}
/**
* Check if this string ends with the specified string.
*
* @param str const wchar_t*
* @return bool
*/
bool String::endsWith(const wchar_t* str) const {
// Check if the string to check is valid
if ( str == NULL)
return false;
// Get the string length, if the comparison string is longer than this string
// then there is no match.
size_t len = wcslen(str);
if ( str == NULL || wcslen(str) > m_string.length())
return false;
// Check if this string ends with the specified string
if ( m_string.compare(m_string.length() - len, len, str) == 0)
return true;
return false;
}
/**
* Check if this string ends with the specified string.
*
* @param str const String&
* @return bool
*/
bool String::endsWith(const String& str) const {
// Get the string length, if the comparison string is longer than this string
// then there is no match.
if ( str.length() > m_string.length())
return false;
// Check if this string ends with the specified string
if ( m_string.compare(m_string.length() - str.length(), str.length(), str.data()) == 0)
return true;
return false;
}
/**
* Trim leading and trailing whitespace from the string returning the resulting String
*
* @return String
*/
String String::trim( void) const {
std::wstring str = m_string;
str.erase( str.find_last_not_of( L" ") + 1);
return String( str);
}
/**
* Return a substring of this string
*
* @param beginIndex unsigned int
* @return String
*/
String String::substring( unsigned int beginIndex) const {
std::wstring str = m_string.substr( beginIndex);
return String(str);
}
/**
* Return a substring of this string
*
* @param beginIndex unsigned int
* @param endIndex unsigned int
* @return String
*/
String String::substring( unsigned int beginIndex, unsigned int endIndex) const {
std::wstring str = m_string.substr( beginIndex, (endIndex - beginIndex));
return String(str);
}
/**
* Assignment operator
*
* @param str const wchar_t*
* @return String&
*/
String& String::operator=(const wchar_t* str) {
m_string = str;
return *this;
}
/**
* Assignment operator
*
* @param str const String&
* @return String&
*/
String& String::operator=(const String& str) {
m_string = str.data();
return *this;
}
/**
* Return the string as an array of 8 bit bytes.
*
* @param byts ByteArray&
* @return ByteArray
*/
ByteArray String::getBytes( ByteArray& byts) const {
// Create a byte array to hold the byte data
byts.setLength( length());
// Convert the wide characters to ASCII characters
for ( unsigned int i = 0; i < length(); i++)
byts[ i] = (char) (charAt(i) & 0xFF);
return byts;
}
/**
* Return the string as an array of 8 bit bytes.
*
* @return ByteArray
*/
ByteArray String::getBytes( void) const {
// Create a byte array to hold the byte data
ByteArray byts;
byts.setLength( length());
// Convert the wide characters to ASCII characters
for ( unsigned int i = 0; i < length(); i++)
byts[ i] = (char) (charAt(i) & 0xFF);
return byts;
}
/**
* Equality operator
*
* @param str const String&
* @return bool
*/
bool String::operator== ( const String& str) const {
return equals( str);
}
/**
* Equality operator
*
* @param str const wchar_t*
* @return bool
*/
bool String::operator== ( const wchar_t* str) const {
return equals( str);
}
/**
* Equality operator
*
* @param str const char*
* @return bool
*/
bool String::operator== ( const char* str) const {
return equals( String( str));
}
/**
* Wide character output stream operator
*
* @param out wostream&
* @param str const String&
* @return wostream&
*/
std::wostream& Alfresco::operator<< ( std::wostream& out, const Alfresco::String& str) {
return out << str.data();
}
/**
* Less than operator
*
* @param str const String&
* @return bool
*/
bool String::operator<( const String& str) const {
return getString().compare( str.getString()) < 0 ? true : false;
}
/**
* ASCII character output stream operator
*
* @param out ostream&
* @param str const String&
* @return ostream&
*/
std::ostream& Alfresco::operator<< ( std::ostream& out, const Alfresco::String& str) {
std::string ascStr;
ascStr.reserve( str.length());
for ( unsigned int i = 0; i < str.length(); i++)
ascStr += (char) ( str.charAt( i) & 0xFF);
return out << ascStr.c_str();
}
/**
* Replace occurrences of the character oldCh with newCh
*
* @param oldCh wchar_t
* @param newCh wchar_t
*/
void String::replace( wchar_t oldCh, wchar_t newCh) {
if ( m_string.size() == 0)
return;
for ( unsigned int i = 0; i < m_string.size(); i++) {
if ( m_string.at( i) == oldCh)
m_string[i] = newCh;
}
}
/**
* Append a character to this string
*
* @param ch wchar_t
*/
void String::append( wchar_t ch) {
m_string += ch;
}
/**
* Append a string to this string
*
* @param str const char*
*/
void String::append ( const char* str) {
// Expand the characters to wide characters and append to the string
wchar_t wch;
while ( *str != '\0') {
wch = (wchar_t) *str++;
m_string += wch;
}
}
/**
* Append a string to this string
*
* @param str const wchar_t*
*/
void String::append (const wchar_t* str) {
m_string += str;
}
/**
* Append a string to this string
*
* @param str const String&
*/
void String::append (const String& str) {
m_string += str.getString();
}
/**
* Append an integer value to this string
*
* @param ival const unsigned int
*/
void String::append (const unsigned int ival) {
wchar_t buf[32];
swprintf( buf, L"%u", ival);
m_string += buf;
}
/**
* Append a long value to this string
*
* @param lval const unsigned long
*/
void String::append (const unsigned long lval) {
wchar_t buf[32];
swprintf( buf, L"%lu", lval);
m_string += buf;
}
/**
* Append a long/64 bit value to this string
*
* @param l64val const unsigned long
*/
void String::append (const LONG64 l64val) {
wchar_t buf[32];
swprintf( buf, L"%I64u", l64val);
m_string += buf;
}
/**
* Split a string into tokens
*
* @param delims const String&
* @return StringList
*/
StringList String::tokenize( const String& delims) const {
// Skip leading delimiters
StringList tokens;
string::size_type lastPos = m_string.find_first_not_of( delims, 0);
// Find a non-delimiter character
string::size_type pos = m_string.find_first_of( delims, lastPos);
while ( pos != string::npos || lastPos != string::npos) {
// Add the current token to the list
tokens.addString( m_string.substr( lastPos, pos - lastPos));
// Skip delimiter(s)
lastPos = m_string.find_first_not_of( delims, pos);
// Find next token
pos = m_string.find_first_of( delims, lastPos);
}
// Return the token list
return tokens;
}
/**
* Default constructor
*/
StringList::StringList( void) {
}
/**
* Class constructor
*
* @param reserve unsigned int
*/
StringList::StringList( unsigned int reserve) {
m_list.reserve( reserve);
}
/**
* Copy constructor
*
* @param strList const StringList&
*/
StringList::StringList( const StringList& strList) {
copyFrom( strList);
}
/**
* Copy strings from the specified list
*
* @param strList const StringList&
*/
void StringList::copyFrom( const StringList& strList) {
for ( unsigned int idx = 0; idx < strList.numberOfStrings(); idx++)
addString( strList.getStringAt( idx));
}
/**
* Check if the list contains the string
*
* @param str const String&
* @return bool
*/
bool StringList::containsString ( const String& str) {
for ( std::vector<String>::iterator pos = m_list.begin(); pos < m_list.end(); pos++) {
if ( str.equals( *pos))
return true;
}
return false;
}
/**
* Check if the list contains the string, ignoring case
*
* @param str const String&
* @return bool
*/
bool StringList::containsStringCaseless ( const String& str) {
for ( std::vector<String>::iterator pos = m_list.begin(); pos < m_list.end(); pos++) {
if ( str.equalsIgnoreCase( *pos))
return true;
}
return false;
}
/**
* Find the specified string and return the position within the list, or -1 if not found
*
* @param str const String&
* @return int
*/
int StringList::indexOf( const String& str) const {
for ( unsigned int i = 0; i < m_list.size(); i++) {
if ( m_list[i].equals( str))
return (int) i;
}
return -1;
}
/**
* Remove the specified string from the list
*
* @param str const String&
*/
void StringList::removeString ( const String& str) {
for ( std::vector<String>::iterator pos = m_list.begin(); pos < m_list.end(); pos++) {
if ( str.equals( *pos)) {
m_list.erase( pos);
return;
}
}
}
/**
* Remove the specified string from the list, ignoring case
*
* @param str const String&
*/
void StringList::removeStringCaseless ( const String& str) {
for ( std::vector<String>::iterator pos = m_list.begin(); pos < m_list.end(); pos++) {
if ( str.equalsIgnoreCase( *pos)) {
m_list.erase( pos);
return;
}
}
}
/**
* Return the string list as a comma separated string
*
* @return String
*/
String StringList::toString( void) const {
String ret;
for ( unsigned int i = 0; i < numberOfStrings(); i++) {
ret += getStringAt( i);
ret += ",";
}
return ret;
}

View File

@@ -0,0 +1,47 @@
/*
* Copyright (C) 2005 Alfresco, Inc.
*
* Licensed under the Alfresco Network License. You may obtain a
* copy of the License at
*
* http://www.alfrescosoftware.com/legal/
*
* Please view the license relevant to your network subscription.
*
* BY CLICKING THE "I UNDERSTAND AND ACCEPT" BOX, OR INSTALLING,
* READING OR USING ALFRESCO'S Network SOFTWARE (THE "SOFTWARE"),
* YOU ARE AGREEING ON BEHALF OF THE ENTITY LICENSING THE SOFTWARE
* ("COMPANY") THAT COMPANY WILL BE BOUND BY AND IS BECOMING A PARTY TO
* THIS ALFRESCO NETWORK AGREEMENT ("AGREEMENT") AND THAT YOU HAVE THE
* AUTHORITY TO BIND COMPANY. IF COMPANY DOES NOT AGREE TO ALL OF THE
* TERMS OF THIS AGREEMENT, DO NOT SELECT THE "I UNDERSTAND AND AGREE"
* BOX AND DO NOT INSTALL THE SOFTWARE OR VIEW THE SOURCE CODE. COMPANY
* HAS NOT BECOME A LICENSEE OF, AND IS NOT AUTHORIZED TO USE THE
* SOFTWARE UNLESS AND UNTIL IT HAS AGREED TO BE BOUND BY THESE LICENSE
* TERMS. THE "EFFECTIVE DATE" FOR THIS AGREEMENT SHALL BE THE DAY YOU
* CHECK THE "I UNDERSTAND AND ACCEPT" BOX.
*/
#include "util\System.h"
#include <sys\timeb.h>
using namespace Alfresco;
/**
* Return the current system time in milliseconds since Jan 1 1970
*
* @return DATETIME
*/
DATETIME System::currentTimeMillis( void) {
// Get the current system time
struct __timeb64 timeNow;
_ftime64( &timeNow);
// Build the milliseconds time
DATETIME timeNowMillis = ( timeNow.time * 1000L) + (DATETIME) timeNow.millitm;
return timeNowMillis;
}