ICU-4612 move filestrm to toolutil to remove core library dependencies on file I/O

X-SVN-Rev: 18100
This commit is contained in:
Markus Scherer 2005-06-30 17:40:53 +00:00
parent df5fb97ee5
commit a5364d12dd
7 changed files with 82 additions and 39 deletions

View file

@ -76,7 +76,7 @@ brkiter.o brkdict.o ubrk.o dbbi.o dbbi_tbl.o \
rbbi.o rbbidata.o rbbinode.o rbbirb.o rbbiscan.o rbbisetb.o rbbistbl.o rbbitblb.o \
serv.o servnotf.o servls.o servlk.o servlkf.o servrbf.o servslkf.o locutil.o \
uenum.o ustrenum.o uidna.o usprep.o punycode.o \
cwchar.o filestrm.o util.o parsepos.o utrace.o locbased.o
cwchar.o util.o parsepos.o utrace.o locbased.o
STATIC_OBJECTS = $(OBJECTS:.o=.$(STATIC_O))

View file

@ -499,12 +499,6 @@
Outputs="..\..\include\unicode\$(InputFileName)"/>
</FileConfiguration>
</File>
<File
RelativePath=".\filestrm.c">
</File>
<File
RelativePath=".\filestrm.h">
</File>
<File
RelativePath=".\locmap.c">
</File>
@ -1760,14 +1754,16 @@
Name="Release|Win32">
<Tool
Name="VCCustomBuildTool"
CommandLine="copy &quot;$(InputPath)&quot; ..\..\include\unicode"
CommandLine="copy &quot;$(InputPath)&quot; ..\..\include\unicode
"
Outputs="..\..\include\unicode\$(InputFileName)"/>
</FileConfiguration>
<FileConfiguration
Name="Debug|Win32">
<Tool
Name="VCCustomBuildTool"
CommandLine="copy &quot;$(InputPath)&quot; ..\..\include\unicode"
CommandLine="copy &quot;$(InputPath)&quot; ..\..\include\unicode
"
Outputs="..\..\include\unicode\$(InputFileName)"/>
</FileConfiguration>
</File>

View file

