Included UtiLite library directly in Rtabmap source (to simplify the installation)

git-svn-id: http://rtabmap.googlecode.com/svn/trunk/rtabmap@857 f169173b-cf89-36c8-b27e-44dbe73f0c83
This commit is contained in:
matlabbe
2013-04-30 20:21:27 +00:00
parent b675d2672b
commit 85c2f5d1e9
5 changed files with 1039 additions and 0 deletions

View File

@@ -0,0 +1,400 @@
/////////////////////////////////////////////////////////////////////
// Written by Phillip Sitbon
// Copyright 2003
//
// Modified by Mathieu Labbe
//
// Posix/Thread.h
// - Posix thread
//
/////////////////////////////////////////////////////////////////////
#ifndef _U_Thread_Posix_
#define _U_Thread_Posix_
#include "rtabmap/utilite/USemaphore.h"
#include "rtabmap/utilite/UMutex.h"
#include <pthread.h>
/**
* Calling thread sleeps for some milliseconds.
*/
inline void uSleep(unsigned int ms)
{
struct timespec req;
struct timespec rem;
req.tv_sec = ms / 1000;
req.tv_nsec = (ms - req.tv_sec * 1000) * 1000 * 1000;
nanosleep (&req, &rem);
}
/**
* Calling thread sleeps for some microseconds.
*/
inline void uSleepMicro(unsigned int us)
{
struct timespec req;
struct timespec rem;
req.tv_sec = us / 1000000;
req.tv_nsec = (us - req.tv_sec * 1000000) * 1000;
nanosleep (&req, &rem);
}
/**
* Calling thread sleeps for some nanoseconds.
*/
inline void uSleepNano(unsigned int ns)
{
struct timespec req;
struct timespec rem;
req.tv_sec = ns / 1000000000;
req.tv_nsec = (ns - req.tv_sec * 1000000000);
nanosleep (&req, &rem);
}
#define InvalidHandle 0
#define THREAD_HANDLE pthread_t
typedef void *( * pthread_fn )( void * );
template
<
typename Thread_T
>
class UThreadC
{
private:
struct Instance;
public:
typedef Thread_T & Thread_R;
typedef const Thread_T & Thread_C_R;
typedef THREAD_HANDLE Handle;
typedef void ( *Handler)( Thread_R );
virtual ~UThreadC() {}
protected:
UThreadC() {}
virtual void ThreadMain( Thread_R ) = 0;
static void Exit()
{ pthread_exit(0); }
static void TestCancel()
{ pthread_testcancel(); }
static Handle Self()
{ return (Handle)pthread_self(); }
public:
static int Create(
const Handler & Function,
Thread_C_R Param,
Handle * const & H = 0,
const bool & CreateDetached = false,
const unsigned int & StackSize = 0,
const bool & CancelEnable = false,
const bool & CancelAsync = false
)
{
M_Create().lock();
pthread_attr_t attr;
pthread_attr_init(&attr);
if ( CreateDetached )
pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
if ( StackSize )
pthread_attr_setstacksize(&attr,StackSize);
Instance I(Param,0,Function,CancelEnable,CancelAsync);
Handle h=InvalidHandle;
int R = pthread_create((pthread_t *)&h,&attr,(pthread_fn)ThreadMainHandler,(void *)&I);
pthread_attr_destroy(&attr);
if(H) *H = h;
if ( !R ) S_Create().acquire();
M_Create().unlock();
return R;
}
int Create(
Thread_C_R Param,
Handle * const & H = 0,
const bool & CreateDetached = false,
const unsigned int & StackSize = 0,
const bool & CancelEnable = false,
const bool & CancelAsync = false
) const
{
M_Create().lock();
pthread_attr_t attr;
pthread_attr_init(&attr);
if ( CreateDetached )
pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
if ( StackSize )
pthread_attr_setstacksize(&attr,StackSize);
Instance I(Param,const_cast<UThreadC *>(this),0,CancelEnable,CancelAsync);
Handle h=InvalidHandle;
int R = pthread_create((pthread_t *)&h,&attr,(pthread_fn)ThreadMainHandler,(void *)&I);
pthread_attr_destroy(&attr);
if(H) *H = h;
if ( !R ) S_Create().acquire();
M_Create().unlock();
return R;
}
static int Join( Handle H )
{ return pthread_join(H,0); }
static int Kill( Handle H )
{ return pthread_cancel(H); }
static int Detach( Handle H )
{ return pthread_detach(H); }
private:
static const UMutex &M_Create() { static UMutex M; return M; }
static USemaphore &S_Create() { static USemaphore S; return S; }
static void *ThreadMainHandler( Instance *Param )
{
Instance I(*Param);
Thread_T Data(I.Data);
S_Create().release();
if ( I.Flags & 1 /*CancelEnable*/ )
{
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL);
if ( I.Flags & 2 /*CancelAsync*/ )
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,NULL);
else
pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,NULL);
}
else
{
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,NULL);
}
if ( I.Owner )
I.Owner->ThreadMain(Data);
else
I.pFN(Data);
return 0;
}
struct Instance
{
Instance( Thread_C_R P, UThreadC<Thread_T> *const &O, const UThreadC<Thread_T>::Handler &pH = 0, const bool &CE=false, const bool &CA=false )
: Data(P), Owner(O), pFN(pH), Flags(0) { if ( CE ) Flags|=1; if ( CA ) Flags|=2; }
Thread_C_R Data;
UThreadC<Thread_T> * Owner;
Handler pFN;
unsigned char Flags;
};
};
/////////////////////////////////////////////////////////////////////
// Explicit specialization, no thread parameters
//
template<>
class UThreadC<void>
{
private:
struct Instance;
public:
typedef THREAD_HANDLE Handle;
typedef void ( *Handler)();
virtual ~UThreadC<void>() {}
protected:
UThreadC<void>() {}
virtual void ThreadMain() = 0;
static void Exit()
{ pthread_exit(0); }
static void TestCancel()
{ pthread_testcancel(); }
static Handle Self()
{ return (Handle)pthread_self(); }
public:
static int Create(
const Handler & Function,
Handle * const & H = 0,
const bool & CreateDetached = false,
const unsigned int & StackSize = 0,
const bool & CancelEnable = false,
const bool & CancelAsync = false
)
{
M_Create().lock();
pthread_attr_t attr;
pthread_attr_init(&attr);
if ( CreateDetached )
pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
if ( StackSize )
pthread_attr_setstacksize(&attr,StackSize);
Instance I(0,Function,CancelEnable,CancelAsync);
Handle h=InvalidHandle;
int R = pthread_create((pthread_t *)&h,&attr,(pthread_fn)ThreadMainHandler,(void *)&I);
pthread_attr_destroy(&attr);
if(H) *H = h;
if ( !R ) S_Create().acquire();
M_Create().unlock();
return R;
}
int Create(
Handle * const & H = 0,
const bool & CreateDetached = false,
const unsigned int & StackSize = 0,
const bool & CancelEnable = false,
const bool & CancelAsync = false
) const
{
M_Create().lock();
pthread_attr_t attr;
pthread_attr_init(&attr);
if ( CreateDetached )
pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
if ( StackSize )
pthread_attr_setstacksize(&attr,StackSize);
Instance I(const_cast<UThreadC *>(this),0,CancelEnable,CancelAsync);
Handle h=InvalidHandle;
int R = pthread_create((pthread_t *)&h,&attr,(pthread_fn)ThreadMainHandler,(void *)&I);
pthread_attr_destroy(&attr);
if(H) *H = h;
if ( !R ) S_Create().acquire();
M_Create().unlock();
return R;
}
int Create(
unsigned long & ThreadId,
Handle * const & H = 0,
const bool & CreateDetached = false,
const unsigned int & StackSize = 0,
const bool & CancelEnable = false,
const bool & CancelAsync = false
) const
{
M_Create().lock();
pthread_attr_t attr;
pthread_attr_init(&attr);
if ( CreateDetached )
pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
if ( StackSize )
pthread_attr_setstacksize(&attr,StackSize);
Instance I(const_cast<UThreadC *>(this),0,CancelEnable,CancelAsync);
*H = InvalidHandle;
int R = pthread_create((pthread_t *)&(*H),&attr,(pthread_fn)ThreadMainHandler,(void *)&I);
ThreadId = (unsigned long)*H;
pthread_attr_destroy(&attr);
if ( !R ) S_Create().acquire();
M_Create().unlock();
return R;
}
static int Join( Handle H )
{ return pthread_join(H,0); }
static int Kill( Handle H )
{ return pthread_cancel(H); }
static int Detach( Handle H )
{ return pthread_detach(H); }
private:
static const UMutex &M_Create() { static UMutex M; return M; }
static USemaphore &S_Create() { static USemaphore S; return S; }
static void *ThreadMainHandler( Instance *Param )
{
Instance I(*Param);
S_Create().release();
if ( I.Flags & 1 /*CancelEnable*/ )
{
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL);
if ( I.Flags & 2 /*CancelAsync*/ )
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,NULL);
else
pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,NULL);
}
else
{
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,NULL);
}
if ( I.Owner )
I.Owner->ThreadMain();
else
I.pFN();
return 0;
}
struct Instance
{
Instance( UThreadC<void> *const &O, const UThreadC<void>::Handler &pH = 0, const bool &CE=false, const bool &CA=false )
: pFN(pH), Owner(O), Flags(0) { if ( CE ) Flags|=1; if ( CA ) Flags|=2; }
UThreadC<void>::Handler pFN;
UThreadC<void> * Owner;
unsigned char Flags;
};
};
#endif // !_U_Thread_Posix_

