ICU-157 tool utilities e.g. for generating binary data files

X-SVN-Rev: 198
This commit is contained in:
Markus Scherer 1999-11-20 01:11:12 +00:00
parent 9a7d692fab
commit bc7e7a3c96
5 changed files with 474 additions and 0 deletions

View File

@ -0,0 +1,64 @@
/*
*******************************************************************************
* *
* COPYRIGHT: *
* (C) Copyright International Business Machines Corporation, 1999 *
* Licensed Material - Program-Property of IBM - All Rights Reserved. *
* US Government Users Restricted Rights - Use, duplication, or disclosure *
* restricted by GSA ADP Schedule Contract with IBM Corp. *
* *
*******************************************************************************
* file name: toolutil.c
* encoding: US-ASCII
* tab size: 8 (not used)
* indentation:4
*
* created on: 1999nov19
* created by: Markus W. Scherer
*
* This file contains utility functions for ICU tools like genccode.
*/
#ifdef WIN32
# define VC_EXTRALEAN
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
#endif
#include "utypes.h"
#include "putil.h"
#include "cmemory.h"
#include "cstring.h"
#include "toolutil.h"
U_CFUNC char *
getLongPathname(char *pathname) {
#ifdef WIN32
/* anticipate problems with "short" pathnames */
static WIN32_FIND_DATA info;
HANDLE file=FindFirstFile(pathname, &info);
if(file!=INVALID_HANDLE_VALUE) {
if(info.cAlternateFileName[0]!=0) {
/* this file has a short name, get and use the long one */
const char *basename=findBasename(pathname);
if(basename!=pathname) {
/* prepend the long filename with the original path */
icu_memmove(info.cFileName+(basename-pathname), info.cFileName, icu_strlen(info.cFileName)+1);
icu_memcpy(info.cFileName, pathname, basename-pathname);
}
pathname=info.cFileName;
}
FindClose(file);
}
#endif
return pathname;
}
U_CFUNC const char *
findBasename(const char *filename) {
const char *basename=icu_strrchr(filename, U_FILE_SEP_CHAR);
if(basename!=NULL) {
return basename+1;
} else {
return filename;
}
}

View File

@ -0,0 +1,108 @@
# Microsoft Developer Studio Project File - Name="toolutil" - Package Owner=<4>
# Microsoft Developer Studio Generated Build File, Format Version 6.00
# ** DO NOT EDIT **
# TARGTYPE "Win32 (x86) Static Library" 0x0104
CFG=toolutil - Win32 Debug
!MESSAGE This is not a valid makefile. To build this project using NMAKE,
!MESSAGE use the Export Makefile command and run
!MESSAGE
!MESSAGE NMAKE /f "toolutil.mak".
!MESSAGE
!MESSAGE You can specify a configuration when running NMAKE
!MESSAGE by defining the macro CFG on the command line. For example:
!MESSAGE
!MESSAGE NMAKE /f "toolutil.mak" CFG="toolutil - Win32 Debug"
!MESSAGE
!MESSAGE Possible choices for configuration are:
!MESSAGE
!MESSAGE "toolutil - Win32 Release" (based on "Win32 (x86) Static Library")
!MESSAGE "toolutil - Win32 Debug" (based on "Win32 (x86) Static Library")
!MESSAGE
# Begin Project
# PROP AllowPerConfigDependencies 0
# PROP Scc_ProjName ""
# PROP Scc_LocalPath ""
CPP=cl.exe
RSC=rc.exe
!IF "$(CFG)" == "toolutil - Win32 Release"
# PROP BASE Use_MFC 0
# PROP BASE Use_Debug_Libraries 0
# PROP BASE Output_Dir "Release"
# PROP BASE Intermediate_Dir "Release"
# PROP BASE Target_Dir ""
# PROP Use_MFC 0
# PROP Use_Debug_Libraries 0
# PROP Output_Dir "Release"
# PROP Intermediate_Dir "Release"
# PROP Target_Dir ""
# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_MBCS" /D "_LIB" /YX /FD /c
# ADD CPP /nologo /W3 /GX /O2 /I "..\..\common" /D "WIN32" /D "NDEBUG" /D "_MBCS" /D "_LIB" /YX /FD /c
# ADD BASE RSC /l 0x409 /d "NDEBUG"
# ADD RSC /l 0x409 /d "NDEBUG"
BSC32=bscmake.exe
# ADD BASE BSC32 /nologo
# ADD BSC32 /nologo
LIB32=link.exe -lib
# ADD BASE LIB32 /nologo
# ADD LIB32 /nologo
!ELSEIF "$(CFG)" == "toolutil - Win32 Debug"
# PROP BASE Use_MFC 0
# PROP BASE Use_Debug_Libraries 1
# PROP BASE Output_Dir "Debug"
# PROP BASE Intermediate_Dir "Debug"
# PROP BASE Target_Dir ""
# PROP Use_MFC 0
# PROP Use_Debug_Libraries 1
# PROP Output_Dir "Debug"
# PROP Intermediate_Dir "Debug"
# PROP Target_Dir ""
# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_MBCS" /D "_LIB" /YX /FD /GZ /c
# ADD CPP /nologo /W3 /Gm /GX /ZI /Od /I "..\..\common" /D "WIN32" /D "_DEBUG" /D "_MBCS" /D "_LIB" /YX /FD /GZ /c
# ADD BASE RSC /l 0x409 /d "_DEBUG"
# ADD RSC /l 0x409 /d "_DEBUG"
BSC32=bscmake.exe
# ADD BASE BSC32 /nologo
# ADD BSC32 /nologo
LIB32=link.exe -lib
# ADD BASE LIB32 /nologo
# ADD LIB32 /nologo
!ENDIF
# Begin Target
# Name "toolutil - Win32 Release"
# Name "toolutil - Win32 Debug"
# Begin Group "Source Files"
# PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat"
# Begin Source File
SOURCE=.\toolutil.c
# End Source File
# Begin Source File
SOURCE=.\unewdata.c
# End Source File
# End Group
# Begin Group "Header Files"
# PROP Default_Filter "h;hpp;hxx;hm;inl"
# Begin Source File
SOURCE=.\toolutil.h
# End Source File
# Begin Source File
SOURCE=.\unewdata.h
# End Source File
# End Group
# End Target
# End Project