@ -21,16 +21,44 @@
#include "udatamem.h"
#include "umapfile.h"
#if U_ICU_VERSION_MAJOR_NUM>3 || (U_ICU_VERSION_MAJOR_NUM==3 && U_ICU_VERSION_MINOR_NUM>4)
# error Time bomb: After ICU 3.4 move the definition of UCONFIG_NO_FILE_IO to uconfig.h. Use Jitterbug 4614.
#endif
/**
* \def UCONFIG_NO_FILE_IO
* This switch turns off all file access in the common library
* where file access is only used for data loading.
* ICU data must then be provided in the form of a data DLL (or with an
* equivalent way to link to the data residing in an executable,
* as in building a combined library with both the common library's code and
* the data), or via udata_setCommonData().
* Application data must be provided via udata_setAppData() or by using
* "open" functions that take pointers to data, for example ucol_openBinary().
*
* File access is not used at all in the i18n library.
*
* File access cannot be turned off for the icuio library or for the ICU
* test suites and ICU tools.
*
* @draft ICU 3.6
*/
#ifndef UCONFIG_NO_FILE_IO
# define UCONFIG_NO_FILE_IO 0
#endif
/* memory-mapping base definitions ------------------------------------------ */
/* MAP_NONE: no memory mapping, no file access at all */
#define MAP_NONE 0
#define MAP_WIN32 1
#define MAP_POSIX 2
#define MAP_FILE_STREAM 3
#define MAP_STDIO 3
#define MAP_390DLL 4
#ifdef U_WINDOWS
#if UCONFIG_NO_FILE_IO
# define MAP_IMPLEMENTATION MAP_NONE
#elif defined(U_WINDOWS)
# define WIN32_LEAN_AND_MEAN
# define VC_EXTRALEAN
# define NOUSER
@ -81,16 +109,16 @@
# define MAP_IMPLEMENTATION MAP_POSIX
# endif
#else /* unknown platform, no memory map implementation: use FileStream/uprv_malloc() instead */
#else /* unknown platform, no memory map implementation: use stdio.h and uprv_malloc() instead */
# include "filestrm.h"
# include <stdio.h>
# include "cmemory.h"
typedef void *MemoryMap;
# define IS_MAP(map) ((map)!=NULL)
# define MAP_IMPLEMENTATION MAP_FILE_STREAM
# define MAP_IMPLEMENTATION MAP_STDIO
#endif
@ -103,7 +131,17 @@
* functions used by the rest of the implementation.*
* *
*----------------------------------------------------------------------------*/
#if MAP_IMPLEMENTATION==MAP_WIN32
#if MAP_IMPLEMENTATION==MAP_NONE
UBool
uprv_mapFile(UDataMemory *pData, const char *path) {
UDataMemory_init(pData); /* Clear the output struct. */
return FALSE; /* no file access */
}
void uprv_unmapFile(UDataMemory *pData) {
/* nothing to do */
}
#elif MAP_IMPLEMENTATION==MAP_WIN32
UBool
uprv_mapFile(
UDataMemory *pData, /* Fill in with info on the result doing the mapping. */
@ -209,42 +247,56 @@
#elif MAP_IMPLEMENTATION==MAP_FILE_STREAM
#elif MAP_IMPLEMENTATION==MAP_STDIO
/* copy of the filestrm.c/T_FileStream_size() implementation */
static int32_t
umap_fsize(FILE *f) {
int32_t savedPos = ftell(f);
int32_t size = 0;
/*Changes by Bertrand A. D. doesn't affect the current position
goes to the end of the file before ftell*/
fseek(f, 0, SEEK_END);
size = (int32_t)ftell(f);
fseek(f, savedPos, SEEK_SET);
return size;
}
UBool
uprv_mapFile(UDataMemory *pData, const char *path) {
FileStream *file;
FILE *file;
int32_t fileLength;
void *p;
UDataMemory_init(pData); /* Clear the output struct. */
/* open the input file */
file=T_FileStream_open(path, "rb");
file=fopen(path, "rb");
if(file==NULL) {
return FALSE;
}
/* get the file length */
fileLength=T_FileStream_size(file);
if(T_FileStream_error(file) || fileLength<=20) {
T_FileStream_close(file);
fileLength=umap_fsize(file);
if(ferror(file) || fileLength<=20) {
fclose(file);
return FALSE;
}
/* allocate the memory to hold the file data */
p=uprv_malloc(fileLength);
if(p==NULL) {
T_FileStream_close(file);
fclose(file);
return FALSE;
}
/* read the file */
if(fileLength!=T_FileStream_read(file, p, fileLength)) {
if(fileLength!=fread(p, 1, fileLength, file)) {
uprv_free(p);
T_FileStream_close(file);
fclose(file);
return FALSE;
}
T_FileStream_close(file);
fclose(file);
pData->map=p;
pData->pHeader=(const DataHeader *)p;
pData->mapAddr=p;
@ -446,5 +498,3 @@
#else
# error MAP_IMPLEMENTATION is set incorrectly
#endif

View file

@ -42,7 +42,7 @@ CPPFLAGS += -I$(top_builddir)/common -I$(top_srcdir)/common -I$(top_srcdir)/i18n
DEFS += -DU_TOOLUTIL_IMPLEMENTATION
LIBS = $(LIBICUI18N) $(LIBICUUC) $(DEFAULT_LIBS)
OBJECTS = propsvec.o toolutil.o unewdata.o ucm.o ucmstate.o uoptions.o uparse.o ucbuf.o xmlparser.o writesrc.o
OBJECTS = filestrm.o propsvec.o toolutil.o unewdata.o ucm.o ucmstate.o uoptions.o uparse.o ucbuf.o xmlparser.o writesrc.o
STATIC_OBJECTS = $(OBJECTS:.o=.$(STATIC_O))

View file

@ -23,8 +23,6 @@
#include "filestrm.h"
#if !defined(UCONFIG_NO_FILE_IO) || !UCONFIG_NO_FILE_IO
#include "cmemory.h"
#include <stdio.h>
@ -170,7 +168,7 @@ T_FileStream_size(FileStream* fileStream)
/*Changes by Bertrand A. D. doesn't affect the current position
goes to the end of the file before ftell*/
fseek((FILE*)fileStream, 0, SEEK_END);
size = ftell((FILE*)fileStream);
size = (int32_t)ftell((FILE*)fileStream);
fseek((FILE*)fileStream, savedPos, SEEK_SET);
return size;
}
@ -225,6 +223,3 @@ U_CAPI UBool U_EXPORT2
T_FileStream_remove(const char* fileName){
return (remove(fileName) == 0);
}
#endif /* !UCONFIG_NO_FILE_IO */

View file

@ -27,8 +27,6 @@
#include "unicode/utypes.h"
#if !defined(UCONFIG_NO_FILE_IO) || !UCONFIG_NO_FILE_IO
typedef struct _FileStream FileStream;
U_CAPI FileStream* U_EXPORT2
@ -103,6 +101,4 @@ T_FileStream_stderr(void);
U_CAPI UBool U_EXPORT2
T_FileStream_remove(const char* fileName);
#endif /* !UCONFIG_NO_FILE_IO */
#endif /* _FILESTRM*/

View file

@ -145,6 +145,9 @@
<Filter
Name="Source Files"
Filter="cpp;c;cxx;rc;def;r;odl;idl;hpj;bat">
<File
RelativePath=".\filestrm.c">
</File>
<File
RelativePath=".\propsvec.c">
</File>
@ -179,6 +182,9 @@
<Filter
Name="Header Files"
Filter="h;hpp;hxx;hm;inl">
<File
RelativePath=".\filestrm.h">
</File>
<File
RelativePath=".\propsvec.h">
</File>