View File

@@ -0,0 +1,382 @@
/////////////////////////////////////////////////////////////////////
// Written by Phillip Sitbon
// Copyright 2003
//
// Modified by Mathieu Labbe
//
// Win32/Thread.h
// - Windows thread
//
// - From CreateThread Platform SDK Documentation:
//
// "A thread that uses functions from the static C run-time
// libraries should use the beginthread and endthread C run-time
// functions for thread management rather than CreateThread and
// ExitThread. Failure to do so results in small memory leaks
// when ExitThread is called. Note that this is not a problem
// with the C run-time in a DLL."
//
// With regards to this, I have decided to use the CreateThread
// API, unless you define _CRT_ in which case there are two
// possibilities:
//
// 1. Define _USE_BEGINTHREAD: Uses _beginthread/_endthread
// (said to be *unreliable* in the SDK docs)
//
// 2. Don't - Uses _beginthreaded/_endthreadex
//
// A note about _endthread:
//
// It will call CloseHandle() on exit, and if it was already
// closed then you will get an exception. To prevent this, I
// removed the CloseHandle() functionality - this means that
// a Join() WILL wait on a Detach()'ed thread.
//
/////////////////////////////////////////////////////////////////////
#ifndef _U_Thread_Win32_
#define _U_Thread_Win32_
#include "rtabmap/utilite/Win32/UWin32.h"
#include "rtabmap/utilite/USemaphore.h"
#include "rtabmap/utilite/UMutex.h"
inline void uSleep(unsigned int ms)
{
Sleep(ms);
}
#ifdef _CRT_
# include <process.h>
# ifdef _USE_BEGINTHREAD
# define THREAD_CALL __cdecl
# define THREAD_HANDLE uintptr_t
# define THREAD_RET_T void
# define CREATE_THREAD_FAILED (-1L)
# define CREATE_THREAD_ERROR (errno)
# define CREATE_THREAD(_S,_F,_P) ((Handle)_beginthread((void (__cdecl *)(void *))_F,_S,(void *)_P))
# define EXIT_THREAD _endthread()
# define CLOSE_HANDLE(x) 1
# define THREAD_RETURN(x) return
# else
# define THREAD_CALL WINAPI
# define THREAD_HANDLE HANDLE
# define THREAD_RET_T UINT
# define CREATE_THREAD_FAILED (0L)
# define CREATE_THREAD_ERROR (errno)
# define CREATE_THREAD(_S,_F,_P) ((Handle)_beginthreadex(0,_S,(UINT (WINAPI *)(void *))_F,(void *)_P,0,0))
# define EXIT_THREAD _endthreadex(0)
# define CLOSE_HANDLE(x) CloseHandle(x)
# define THREAD_RETURN(x) return(x)
# endif
#else
# define THREAD_CALL WINAPI
# define THREAD_HANDLE HANDLE
# define THREAD_RET_T DWORD
# define CREATE_THREAD_FAILED (0L)
# define CREATE_THREAD_ERROR GetLastError()
# define CREATE_THREAD(_S,_F,_P) ((Handle)CreateThread(0,_S,(DWORD (WINAPI *)(void *))_F,(void *)_P,0,0))
# define CREATE_THREAD2(_S,_F,_P,_ID) ((Handle)CreateThread(0,_S,(DWORD (WINAPI *)(void *))_F,(void *)_P,0,_ID))
# define EXIT_THREAD ExitThread(0)
# define CLOSE_HANDLE(x) CloseHandle(x)
# define THREAD_RETURN(x) return(x)
#endif
#define InvalidHandle 0
template
<
typename Thread_T
>
class UTILITE_EXP UThreadC
{
private:
struct Instance;
public:
typedef Thread_T & Thread_R;
typedef const Thread_T & Thread_C_R;
typedef THREAD_HANDLE Handle;
typedef void (* Handler)( Thread_R );
protected:
UThreadC() {}
virtual void ThreadMain( Thread_R ) = 0;
static void Exit()
{ EXIT_THREAD; }
static void TestCancel()
{ Sleep(0); }
static int Self()
{
Handle Hnd = InvalidHandle;
DuplicateHandle(GetCurrentProcess(),GetCurrentThread(),GetCurrentProcess(),(LPHANDLE)&Hnd,NULL,0,NULL);
return Hnd;
// only a pseudo-handle!
//return (Handle)GetCurrentThread();
}
public:
static int Create(
const Handler & Function,
Thread_C_R Param,
Handle * const & H = 0,
const bool & CreateDetached = false,
const unsigned int & StackSize = 0,
const bool & CancelEnable = false, // UNUSED
const bool & CancelAsync = false // UNUSED
)
{
M_Create().lock();
Instance I(Param,0,Function);
Handle Hnd(CREATE_THREAD(StackSize,ThreadMainHandler,&I));
if ( Hnd == CREATE_THREAD_FAILED )
{
if ( H ) *H = InvalidHandle;
M_Create().unlock();
return CREATE_THREAD_ERROR;
}
if ( H ) *H = Hnd;
S_Create().Wait();
M_Create().unlock();
if ( CreateDetached ) CLOSE_HANDLE(Hnd);
return 0;
}
int Create(
Thread_C_R Param,
Handle * const & H = 0,
const bool & CreateDetached = false,
const unsigned int & StackSize = 0,
const bool & CancelEnable = false, // UNUSED
const bool & CancelAsync = false // UNUSED
) const
{
M_Create().lock();
Instance I(Param,const_cast<UThreadC *>(this));
Handle Hnd(CREATE_THREAD(StackSize,ThreadMainHandler,&I));
if ( Hnd == CREATE_THREAD_FAILED )
{
if ( H ) *H = InvalidHandle;
M_Create().unlock();
return CREATE_THREAD_ERROR;
}
if ( H ) *H = Hnd;
S_Create().Wait();
M_Create().unlock();
if ( CreateDetached ) CLOSE_HANDLE(Hnd);
return 0;
}
static int Join( const Handle &H )
{
DWORD R = WaitForSingleObject((HANDLE)H,INFINITE);
if ( (R == WAIT_OBJECT_0) || (R == WAIT_ABANDONED) )
{
CLOSE_HANDLE(H);
return 0;
}
if ( R == WAIT_TIMEOUT ) return EAGAIN;
return EINVAL;
}
static int Kill( const Handle &H )
{ return TerminateThread((HANDLE)H,0) ? 0 : EINVAL; }
static int Detach( const Handle &H )
{ return (CLOSE_HANDLE(H)?0:EINVAL); }
private:
static const UMutex &M_Create() { static UMutex M; return M; }
static const USemaphore &S_Create() { static USemaphore S; return S; }
static THREAD_RET_T THREAD_CALL ThreadMainHandler( Instance *Param )
{
Instance I(*Param);
Thread_T Data(I.Data);
S_Create().Post();
if ( I.Owner )
I.Owner->ThreadMain(Data);
else
I.pFN(Data);
Exit();
THREAD_RETURN(0);
}
struct Instance
{
Instance( Thread_C_R P, UThreadC<Thread_T> *const &O, const typename UThreadC<Thread_T>::Handler &pH = 0 )
: pFN(pH), Data(P), Owner(O) {}
typename UThreadC<Thread_T>::Handler pFN;
typename UThreadC<Thread_T>::Thread_C_R Data;
UThreadC<Thread_T> * Owner;
};
};
/////////////////////////////////////////////////////////////////////
// Explicit Specialization of void
//
template<>
class UTILITE_EXP UThreadC<void>
{
private:
struct Instance;
public:
typedef THREAD_HANDLE Handle;
typedef void ( *Handler)();
virtual ~UThreadC<void>() {}
protected:
UThreadC<void>() {}
virtual void ThreadMain() = 0;
static void Exit()
{ EXIT_THREAD; }
static void TestCancel()
{ Sleep(0); }
static int Self()
{
return (int)GetCurrentThreadId();
//Handle Hnd = InvalidHandle;
//DuplicateHandle(GetCurrentProcess(),GetCurrentThread(),GetCurrentProcess(),(LPHANDLE)&Hnd,NULL,0,NULL);
//return Hnd;
// only a pseudo-handle!
//return (Handle)GetCurrentThread();
}
public:
static int Create(
const Handler & Function,
Handle * const & H = 0,
const bool & CreateDetached = false,
const unsigned int & StackSize = 0,
const bool & CancelEnable = false, // UNUSED
const bool & CancelAsync = false // UNUSED
)
{
Handle Hnd(CREATE_THREAD(StackSize,ThreadMainHandler_S,Function));
if ( Hnd == CREATE_THREAD_FAILED )
{
if ( H ) *H = InvalidHandle;
return (int)CREATE_THREAD_ERROR;
}
if ( H ) *H = Hnd;
if ( CreateDetached ) CLOSE_HANDLE(Hnd);
return 0;
}
int Create(
Handle * const & H = 0,
const bool & CreateDetached = false,
const unsigned int & StackSize = 0,
const bool & CancelEnable = false, // UNUSED
const bool & CancelAsync = false // UNUSED
) const
{
Handle Hnd(CREATE_THREAD(StackSize,ThreadMainHandler,this));
if ( Hnd == CREATE_THREAD_FAILED )
{
if ( H ) *H = InvalidHandle;
return (int)CREATE_THREAD_ERROR;
}
if ( H ) *H = Hnd;
if ( CreateDetached ) CLOSE_HANDLE(Hnd);
Self();
return 0;
}
int Create(
int & ThreadId,
Handle * const & H = 0,
const bool & CreateDetached = false,
const unsigned int & StackSize = 0,
const bool & CancelEnable = false, // UNUSED
const bool & CancelAsync = false // UNUSED
) const
{
*H = InvalidHandle;
*H = CREATE_THREAD2(StackSize,ThreadMainHandler,this, (LPDWORD)&ThreadId);
if ( *H == CREATE_THREAD_FAILED )
{
*H = InvalidHandle;
return (int)CREATE_THREAD_ERROR;
}
if ( CreateDetached ) CLOSE_HANDLE(*H);
return 0;
}
static int Join( const Handle &H )
{
DWORD R = WaitForSingleObject((HANDLE)H,INFINITE);
if ( (R == WAIT_OBJECT_0) || (R == WAIT_ABANDONED) )
{
CLOSE_HANDLE(H);
return 0;
}
if ( R == WAIT_TIMEOUT ) return EAGAIN;
return EINVAL;
}
static int Kill( const Handle &H )
{ return TerminateThread((HANDLE)H,0) ? 0 : EINVAL; }
static int Detach( const Handle &H )
{ return (CLOSE_HANDLE(H)?0:EINVAL); }
private:
static THREAD_RET_T THREAD_CALL ThreadMainHandler( UThreadC<void> *Param )
{
Param->ThreadMain();
Exit();
THREAD_RETURN(0);
}
static THREAD_RET_T THREAD_CALL ThreadMainHandler_S( Handler Param )
{
Param();
Exit();
THREAD_RETURN(0);
}
};
#endif // !_U_Thread_Win32_