View File

@ -0,0 +1,55 @@
/*
*******************************************************************************
* *
* COPYRIGHT: *
* (C) Copyright International Business Machines Corporation, 1999 *
* Licensed Material - Program-Property of IBM - All Rights Reserved. *
* US Government Users Restricted Rights - Use, duplication, or disclosure *
* restricted by GSA ADP Schedule Contract with IBM Corp. *
* *
*******************************************************************************
* file name: toolutil.h
* encoding: US-ASCII
* tab size: 8 (not used)
* indentation:4
*
* created on: 1999nov19
* created by: Markus W. Scherer
*
* This file defines utility functions for ICU tools like genccode.
*/
#ifndef __TOOLUTIL_H__
#define __TOOLUTIL_H__
#include "utypes.h"
/*
* For Windows, a path/filename may be the short (8.3) version
* of the "real", long one. In this case, the short one
* is abbreviated and contains a tilde etc.
* This function returns a pointer to the original pathname
* if it is the "real" one itself, and a pointer to a static
* buffer (not thread-safe) containing the long version
* if the pathname is indeed abbreviated.
*
* On platforms other than Windows, this function always returns
* the input pathname pointer.
*
* This function is especially useful in tools that are called
* by a batch file for loop, which yields short pathnames on Win9x.
*/
U_CFUNC char *
getLongPathname(char *pathname);
/*
* Find the basename at the end of a pathname, i.e., the part
* after the last file separator, and return a pointer
* to this part of the pathname.
* If the pathname only contains a basename and no file separator,
* then the pathname pointer itself is returned.
*/
U_CFUNC const char *
findBasename(const char *filename);
#endif

View File