View File

@@ -0,0 +1,64 @@
/////////////////////////////////////////////////////////////////////
// Written by Phillip Sitbon
// Copyright 2003
//
// Win32.h
// - Windows includes
//
/////////////////////////////////////////////////////////////////////
#ifndef _U_Win32_
#define _U_Win32_
#include "rtabmap/utilite/UtiLiteExp.h"
#if !defined(_WINDOWS_)
// WIN32 Excludes
#ifdef WIN32_LEAN_AND_MEAN
# define VC_EXTRALEAN
# define WIN32_LEAN_AND_MEAN
# define _PRSHT_H_
# define NOGDICAPMASKS // CC_*, LC_*, PC_*, CP_*, TC_*, RC_
# define NOVIRTUALKEYCODES // VK_*
# define NOWINMESSAGES // WM_*, EM_*, LB_*, CB_*
# define NOWINSTYLES // WS_*, CS_*, ES_*, LBS_*, SBS_*, CBS_*
# define NOSYSMETRICS // SM_*
# define NOMENUS // MF_*
# define NOICONS // IDI_*
# define NOKEYSTATES // MK_*
# define NOSYSCOMMANDS // SC_*
# define NORASTEROPS // Binary and Tertiary raster ops
# define NOSHOWWINDOW // SW_*
# define OEMRESOURCE // OEM Resource values
# define NOATOM // Atom Manager routines
# define NOCLIPBOARD // Clipboard routines
# define NOCOLOR // Screen colors
# define NOCTLMGR // Control and Dialog routines
# define NODRAWTEXT // DrawText() and DT_*
# define NOGDI // All GDI defines and routines
# define NOKERNEL // All KERNEL defines and routines
# define NOUSER // All USER defines and routines
# define NONLS // All NLS defines and routines
# define NOMB // MB_* and MessageBox()
# define NOMEMMGR // GMEM_*, LMEM_*, GHND, LHND, associated routines
# define NOMETAFILE // typedef METAFILEPICT
# define NOMINMAX // Macros min(a,b) and max(a,b)
# define NOMSG // typedef MSG and associated routines
# define NOOPENFILE // OpenFile(), OemToAnsi, AnsiToOem, and OF_*
# define NOSCROLL // SB_* and scrolling routines
# define NOSERVICE // All Service Controller routines, SERVICE_ equates, etc.
# define NOSOUND // Sound driver routines
# define NOTEXTMETRIC // typedef TEXTMETRIC and associated routines
# define NOWH // SetWindowsHook and WH_*
# define NOWINOFFSETS // GWL_*, GCL_*, associated routines
# define NOCOMM // COMM driver routines
# define NOKANJI // Kanji support stuff.
# define NOHELP // Help engine interface.
# define NOPROFILER // Profiler interface.
# define NODEFERWINDOWPOS // DeferWindowPos routines
# define NOMCX // Modem Configuration Extensions
#endif // WIN32_LEAN_AND_MEAN
//
# include <windows.h>
#endif
#endif // !_U_Win32_

View File

@@ -0,0 +1,23 @@
SET(SRC_FILES
main.cpp
)
SET(INCLUDE_DIRS
../include
)
# Make sure the compiler can find include files from our library.
INCLUDE_DIRECTORIES(${INCLUDE_DIRS})
# Add binary called "resource_tool" that is built from the source file "main.cpp".
# The extension is automatically found.
ADD_EXECUTABLE(uresourcegenerator ${SRC_FILES})
TARGET_LINK_LIBRARIES(uresourcegenerator rtabmap_utilite)
SET_TARGET_PROPERTIES(
uresourcegenerator
PROPERTIES
VERSION ${UTILITE_VERSION}
SOVERSION ${UTILITE_VERSION}
)

View File

@@ -0,0 +1,170 @@
/*
* utilite is a cross-platform library with
* useful utilities for fast and small developing.
* Copyright (C) 2010 Mathieu Labbe
*
* utilite is free library: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* utilite 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 for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "rtabmap/utilite/UtiLite.h"
#include <fstream>
#include <iostream>
#include <string.h>
void showUsage()
{
printf("Usage:\n"
"uresourcegenerator.exe [option] \"file1\" \"file2\" ... \n"
" Create a file named \"file\".h with string\n"
" variable named \"file\" which contains the data of the file.\n"
" Warning, it overwrites the target file\n"
" Options:\n"
" -n \"namespace\" namespace used\n"
" -p \"targetPath\" target path where the file is created\n"
" -v version of the UtiLite library\n");
exit(1);
}
int main(int argc, char * argv[])
{
if(argc < 2)
{
showUsage();
}
else if(argc == 2 && strcmp(argv[1], "-v") == 0)
{
printf("%s\n", UTILITE_VERSION);
exit(0);
}
std::string targetDir = UDirectory::currentDir(); // By default, use the current directory
std::string nspace; // namespace
int k;
for(k=1; k<(argc-1); ++k)
{
if(strcmp(argv[k], "-n") == 0)
{
if(!(k+1<(argc-1)))
{
showUsage();
}
nspace = argv[k+1];
printf(" Using namespace=%s\n", nspace.c_str());
++k;
}
else if(strcmp(argv[k], "-p") == 0)
{
if(!(k+1<(argc-1)))
{
showUsage();
}
targetDir = argv[k+1];
printf(" Using target directory=%s\n", targetDir.c_str());
++k;
}
else
{
break;
}
}
while(k < argc)
{
std::string filePath = argv[k];
std::string varName = UFile::getName(argv[k]);
// replace '_'
for(unsigned int i=0; i<varName.size(); ++i)
{
if(!((varName[i] >= '0' && varName[i] <= '9') ||
(varName[i] >= 'A' && varName[i] <= 'Z') ||
(varName[i] >= 'a' && varName[i] <= 'z')))
{
varName[i] = '_';
}
}
std::string targetFileName = varName + ".h";
// upper case
for(unsigned int i=0; i<varName.size(); ++i)
{
if(varName[i] >= 'a' && varName[i] <= 'z')
{
varName[i] -= 32; // upper case
}
}
std::fstream outFile;
std::fstream inFile;
outFile.open(((targetDir + "/") + targetFileName).c_str(), std::fstream::out);
inFile.open(filePath.c_str(), std::fstream::in | std::fstream::binary);
printf("Input file \"%s\" size = %ld bytes\n", filePath.c_str(), UFile::length(filePath));
if(outFile.is_open() && inFile.is_open())
{
outFile << "/*This is a generated file...*/\n\n";
outFile << "#ifndef " << varName << "_H\n";
outFile << "#define " << varName << "_H\n\n";
if(!nspace.empty())
{
outFile << "namespace " << nspace.c_str() << "\n{\n\n";
}
outFile << "static const char * " << varName.c_str() << " = ";
if(!inFile.good())
{
outFile << "\"\""; //empty string
}
else
{
std::string startLine = "\n \"";
std::string endLine = "\"";
std::vector<char> buffer(1024);
while(inFile.good())
{
inFile.read(buffer.data(), 1024);
std::streamsize count = inFile.gcount();
if(count)
{
outFile.write(startLine.c_str(), startLine.size());
std::string hex = uBytes2Hex(buffer.data(), count);
outFile.write(hex.c_str(), hex.size());
outFile.write(endLine.c_str(), endLine.size());
}
}
}
std::string endOfVar = ";\n\n";
outFile.write(endOfVar.c_str(), endOfVar.size());
if(!nspace.empty())
{
outFile << "}\n\n";
}
outFile << "#endif //" << varName << "_H\n\n";
}
outFile.close();
inFile.close();
printf("Output file \"%s\" size = %ld bytes\n", ((targetDir + "/") + targetFileName).c_str(), UFile::length(((targetDir + "/") + targetFileName).c_str()));
++k;
}
}