@ -0,0 +1,189 @@
/*
*******************************************************************************
* *
* COPYRIGHT: *
* (C) Copyright International Business Machines Corporation, 1999 *
* Licensed Material - Program-Property of IBM - All Rights Reserved. *
* US Government Users Restricted Rights - Use, duplication, or disclosure *
* restricted by GSA ADP Schedule Contract with IBM Corp. *
* *
*******************************************************************************
* file name: unewdata.c
* encoding: US-ASCII
* tab size: 8 (not used)
* indentation:4
*
* created on: 1999oct25
* created by: Markus W. Scherer
*/
#include "utypes.h"
#include "ustring.h"
#include "cmemory.h"
#include "cstring.h"
#include "filestrm.h"
#include "udata.h"
#include "unewdata.h"
struct UNewDataMemory {
FileStream *file;
uint16_t headerSize;
uint8_t magic1, magic2;
};
U_CAPI UNewDataMemory * U_EXPORT2
udata_create(const char *type, const char *name,
const UDataInfo *pInfo,
const char *comment,
UErrorCode *pErrorCode) {
UNewDataMemory *pData;
uint16_t headerSize, commentLength;
const char *path;
char filename[512];
uint8_t bytes[16];
if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
return NULL;
} else if(name==NULL || *name==0 || pInfo==NULL) {
*pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
return NULL;
}
/* allocate the data structure */
pData=(UNewDataMemory *)icu_malloc(sizeof(UNewDataMemory));
if(pData==NULL) {
*pErrorCode=U_MEMORY_ALLOCATION_ERROR;
return NULL;
}
/* open the output file */
path=u_getDataDirectory();
if(path!=NULL) {
icu_strcpy(filename, path);
} else {
filename[0]=0;
}
icu_strcat(filename, name);
if(type!=NULL && *type!=0) {
icu_strcat(filename, ".");
icu_strcat(filename, type);
}
pData->file=T_FileStream_open(filename, "wb");
if(pData->file==NULL) {
icu_free(pData);
*pErrorCode=U_FILE_ACCESS_ERROR;
return NULL;
}
/* write the header information */
headerSize=pInfo->size+4;
if(comment!=NULL && *comment!=0) {
commentLength=icu_strlen(comment)+1;
headerSize+=commentLength;
} else {
commentLength=0;
}
/* write the size of the header, take padding into account */
pData->headerSize=(headerSize+15)&~0xf;
pData->magic1=0xda;
pData->magic2=0x27;
T_FileStream_write(pData->file, &pData->headerSize, 4);
/* write the information data */
T_FileStream_write(pData->file, pInfo, pInfo->size);
/* write the comment */
if(commentLength>0) {
T_FileStream_write(pData->file, comment, commentLength);
}
/* write padding bytes to align the data section to 16 bytes */
headerSize&=0xf;
if(headerSize!=0) {
headerSize=16-headerSize;
icu_memset(bytes, 0, headerSize);
T_FileStream_write(pData->file, bytes, headerSize);
}
return pData;
}
U_CAPI uint32_t U_EXPORT2
udata_finish(UNewDataMemory *pData, UErrorCode *pErrorCode) {
uint32_t fileLength=0;
if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
return 0;
}
if(pData!=NULL) {
if(pData->file!=NULL) {
/* fflush(pData->file);*/
fileLength=T_FileStream_size(pData->file);
if(T_FileStream_error(pData->file)) {
*pErrorCode=U_FILE_ACCESS_ERROR;
} else {
fileLength-=pData->headerSize;
}
T_FileStream_close(pData->file);
}
icu_free(pData);
}
return fileLength;
}
U_CAPI void U_EXPORT2
udata_write8(UNewDataMemory *pData, uint8_t byte) {
if(pData!=NULL && pData->file!=NULL) {
T_FileStream_write(pData->file, &byte, 1);
}
}
U_CAPI void U_EXPORT2
udata_write16(UNewDataMemory *pData, uint16_t word) {
if(pData!=NULL && pData->file!=NULL) {
T_FileStream_write(pData->file, &word, 2);
}
}
U_CAPI void U_EXPORT2
udata_write32(UNewDataMemory *pData, uint32_t wyde) {
if(pData!=NULL && pData->file!=NULL) {
T_FileStream_write(pData->file, &wyde, 4);
}
}
U_CAPI void U_EXPORT2
udata_writeBlock(UNewDataMemory *pData, const void *s, UTextOffset length) {
if(pData!=NULL && pData->file!=NULL) {
if(length>0) {
T_FileStream_write(pData->file, s, length);
}
}
}
U_CAPI void U_EXPORT2
udata_writeString(UNewDataMemory *pData, const char *s, UTextOffset length) {
if(pData!=NULL && pData->file!=NULL) {
if(length==-1) {
length=icu_strlen(s);
}
if(length>0) {
T_FileStream_write(pData->file, s, length);
}
}
}
U_CAPI void U_EXPORT2
udata_writeUString(UNewDataMemory *pData, const UChar *s, UTextOffset length) {
if(pData!=NULL && pData->file!=NULL) {
if(length==-1) {
length=u_strlen(s);
}
if(length>0) {
T_FileStream_write(pData->file, s, length*sizeof(UChar));
}
}
}

View File

@ -0,0 +1,58 @@
/*
*******************************************************************************
* *
* COPYRIGHT: *
* (C) Copyright International Business Machines Corporation, 1999 *
* Licensed Material - Program-Property of IBM - All Rights Reserved. *
* US Government Users Restricted Rights - Use, duplication, or disclosure *
* restricted by GSA ADP Schedule Contract with IBM Corp. *
* *
*******************************************************************************
* file name: unewdata.h
* encoding: US-ASCII
* tab size: 8 (not used)
* indentation:4
*
* created on: 1999oct25
* created by: Markus W. Scherer
*/
#ifndef __UNEWDATA_H__
#define __UNEWDATA_H__
#include "utypes.h"
#include "udata.h"
/* API for writing data -----------------------------------------------------*/
/* forward declaration of the data memory creation type */
typedef struct UNewDataMemory UNewDataMemory;
U_CAPI UNewDataMemory * U_EXPORT2
udata_create(const char *type, const char *name,
const UDataInfo *pInfo,
const char *comment,
UErrorCode *pErrorCode);
U_CAPI uint32_t U_EXPORT2
udata_finish(UNewDataMemory *pData, UErrorCode *pErrorCode);
U_CAPI void U_EXPORT2
udata_write8(UNewDataMemory *pData, uint8_t byte);
U_CAPI void U_EXPORT2
udata_write16(UNewDataMemory *pData, uint16_t word);
U_CAPI void U_EXPORT2
udata_write32(UNewDataMemory *pData, uint32_t wyde);
U_CAPI void U_EXPORT2
udata_writeBlock(UNewDataMemory *pData, const void *s, UTextOffset length);
U_CAPI void U_EXPORT2
udata_writeString(UNewDataMemory *pData, const char *s, UTextOffset length);
U_CAPI void U_EXPORT2
udata_writeUString(UNewDataMemory *pData, const UChar *s, UTextOffset length);
#endif