diff options
Diffstat (limited to 'intl/icu/source/io')
24 files changed, 7846 insertions, 0 deletions
diff --git a/intl/icu/source/io/Makefile.in b/intl/icu/source/io/Makefile.in new file mode 100644 index 000000000..848432543 --- /dev/null +++ b/intl/icu/source/io/Makefile.in @@ -0,0 +1,172 @@ +# Copyright (C) 2016 and later: Unicode, Inc. and others. +# License & terms of use: http://www.unicode.org/copyright.html +#****************************************************************************** +# +# Copyright (C) 1999-2011, International Business Machines +# Corporation and others. All Rights Reserved. +# +#****************************************************************************** +## Makefile.in for ICU - icuio.so +## Stephen F. Booth + +## Source directory information +srcdir = @srcdir@ +top_srcdir = @top_srcdir@ + +top_builddir = .. + +## All the flags and other definitions are included here. +include $(top_builddir)/icudefs.mk + +## Build directory information +subdir = io + +## Extra files to remove for 'make clean' +CLEANFILES = *~ $(DEPS) $(IMPORT_LIB) $(MIDDLE_IMPORT_LIB) $(FINAL_IMPORT_LIB) + +## Target information + +TARGET_STUBNAME=$(IO_STUBNAME) + +ifneq ($(ENABLE_STATIC),) +TARGET = $(LIBDIR)/$(LIBSICU)$(TARGET_STUBNAME)$(ICULIBSUFFIX).$(A) +endif + +ifneq ($(ENABLE_SHARED),) +SO_TARGET = $(LIBDIR)/$(LIBICU)$(TARGET_STUBNAME)$(ICULIBSUFFIX).$(SO) +ALL_SO_TARGETS = $(SO_TARGET) $(MIDDLE_SO_TARGET) $(FINAL_SO_TARGET) $(SHARED_OBJECT) + +ifeq ($(ENABLE_SO_VERSION_DATA),1) +SO_VERSION_DATA = io.res +endif + +ifeq ($(OS390BATCH),1) +BATCH_TARGET = $(BATCH_IO_TARGET) +BATCH_LIBS = $(BATCH_LIBICUUC) $(BATCH_LIBICUI18N) -lm +endif # OS390BATCH + +endif # ENABLE_SHARED + +ALL_TARGETS = $(TARGET) $(ALL_SO_TARGETS) $(BATCH_TARGET) + +DYNAMICCPPFLAGS = $(SHAREDLIBCPPFLAGS) +DYNAMICCFLAGS = $(SHAREDLIBCFLAGS) +DYNAMICCXXFLAGS = $(SHAREDLIBCXXFLAGS) +CFLAGS += $(LIBCFLAGS) +CXXFLAGS += $(LIBCXXFLAGS) + +CPPFLAGS += -I$(top_srcdir)/common -I$(top_srcdir)/i18n $(LIBCPPFLAGS) $(CPPFLAGSICUIO) +DEFS += -DU_IO_IMPLEMENTATION +LDFLAGS += $(LDFLAGSICUIO) +LIBS = $(LIBICUUC) $(LIBICUI18N) $(DEFAULT_LIBS) + +OBJECTS = locbund.o ufile.o ufmt_cmn.o uprintf.o uprntf_p.o \ +uscanf.o uscanf_p.o ustdio.o sprintf.o sscanf.o \ +ustream.o ucln_io.o + +## Header files to install +HEADERS = $(srcdir)/unicode/*.h + +STATIC_OBJECTS = $(OBJECTS:.o=.$(STATIC_O)) + +DEPS = $(OBJECTS:.o=.d) + +-include Makefile.local + +## List of phony targets +.PHONY : all all-local install install-local clean clean-local \ +distclean distclean-local install-library install-headers dist \ +dist-local check check-local check-exhaustive + +## Clear suffix list +.SUFFIXES : + +## List of standard targets +all: all-local +install: install-local +clean: clean-local +distclean : distclean-local +dist: dist-local +check: all check-local + +check-exhaustive: check + +all-local: $(ALL_TARGETS) + +install-local: install-headers install-library + +install-library: all-local + $(MKINSTALLDIRS) $(DESTDIR)$(libdir) +ifneq ($(ENABLE_STATIC),) + $(INSTALL-L) $(TARGET) $(DESTDIR)$(libdir) +endif +ifneq ($(ENABLE_SHARED),) + $(INSTALL-L) $(FINAL_SO_TARGET) $(DESTDIR)$(libdir) +ifneq ($(FINAL_SO_TARGET),$(SO_TARGET)) + cd $(DESTDIR)$(libdir) && $(RM) $(notdir $(SO_TARGET)) && ln -s $(notdir $(FINAL_SO_TARGET)) $(notdir $(SO_TARGET)) +ifneq ($(FINAL_SO_TARGET),$(MIDDLE_SO_TARGET)) + cd $(DESTDIR)$(libdir) && $(RM) $(notdir $(MIDDLE_SO_TARGET)) && ln -s $(notdir $(FINAL_SO_TARGET)) $(notdir $(MIDDLE_SO_TARGET)) +endif +endif +ifneq ($(IMPORT_LIB_EXT),) + $(INSTALL-L) $(FINAL_IMPORT_LIB) $(DESTDIR)$(libdir) +ifneq ($(IMPORT_LIB),$(FINAL_IMPORT_LIB)) + cd $(DESTDIR)$(libdir) && $(RM) $(notdir $(IMPORT_LIB)) && ln -s $(notdir $(FINAL_IMPORT_LIB)) $(notdir $(IMPORT_LIB)) +endif +ifneq ($(MIDDLE_IMPORT_LIB),$(FINAL_IMPORT_LIB)) + cd $(DESTDIR)$(libdir) && $(RM) $(notdir $(MIDDLE_IMPORT_LIB)) && ln -s $(notdir $(FINAL_IMPORT_LIB)) $(notdir $(MIDDLE_IMPORT_LIB)) +endif +endif +endif + +install-headers: + $(MKINSTALLDIRS) $(DESTDIR)$(includedir)/unicode + @for file in $(HEADERS); do \ + echo "$(INSTALL_DATA) $$file $(DESTDIR)$(includedir)/unicode"; \ + $(INSTALL_DATA) $$file $(DESTDIR)$(includedir)/unicode || exit; \ + done + +dist-local: + +clean-local: + test -z "$(CLEANFILES)" || $(RMV) $(CLEANFILES) + $(RMV) $(OBJECTS) $(STATIC_OBJECTS) $(ALL_TARGETS) $(SO_VERSION_DATA) + +distclean-local: clean-local + $(RMV) Makefile + +check-local: + +Makefile: $(srcdir)/Makefile.in $(top_builddir)/config.status + cd $(top_builddir) \ + && CONFIG_FILES=$(subdir)/$@ CONFIG_HEADERS= $(SHELL) ./config.status + +ifneq ($(ENABLE_STATIC),) +$(TARGET): $(STATIC_OBJECTS) + $(AR) $(ARFLAGS) $(AR_OUTOPT)$@ $^ + $(RANLIB) $@ +endif + +ifneq ($(ENABLE_SHARED),) +$(SHARED_OBJECT): $(OBJECTS) $(SO_VERSION_DATA) + $(SHLIB.cc) $(LD_SONAME) $(OUTOPT)$@ $^ $(LIBS) +ifeq ($(ENABLE_RPATH),YES) +ifneq ($(wildcard $(libdir)/$(MIDDLE_SO_TARGET)),) + $(warning RPATH warning: --enable-rpath means test programs may use existing $(libdir)/$(MIDDLE_SO_TARGET)) +endif +endif + +ifeq ($(OS390BATCH),1) +$(BATCH_TARGET):$(OBJECTS) + $(SHLIB.cc) $(LD_SONAME) $(OUTOPT)$@ $^ $(BATCH_LIBS) +endif # OS390BATCH +endif # ENABLE_SHARED + +ifeq (,$(MAKECMDGOALS)) +-include $(DEPS) +else +ifneq ($(patsubst %clean,,$(MAKECMDGOALS)),) +-include $(DEPS) +endif +endif + diff --git a/intl/icu/source/io/io.rc b/intl/icu/source/io/io.rc new file mode 100644 index 000000000..b3c750b5f --- /dev/null +++ b/intl/icu/source/io/io.rc @@ -0,0 +1,110 @@ +// Do not edit with Microsoft Developer Studio Resource Editor. +// It will permanently substitute version numbers that are intended to be +// picked up by the pre-processor during each build. +// Copyright (C) 2016 and later: Unicode, Inc. and others. +// License & terms of use: http://www.unicode.org/copyright.html +// Copyright (c) 2001-2010 International Business Machines +// Corporation and others. All Rights Reserved. +// +#include "../common/msvcres.h" + +#define APSTUDIO_READONLY_SYMBOLS +///////////////////////////////////////////////////////////////////////////// +// +// Generated from the TEXTINCLUDE 2 resource. +// +#include <winresrc.h> +///////////////////////////////////////////////////////////////////////////// +#undef APSTUDIO_READONLY_SYMBOLS + +///////////////////////////////////////////////////////////////////////////// +// + +LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL +#pragma code_page(1252) + +#ifdef APSTUDIO_INVOKED +///////////////////////////////////////////////////////////////////////////// +// +// TEXTINCLUDE +// + +1 TEXTINCLUDE +BEGIN + "../../common/msvcres.h\0" +END + +2 TEXTINCLUDE +BEGIN + "#include <winresrc.h>\0" +END + +3 TEXTINCLUDE +BEGIN + "\r\n" + "\0" +END + +#endif // APSTUDIO_INVOKED + + +///////////////////////////////////////////////////////////////////////////// +// +// Version +// +#define STR(s) #s +#define CommaVersionString(a, b, c, d) STR(a) ", " STR(b) ", " STR(c) ", " STR(d) "\0" + +VS_VERSION_INFO VERSIONINFO + FILEVERSION U_ICU_VERSION_MAJOR_NUM, U_ICU_VERSION_MINOR_NUM, U_ICU_VERSION_PATCHLEVEL_NUM, U_ICU_VERSION_BUILDLEVEL_NUM + PRODUCTVERSION U_ICU_VERSION_MAJOR_NUM, U_ICU_VERSION_MINOR_NUM, U_ICU_VERSION_PATCHLEVEL_NUM, U_ICU_VERSION_BUILDLEVEL_NUM + FILEFLAGSMASK 0x3fL +#ifdef _DEBUG + FILEFLAGS 0x1L +#else + FILEFLAGS 0x0L +#endif + FILEOS VOS__WINDOWS32 + FILETYPE VFT_DLL + FILESUBTYPE 0x0L +BEGIN + BLOCK "StringFileInfo" + BEGIN + BLOCK "00000000" + BEGIN + VALUE "Comments", ICU_WEBSITE "\0" + VALUE "CompanyName", ICU_COMPANY "\0" + VALUE "FileDescription", ICU_PRODUCT_PREFIX " I/O DLL\0" + VALUE "FileVersion", CommaVersionString(U_ICU_VERSION_MAJOR_NUM, U_ICU_VERSION_MINOR_NUM, U_ICU_VERSION_PATCHLEVEL_NUM, U_ICU_VERSION_BUILDLEVEL_NUM) + VALUE "LegalCopyright", U_COPYRIGHT_STRING "\0" +#ifdef _DEBUG + VALUE "OriginalFilename", "icuio" U_ICU_VERSION_SHORT "d.dll\0" +#else + VALUE "OriginalFilename", "icuio" U_ICU_VERSION_SHORT ".dll\0" +#endif + VALUE "PrivateBuild", "\0" + VALUE "ProductName", ICU_PRODUCT "\0" + VALUE "ProductVersion", CommaVersionString(U_ICU_VERSION_MAJOR_NUM, U_ICU_VERSION_MINOR_NUM, U_ICU_VERSION_PATCHLEVEL_NUM, U_ICU_VERSION_BUILDLEVEL_NUM) + VALUE "SpecialBuild", "\0" + END + END + BLOCK "VarFileInfo" + BEGIN + VALUE "Translation", 0x000, 0000 + END +END + +///////////////////////////////////////////////////////////////////////////// + + + +#ifndef APSTUDIO_INVOKED +///////////////////////////////////////////////////////////////////////////// +// +// Generated from the TEXTINCLUDE 3 resource. +// + + +///////////////////////////////////////////////////////////////////////////// +#endif // not APSTUDIO_INVOKED + diff --git a/intl/icu/source/io/io.vcxproj b/intl/icu/source/io/io.vcxproj new file mode 100644 index 000000000..3173e9208 --- /dev/null +++ b/intl/icu/source/io/io.vcxproj @@ -0,0 +1,323 @@ +<?xml version="1.0" encoding="utf-8"?> +<Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> + <ItemGroup Label="ProjectConfigurations"> + <ProjectConfiguration Include="Debug|Win32"> + <Configuration>Debug</Configuration> + <Platform>Win32</Platform> + </ProjectConfiguration> + <ProjectConfiguration Include="Debug|x64"> + <Configuration>Debug</Configuration> + <Platform>x64</Platform> + </ProjectConfiguration> + <ProjectConfiguration Include="Release|Win32"> + <Configuration>Release</Configuration> + <Platform>Win32</Platform> + </ProjectConfiguration> + <ProjectConfiguration Include="Release|x64"> + <Configuration>Release</Configuration> + <Platform>x64</Platform> + </ProjectConfiguration> + </ItemGroup> + <PropertyGroup Label="Globals"> + <ProjectGuid>{C2B04507-2521-4801-BF0D-5FD79D6D518C}</ProjectGuid> + </PropertyGroup> + <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /> + <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration"> + <ConfigurationType>DynamicLibrary</ConfigurationType> + <UseOfMfc>false</UseOfMfc> + <CharacterSet>MultiByte</CharacterSet> + <PlatformToolset>v140</PlatformToolset> + </PropertyGroup> + <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration"> + <ConfigurationType>DynamicLibrary</ConfigurationType> + <UseOfMfc>false</UseOfMfc> + <CharacterSet>MultiByte</CharacterSet> + <PlatformToolset>v140</PlatformToolset> + </PropertyGroup> + <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration"> + <ConfigurationType>DynamicLibrary</ConfigurationType> + <UseOfMfc>false</UseOfMfc> + <CharacterSet>MultiByte</CharacterSet> + <PlatformToolset>v140</PlatformToolset> + </PropertyGroup> + <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration"> + <ConfigurationType>DynamicLibrary</ConfigurationType> + <UseOfMfc>false</UseOfMfc> + <CharacterSet>MultiByte</CharacterSet> + <PlatformToolset>v140</PlatformToolset> + </PropertyGroup> + <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" /> + <ImportGroup Label="ExtensionSettings"> + </ImportGroup> + <ImportGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="PropertySheets"> + <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> + <Import Project="$(VCTargetsPath)Microsoft.CPP.UpgradeFromVC71.props" /> + </ImportGroup> + <ImportGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="PropertySheets"> + <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> + <Import Project="$(VCTargetsPath)Microsoft.CPP.UpgradeFromVC71.props" /> + </ImportGroup> + <ImportGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="PropertySheets"> + <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> + <Import Project="$(VCTargetsPath)Microsoft.CPP.UpgradeFromVC71.props" /> + </ImportGroup> + <ImportGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="PropertySheets"> + <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> + <Import Project="$(VCTargetsPath)Microsoft.CPP.UpgradeFromVC71.props" /> + </ImportGroup> + <PropertyGroup Label="UserMacros" /> + <PropertyGroup> + <_ProjectFileVersion>10.0.30319.1</_ProjectFileVersion> + <OutDir Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">.\..\..\lib\</OutDir> + <IntDir Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">.\x86\Release\</IntDir> + <LinkIncremental Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">false</LinkIncremental> + <OutDir Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">.\..\..\lib\</OutDir> + <IntDir Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">.\x86\Debug\</IntDir> + <LinkIncremental Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</LinkIncremental> + <OutDir Condition="'$(Configuration)|$(Platform)'=='Release|x64'">.\x64\Release\</OutDir> + <IntDir Condition="'$(Configuration)|$(Platform)'=='Release|x64'">.\x64\Release\</IntDir> + <LinkIncremental Condition="'$(Configuration)|$(Platform)'=='Release|x64'">false</LinkIncremental> + <OutDir Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">.\x64\Debug\</OutDir> + <IntDir Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">.\x64\Debug\</IntDir> + <LinkIncremental Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</LinkIncremental> + </PropertyGroup> + <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> + <Midl> + <PreprocessorDefinitions>NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions> + <MkTypLibCompatible>true</MkTypLibCompatible> + <SuppressStartupBanner>true</SuppressStartupBanner> + <TargetEnvironment>Win32</TargetEnvironment> + <TypeLibraryName>.\..\..\lib\icuio.tlb</TypeLibraryName> + </Midl> + <ClCompile> + <AdditionalIncludeDirectories>..\..\include;..\common;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> + <PreprocessorDefinitions>WIN32;NDEBUG;_CRT_SECURE_NO_DEPRECATE;U_IO_IMPLEMENTATION;%(PreprocessorDefinitions)</PreprocessorDefinitions> + <StringPooling>true</StringPooling> + <RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary> + <FunctionLevelLinking>true</FunctionLevelLinking> + <DisableLanguageExtensions>false</DisableLanguageExtensions> + <TreatWChar_tAsBuiltInType>true</TreatWChar_tAsBuiltInType> + <PrecompiledHeaderOutputFile>.\x86\Release/icuio.pch</PrecompiledHeaderOutputFile> + <AssemblerListingLocation>.\x86\Release/</AssemblerListingLocation> + <ObjectFileName>.\x86\Release/</ObjectFileName> + <ProgramDataBaseFileName>.\x86\Release/</ProgramDataBaseFileName> + <WarningLevel>Level3</WarningLevel> + <SuppressStartupBanner>true</SuppressStartupBanner> + <CompileAs>Default</CompileAs> + </ClCompile> + <ResourceCompile> + <PreprocessorDefinitions>NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions> + <Culture>0x0409</Culture> + <AdditionalIncludeDirectories>..\common;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> + </ResourceCompile> + <Link> + <OutputFile>..\..\bin\icuio58.dll</OutputFile> + <SuppressStartupBanner>true</SuppressStartupBanner> + <ProgramDatabaseFile>.\..\..\lib\icuio.pdb</ProgramDatabaseFile> + <EnableCOMDATFolding>true</EnableCOMDATFolding> + <BaseAddress>0x4ab00000</BaseAddress> + <RandomizedBaseAddress>false</RandomizedBaseAddress> + <DataExecutionPrevention> + </DataExecutionPrevention> + <ImportLibrary>..\..\lib\icuio.lib</ImportLibrary> + </Link> + </ItemDefinitionGroup> + <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> + <Midl> + <PreprocessorDefinitions>_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions> + <MkTypLibCompatible>true</MkTypLibCompatible> + <SuppressStartupBanner>true</SuppressStartupBanner> + <TargetEnvironment>Win32</TargetEnvironment> + <TypeLibraryName>.\..\..\lib\icuio.tlb</TypeLibraryName> + </Midl> + <ClCompile> + <Optimization>Disabled</Optimization> + <AdditionalIncludeDirectories>..\..\include;..\common;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> + <PreprocessorDefinitions>WIN32;_DEBUG;_CRT_SECURE_NO_DEPRECATE;U_IO_IMPLEMENTATION;%(PreprocessorDefinitions)</PreprocessorDefinitions> + <BasicRuntimeChecks>EnableFastChecks</BasicRuntimeChecks> + <RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary> + <BufferSecurityCheck>true</BufferSecurityCheck> + <DisableLanguageExtensions>false</DisableLanguageExtensions> + <TreatWChar_tAsBuiltInType>true</TreatWChar_tAsBuiltInType> + <PrecompiledHeaderOutputFile>.\x86\Debug/icuio.pch</PrecompiledHeaderOutputFile> + <AssemblerListingLocation>.\x86\Debug/</AssemblerListingLocation> + <ObjectFileName>.\x86\Debug/</ObjectFileName> + <ProgramDataBaseFileName>.\x86\Debug/</ProgramDataBaseFileName> + <BrowseInformation>true</BrowseInformation> + <WarningLevel>Level3</WarningLevel> + <SuppressStartupBanner>true</SuppressStartupBanner> + <DebugInformationFormat>EditAndContinue</DebugInformationFormat> + <CompileAs>Default</CompileAs> + </ClCompile> + <ResourceCompile> + <PreprocessorDefinitions>_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions> + <Culture>0x0409</Culture> + <AdditionalIncludeDirectories>..\common;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> + </ResourceCompile> + <Link> + <OutputFile>..\..\bin\icuio58d.dll</OutputFile> + <SuppressStartupBanner>true</SuppressStartupBanner> + <GenerateDebugInformation>true</GenerateDebugInformation> + <ProgramDatabaseFile>.\..\..\lib\icuiod.pdb</ProgramDatabaseFile> + <BaseAddress>0x4ab00000</BaseAddress> + <RandomizedBaseAddress>false</RandomizedBaseAddress> + <DataExecutionPrevention> + </DataExecutionPrevention> + <ImportLibrary>..\..\lib\icuiod.lib</ImportLibrary> + </Link> + </ItemDefinitionGroup> + <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> + <Midl> + <PreprocessorDefinitions>NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions> + <MkTypLibCompatible>true</MkTypLibCompatible> + <SuppressStartupBanner>true</SuppressStartupBanner> + <TargetEnvironment>X64</TargetEnvironment> + <TypeLibraryName>.\..\..\lib64\icuio.tlb</TypeLibraryName> + </Midl> + <ClCompile> + <AdditionalIncludeDirectories>..\..\include;..\common;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> + <PreprocessorDefinitions>WIN64;WIN32;NDEBUG;_CRT_SECURE_NO_DEPRECATE;U_IO_IMPLEMENTATION;%(PreprocessorDefinitions)</PreprocessorDefinitions> + <StringPooling>true</StringPooling> + <RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary> + <FunctionLevelLinking>true</FunctionLevelLinking> + <DisableLanguageExtensions>false</DisableLanguageExtensions> + <TreatWChar_tAsBuiltInType>true</TreatWChar_tAsBuiltInType> + <PrecompiledHeaderOutputFile>.\x64\Release/icuio.pch</PrecompiledHeaderOutputFile> + <AssemblerListingLocation>.\x64\Release/</AssemblerListingLocation> + <ObjectFileName>.\x64\Release/</ObjectFileName> + <ProgramDataBaseFileName>.\x64\Release/</ProgramDataBaseFileName> + <WarningLevel>Level3</WarningLevel> + <SuppressStartupBanner>true</SuppressStartupBanner> + <CompileAs>Default</CompileAs> + </ClCompile> + <ResourceCompile> + <PreprocessorDefinitions>NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions> + <Culture>0x0409</Culture> + <AdditionalIncludeDirectories>..\common;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> + </ResourceCompile> + <Link> + <OutputFile>..\..\bin64\icuio58.dll</OutputFile> + <SuppressStartupBanner>true</SuppressStartupBanner> + <ProgramDatabaseFile>.\..\..\lib64\icuio.pdb</ProgramDatabaseFile> + <EnableCOMDATFolding>true</EnableCOMDATFolding> + <BaseAddress>0x4ab00000</BaseAddress> + <ImportLibrary>..\..\lib64\icuio.lib</ImportLibrary> + <TargetMachine>MachineX64</TargetMachine> + </Link> + </ItemDefinitionGroup> + <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> + <Midl> + <PreprocessorDefinitions>_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions> + <MkTypLibCompatible>true</MkTypLibCompatible> + <SuppressStartupBanner>true</SuppressStartupBanner> + <TargetEnvironment>X64</TargetEnvironment> + <TypeLibraryName>.\..\..\lib64\icuio.tlb</TypeLibraryName> + </Midl> + <ClCompile> + <Optimization>Disabled</Optimization> + <AdditionalIncludeDirectories>..\..\include;..\common;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> + <PreprocessorDefinitions>WIN64;WIN32;_DEBUG;_CRT_SECURE_NO_DEPRECATE;U_IO_IMPLEMENTATION;%(PreprocessorDefinitions)</PreprocessorDefinitions> + <BasicRuntimeChecks>EnableFastChecks</BasicRuntimeChecks> + <RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary> + <BufferSecurityCheck>true</BufferSecurityCheck> + <DisableLanguageExtensions>false</DisableLanguageExtensions> + <TreatWChar_tAsBuiltInType>true</TreatWChar_tAsBuiltInType> + <PrecompiledHeaderOutputFile>.\x64\Debug/icuio.pch</PrecompiledHeaderOutputFile> + <AssemblerListingLocation>.\x64\Debug/</AssemblerListingLocation> + <ObjectFileName>.\x64\Debug/</ObjectFileName> + <ProgramDataBaseFileName>.\x64\Debug/</ProgramDataBaseFileName> + <BrowseInformation>true</BrowseInformation> + <WarningLevel>Level3</WarningLevel> + <SuppressStartupBanner>true</SuppressStartupBanner> + <DebugInformationFormat>ProgramDatabase</DebugInformationFormat> + <CompileAs>Default</CompileAs> + </ClCompile> + <ResourceCompile> + <PreprocessorDefinitions>_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions> + <Culture>0x0409</Culture> + <AdditionalIncludeDirectories>..\common;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> + </ResourceCompile> + <Link> + <OutputFile>..\..\bin64\icuio58d.dll</OutputFile> + <SuppressStartupBanner>true</SuppressStartupBanner> + <GenerateDebugInformation>true</GenerateDebugInformation> + <ProgramDatabaseFile>.\..\..\lib64\icuiod.pdb</ProgramDatabaseFile> + <BaseAddress>0x4ab00000</BaseAddress> + <ImportLibrary>..\..\lib64\icuiod.lib</ImportLibrary> + <TargetMachine>MachineX64</TargetMachine> + </Link> + </ItemDefinitionGroup> + <ItemGroup> + <ClCompile Include="locbund.cpp" /> + <ClCompile Include="sprintf.c" /> + <ClCompile Include="sscanf.c" /> + <ClCompile Include="ucln_io.cpp"> + <DisableLanguageExtensions>false</DisableLanguageExtensions> + </ClCompile> + <ClCompile Include="ufile.c" /> + <ClCompile Include="ufmt_cmn.c" /> + <ClCompile Include="uprintf.cpp" /> + <ClCompile Include="uprntf_p.c" /> + <ClCompile Include="uscanf.c" /> + <ClCompile Include="uscanf_p.c" /> + <ClCompile Include="ustdio.c" /> + <ClCompile Include="ustream.cpp"> + <DisableLanguageExtensions Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">false</DisableLanguageExtensions> + <DisableLanguageExtensions Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">false</DisableLanguageExtensions> + <DisableLanguageExtensions Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">false</DisableLanguageExtensions> + <DisableLanguageExtensions Condition="'$(Configuration)|$(Platform)'=='Release|x64'">false</DisableLanguageExtensions> + </ClCompile> + </ItemGroup> + <ItemGroup> + <ClInclude Include="locbund.h" /> + <ClInclude Include="ucln_io.h" /> + <ClInclude Include="ufile.h" /> + <ClInclude Include="ufmt_cmn.h" /> + <ClInclude Include="uprintf.h" /> + <ClInclude Include="uscanf.h" /> + <CustomBuild Include="unicode\ustdio.h"> + <Command Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">copy "%(FullPath)" ..\..\include\unicode +</Command> + <Outputs Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">..\..\include\unicode\%(Filename)%(Extension);%(Outputs)</Outputs> + <Command Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">copy "%(FullPath)" ..\..\include\unicode +</Command> + <Outputs Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">..\..\include\unicode\%(Filename)%(Extension);%(Outputs)</Outputs> + <Command Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">copy "%(FullPath)" ..\..\include\unicode +</Command> + <Outputs Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">..\..\include\unicode\%(Filename)%(Extension);%(Outputs)</Outputs> + <Command Condition="'$(Configuration)|$(Platform)'=='Release|x64'">copy "%(FullPath)" ..\..\include\unicode +</Command> + <Outputs Condition="'$(Configuration)|$(Platform)'=='Release|x64'">..\..\include\unicode\%(Filename)%(Extension);%(Outputs)</Outputs> + </CustomBuild> + <CustomBuild Include="unicode\ustream.h"> + <Command Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">copy "%(FullPath)" ..\..\include\unicode +</Command> + <Outputs Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">..\..\include\unicode\%(Filename)%(Extension);%(Outputs)</Outputs> + <Command Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">copy "%(FullPath)" ..\..\include\unicode +</Command> + <Outputs Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">..\..\include\unicode\%(Filename)%(Extension);%(Outputs)</Outputs> + <Command Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">copy "%(FullPath)" ..\..\include\unicode +</Command> + <Outputs Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">..\..\include\unicode\%(Filename)%(Extension);%(Outputs)</Outputs> + <Command Condition="'$(Configuration)|$(Platform)'=='Release|x64'">copy "%(FullPath)" ..\..\include\unicode +</Command> + <Outputs Condition="'$(Configuration)|$(Platform)'=='Release|x64'">..\..\include\unicode\%(Filename)%(Extension);%(Outputs)</Outputs> + </CustomBuild> + </ItemGroup> + <ItemGroup> + <ResourceCompile Include="io.rc" /> + </ItemGroup> + <ItemGroup> + <ProjectReference Include="..\common\common.vcxproj"> + <Project>{73c0a65b-d1f2-4de1-b3a6-15dad2c23f3d}</Project> + <ReferenceOutputAssembly>false</ReferenceOutputAssembly> + </ProjectReference> + <ProjectReference Include="..\i18n\i18n.vcxproj"> + <Project>{0178b127-6269-407d-b112-93877bb62776}</Project> + <ReferenceOutputAssembly>false</ReferenceOutputAssembly> + </ProjectReference> + </ItemGroup> + <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> + <ImportGroup Label="ExtensionTargets"> + </ImportGroup> +</Project>
\ No newline at end of file diff --git a/intl/icu/source/io/io.vcxproj.filters b/intl/icu/source/io/io.vcxproj.filters new file mode 100644 index 000000000..61252bff3 --- /dev/null +++ b/intl/icu/source/io/io.vcxproj.filters @@ -0,0 +1,88 @@ +<?xml version="1.0" encoding="utf-8"?> +<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> + <ItemGroup> + <Filter Include="Source Files"> + <UniqueIdentifier>{851db102-16a0-4e9d-b6bc-aa5cce5119d6}</UniqueIdentifier> + <Extensions>cpp;c;cxx;rc;def;r;odl;idl;hpj;bat</Extensions> + </Filter> + <Filter Include="Header Files"> + <UniqueIdentifier>{051f18b2-608f-486c-9b00-308b7a429167}</UniqueIdentifier> + <Extensions>h;hpp;hxx;hm;inl</Extensions> + </Filter> + <Filter Include="Resource Files"> + <UniqueIdentifier>{9fd3bb57-16c4-405d-a045-2be622895424}</UniqueIdentifier> + <Extensions>ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe</Extensions> + </Filter> + </ItemGroup> + <ItemGroup> + <ClCompile Include="locbund.cpp"> + <Filter>Source Files</Filter> + </ClCompile> + <ClCompile Include="sprintf.c"> + <Filter>Source Files</Filter> + </ClCompile> + <ClCompile Include="sscanf.c"> + <Filter>Source Files</Filter> + </ClCompile> + <ClCompile Include="ucln_io.cpp"> + <Filter>Source Files</Filter> + </ClCompile> + <ClCompile Include="ufile.c"> + <Filter>Source Files</Filter> + </ClCompile> + <ClCompile Include="ufmt_cmn.c"> + <Filter>Source Files</Filter> + </ClCompile> + <ClCompile Include="uprintf.cpp"> + <Filter>Source Files</Filter> + </ClCompile> + <ClCompile Include="uprntf_p.c"> + <Filter>Source Files</Filter> + </ClCompile> + <ClCompile Include="uscanf.c"> + <Filter>Source Files</Filter> + </ClCompile> + <ClCompile Include="uscanf_p.c"> + <Filter>Source Files</Filter> + </ClCompile> + <ClCompile Include="ustdio.c"> + <Filter>Source Files</Filter> + </ClCompile> + <ClCompile Include="ustream.cpp"> + <Filter>Source Files</Filter> + </ClCompile> + </ItemGroup> + <ItemGroup> + <ClInclude Include="locbund.h"> + <Filter>Header Files</Filter> + </ClInclude> + <ClInclude Include="ucln_io.h"> + <Filter>Header Files</Filter> + </ClInclude> + <ClInclude Include="ufile.h"> + <Filter>Header Files</Filter> + </ClInclude> + <ClInclude Include="ufmt_cmn.h"> + <Filter>Header Files</Filter> + </ClInclude> + <ClInclude Include="uprintf.h"> + <Filter>Header Files</Filter> + </ClInclude> + <ClInclude Include="uscanf.h"> + <Filter>Header Files</Filter> + </ClInclude> + </ItemGroup> + <ItemGroup> + <ResourceCompile Include="io.rc"> + <Filter>Resource Files</Filter> + </ResourceCompile> + </ItemGroup> + <ItemGroup> + <CustomBuild Include="unicode\ustdio.h"> + <Filter>Header Files</Filter> + </CustomBuild> + <CustomBuild Include="unicode\ustream.h"> + <Filter>Header Files</Filter> + </CustomBuild> + </ItemGroup> +</Project> diff --git a/intl/icu/source/io/locbund.cpp b/intl/icu/source/io/locbund.cpp new file mode 100644 index 000000000..298452859 --- /dev/null +++ b/intl/icu/source/io/locbund.cpp @@ -0,0 +1,185 @@ +// Copyright (C) 2016 and later: Unicode, Inc. and others. +// License & terms of use: http://www.unicode.org/copyright.html +/* +******************************************************************************* +* +* Copyright (C) 1998-2014, International Business Machines +* Corporation and others. All Rights Reserved. +* +******************************************************************************* +* +* File locbund.cpp +* +* Modification History: +* +* Date Name Description +* 11/18/98 stephen Creation. +* 12/10/1999 bobbyr(at)optiosoftware.com Fix for memory leak + string allocation bugs +******************************************************************************* +*/ + +#include "unicode/utypes.h" + +#if !UCONFIG_NO_FORMATTING && !UCONFIG_NO_CONVERSION + +#include "locbund.h" + +#include "cmemory.h" +#include "cstring.h" +#include "ucln_io.h" +#include "mutex.h" +#include "umutex.h" +#include "unicode/ustring.h" +#include "unicode/uloc.h" + +static UNumberFormat *gPosixNumberFormat[ULOCALEBUNDLE_NUMBERFORMAT_COUNT]; + +U_CDECL_BEGIN +static UBool U_CALLCONV locbund_cleanup(void) { + int32_t style; + for (style = 0; style < ULOCALEBUNDLE_NUMBERFORMAT_COUNT; style++) { + unum_close(gPosixNumberFormat[style]); + gPosixNumberFormat[style] = NULL; + } + return TRUE; +} +U_CDECL_END + +static UMutex gLock = U_MUTEX_INITIALIZER; +static inline UNumberFormat * copyInvariantFormatter(ULocaleBundle *result, UNumberFormatStyle style) { + U_NAMESPACE_USE + Mutex lock(&gLock); + if (result->fNumberFormat[style-1] == NULL) { + if (gPosixNumberFormat[style-1] == NULL) { + UErrorCode status = U_ZERO_ERROR; + UNumberFormat *formatAlias = unum_open(style, NULL, 0, "en_US_POSIX", NULL, &status); + if (U_SUCCESS(status)) { + gPosixNumberFormat[style-1] = formatAlias; + ucln_io_registerCleanup(UCLN_IO_LOCBUND, locbund_cleanup); + } + } + /* Copy the needed formatter. */ + if (gPosixNumberFormat[style-1] != NULL) { + UErrorCode status = U_ZERO_ERROR; + result->fNumberFormat[style-1] = unum_clone(gPosixNumberFormat[style-1], &status); + } + } + return result->fNumberFormat[style-1]; +} + +U_CAPI ULocaleBundle * +u_locbund_init(ULocaleBundle *result, const char *loc) +{ + int32_t len; + + if(result == 0) + return 0; + + if (loc == NULL) { + loc = uloc_getDefault(); + } + + uprv_memset(result, 0, sizeof(ULocaleBundle)); + + len = (int32_t)strlen(loc); + result->fLocale = (char*) uprv_malloc(len + 1); + if(result->fLocale == 0) { + return 0; + } + + uprv_strcpy(result->fLocale, loc); + + result->isInvariantLocale = uprv_strcmp(result->fLocale, "en_US_POSIX") == 0; + + return result; +} + +/*U_CAPI ULocaleBundle * +u_locbund_new(const char *loc) +{ + ULocaleBundle *result = (ULocaleBundle*) uprv_malloc(sizeof(ULocaleBundle)); + return u_locbund_init(result, loc); +} + +U_CAPI ULocaleBundle * +u_locbund_clone(const ULocaleBundle *bundle) +{ + ULocaleBundle *result = (ULocaleBundle*)uprv_malloc(sizeof(ULocaleBundle)); + UErrorCode status = U_ZERO_ERROR; + int32_t styleIdx; + + if(result == 0) + return 0; + + result->fLocale = (char*) uprv_malloc(strlen(bundle->fLocale) + 1); + if(result->fLocale == 0) { + uprv_free(result); + return 0; + } + + strcpy(result->fLocale, bundle->fLocale ); + + for (styleIdx = 0; styleIdx < ULOCALEBUNDLE_NUMBERFORMAT_COUNT; styleIdx++) { + status = U_ZERO_ERROR; + if (result->fNumberFormat[styleIdx]) { + result->fNumberFormat[styleIdx] = unum_clone(bundle->fNumberFormat[styleIdx], &status); + if (U_FAILURE(status)) { + result->fNumberFormat[styleIdx] = NULL; + } + } + else { + result->fNumberFormat[styleIdx] = NULL; + } + } + result->fDateFormat = (bundle->fDateFormat == 0 ? 0 : + udat_clone(bundle->fDateFormat, &status)); + result->fTimeFormat = (bundle->fTimeFormat == 0 ? 0 : + udat_clone(bundle->fTimeFormat, &status)); + + return result; +}*/ + +U_CAPI void +u_locbund_close(ULocaleBundle *bundle) +{ + int32_t styleIdx; + + uprv_free(bundle->fLocale); + + for (styleIdx = 0; styleIdx < ULOCALEBUNDLE_NUMBERFORMAT_COUNT; styleIdx++) { + if (bundle->fNumberFormat[styleIdx]) { + unum_close(bundle->fNumberFormat[styleIdx]); + } + } + + uprv_memset(bundle, 0, sizeof(ULocaleBundle)); +/* uprv_free(bundle);*/ +} + +U_CAPI UNumberFormat * +u_locbund_getNumberFormat(ULocaleBundle *bundle, UNumberFormatStyle style) +{ + UNumberFormat *formatAlias = NULL; + if (style > UNUM_IGNORE) { + formatAlias = bundle->fNumberFormat[style-1]; + if (formatAlias == NULL) { + if (bundle->isInvariantLocale) { + formatAlias = copyInvariantFormatter(bundle, style); + } + else { + UErrorCode status = U_ZERO_ERROR; + formatAlias = unum_open(style, NULL, 0, bundle->fLocale, NULL, &status); + if (U_FAILURE(status)) { + unum_close(formatAlias); + formatAlias = NULL; + } + else { + bundle->fNumberFormat[style-1] = formatAlias; + } + } + } + } + return formatAlias; +} + +#endif /* #if !UCONFIG_NO_FORMATTING */ diff --git a/intl/icu/source/io/locbund.h b/intl/icu/source/io/locbund.h new file mode 100644 index 000000000..b9e19484b --- /dev/null +++ b/intl/icu/source/io/locbund.h @@ -0,0 +1,82 @@ +// Copyright (C) 2016 and later: Unicode, Inc. and others. +// License & terms of use: http://www.unicode.org/copyright.html +/* +******************************************************************************* +* +* Copyright (C) 1998-2011, International Business Machines +* Corporation and others. All Rights Reserved. +* +******************************************************************************* +* +* File locbund.h +* +* Modification History: +* +* Date Name Description +* 10/16/98 stephen Creation. +* 02/25/99 stephen Modified for new C API. +******************************************************************************* +*/ + +#ifndef LOCBUND_H +#define LOCBUND_H + +#include "unicode/utypes.h" + +#if !UCONFIG_NO_FORMATTING + +#include "unicode/unum.h" + +#define ULOCALEBUNDLE_NUMBERFORMAT_COUNT ((int32_t)UNUM_SPELLOUT) + +typedef struct ULocaleBundle { + char *fLocale; + + UNumberFormat *fNumberFormat[ULOCALEBUNDLE_NUMBERFORMAT_COUNT]; + UBool isInvariantLocale; +} ULocaleBundle; + + +/** + * Initialize a ULocaleBundle, initializing all formatters to 0. + * @param result A ULocaleBundle to initialize. + * @param loc The locale of the ULocaleBundle. + * @return A pointer to a ULocaleBundle, or 0 if <TT>loc</TT> was invalid. + */ +U_CAPI ULocaleBundle * +u_locbund_init(ULocaleBundle *result, const char *loc); + +/** + * Create a new ULocaleBundle, initializing all formatters to 0. + * @param loc The locale of the ULocaleBundle. + * @return A pointer to a ULocaleBundle, or 0 if <TT>loc</TT> was invalid. + */ +/*U_CAPI ULocaleBundle * +u_locbund_new(const char *loc);*/ + +/** + * Create a deep copy of this ULocaleBundle; + * @param bundle The ULocaleBundle to clone. + * @return A new ULocaleBundle. + */ +/*U_CAPI ULocaleBundle * +u_locbund_clone(const ULocaleBundle *bundle);*/ + +/** + * Delete the specified ULocaleBundle, freeing all associated memory. + * @param bundle The ULocaleBundle to delete + */ +U_CAPI void +u_locbund_close(ULocaleBundle *bundle); + +/** + * Get the NumberFormat used to format and parse numbers in a ULocaleBundle. + * @param bundle The ULocaleBundle to use + * @return A pointer to the NumberFormat used for number formatting and parsing. + */ +U_CAPI UNumberFormat * +u_locbund_getNumberFormat(ULocaleBundle *bundle, UNumberFormatStyle style); + +#endif /* #if !UCONFIG_NO_FORMATTING */ + +#endif diff --git a/intl/icu/source/io/sprintf.c b/intl/icu/source/io/sprintf.c new file mode 100644 index 000000000..ff143deb2 --- /dev/null +++ b/intl/icu/source/io/sprintf.c @@ -0,0 +1,262 @@ +// Copyright (C) 2016 and later: Unicode, Inc. and others. +// License & terms of use: http://www.unicode.org/copyright.html +/* +****************************************************************************** +* +* Copyright (C) 2001-2014, International Business Machines +* Corporation and others. All Rights Reserved. +* +****************************************************************************** +* +* File sprintf.c +* +* Modification History: +* +* Date Name Description +* 02/08/2001 george Creation. Copied from uprintf.c +* 03/27/2002 Mark Schneckloth Many fixes regarding alignment, null termination +* (mschneckloth@atomz.com) and other various problems. +* 08/07/2003 george Reunify printf implementations +******************************************************************************* +*/ + +#include "unicode/utypes.h" + +#if !UCONFIG_NO_FORMATTING && !UCONFIG_NO_CONVERSION + +#include "unicode/ustdio.h" +#include "unicode/ustring.h" +#include "unicode/putil.h" + +#include "uprintf.h" +#include "locbund.h" + +#include "cmemory.h" +#include <ctype.h> + +/* u_minstrncpy copies the minimum number of code units of (count or output->available) */ +static int32_t +u_sprintf_write(void *context, + const UChar *str, + int32_t count) +{ + u_localized_print_string *output = (u_localized_print_string *)context; + int32_t size = ufmt_min(count, output->available); + + u_strncpy(output->str + (output->len - output->available), str, size); + output->available -= size; + return size; +} + +static int32_t +u_sprintf_pad_and_justify(void *context, + const u_printf_spec_info *info, + const UChar *result, + int32_t resultLen) +{ + u_localized_print_string *output = (u_localized_print_string *)context; + int32_t written = 0; + int32_t lengthOfResult = resultLen; + + resultLen = ufmt_min(resultLen, output->available); + + /* pad and justify, if needed */ + if(info->fWidth != -1 && resultLen < info->fWidth) { + int32_t paddingLeft = info->fWidth - resultLen; + int32_t outputPos = output->len - output->available; + + if (paddingLeft + resultLen > output->available) { + paddingLeft = output->available - resultLen; + if (paddingLeft < 0) { + paddingLeft = 0; + } + /* paddingLeft = output->available - resultLen;*/ + } + written += paddingLeft; + + /* left justify */ + if(info->fLeft) { + written += u_sprintf_write(output, result, resultLen); + u_memset(&output->str[outputPos + resultLen], info->fPadChar, paddingLeft); + output->available -= paddingLeft; + } + /* right justify */ + else { + u_memset(&output->str[outputPos], info->fPadChar, paddingLeft); + output->available -= paddingLeft; + written += u_sprintf_write(output, result, resultLen); + } + } + /* just write the formatted output */ + else { + written = u_sprintf_write(output, result, resultLen); + } + + if (written >= 0 && lengthOfResult > written) { + return lengthOfResult; + } + + return written; +} + +U_CAPI int32_t U_EXPORT2 +u_sprintf(UChar *buffer, + const char *patternSpecification, + ... ) +{ + va_list ap; + int32_t written; + + va_start(ap, patternSpecification); + written = u_vsnprintf(buffer, INT32_MAX, patternSpecification, ap); + va_end(ap); + + return written; +} + +U_CAPI int32_t U_EXPORT2 +u_sprintf_u(UChar *buffer, + const UChar *patternSpecification, + ... ) +{ + va_list ap; + int32_t written; + + va_start(ap, patternSpecification); + written = u_vsnprintf_u(buffer, INT32_MAX, patternSpecification, ap); + va_end(ap); + + return written; +} + +U_CAPI int32_t U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */ +u_vsprintf(UChar *buffer, + const char *patternSpecification, + va_list ap) +{ + return u_vsnprintf(buffer, INT32_MAX, patternSpecification, ap); +} + +U_CAPI int32_t U_EXPORT2 +u_snprintf(UChar *buffer, + int32_t count, + const char *patternSpecification, + ... ) +{ + va_list ap; + int32_t written; + + va_start(ap, patternSpecification); + written = u_vsnprintf(buffer, count, patternSpecification, ap); + va_end(ap); + + return written; +} + +U_CAPI int32_t U_EXPORT2 +u_snprintf_u(UChar *buffer, + int32_t count, + const UChar *patternSpecification, + ... ) +{ + va_list ap; + int32_t written; + + va_start(ap, patternSpecification); + written = u_vsnprintf_u(buffer, count, patternSpecification, ap); + va_end(ap); + + return written; +} + +U_CAPI int32_t U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */ +u_vsnprintf(UChar *buffer, + int32_t count, + const char *patternSpecification, + va_list ap) +{ + int32_t written; + UChar *pattern; + UChar patBuffer[UFMT_DEFAULT_BUFFER_SIZE]; + int32_t size = (int32_t)strlen(patternSpecification) + 1; + + /* convert from the default codepage to Unicode */ + if (size >= (int32_t)MAX_UCHAR_BUFFER_SIZE(patBuffer)) { + pattern = (UChar *)uprv_malloc(size * sizeof(UChar)); + if(pattern == 0) { + return 0; + } + } + else { + pattern = patBuffer; + } + u_charsToUChars(patternSpecification, pattern, size); + + /* do the work */ + written = u_vsnprintf_u(buffer, count, pattern, ap); + + /* clean up */ + if (pattern != patBuffer) { + uprv_free(pattern); + } + + return written; +} + +U_CAPI int32_t U_EXPORT2 +u_vsprintf_u(UChar *buffer, + const UChar *patternSpecification, + va_list ap) +{ + return u_vsnprintf_u(buffer, INT32_MAX, patternSpecification, ap); +} + +static const u_printf_stream_handler g_sprintf_stream_handler = { + u_sprintf_write, + u_sprintf_pad_and_justify +}; + +U_CAPI int32_t U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */ +u_vsnprintf_u(UChar *buffer, + int32_t count, + const UChar *patternSpecification, + va_list ap) +{ + int32_t written = 0; /* haven't written anything yet */ + int32_t result = 0; /* test the return value of u_printf_parse */ + + u_localized_print_string outStr; + + if (count < 0) { + count = INT32_MAX; + } + + outStr.str = buffer; + outStr.len = count; + outStr.available = count; + + if(u_locbund_init(&outStr.fBundle, "en_US_POSIX") == 0) { + return 0; + } + + /* parse and print the whole format string */ + result = u_printf_parse(&g_sprintf_stream_handler, patternSpecification, &outStr, &outStr, &outStr.fBundle, &written, ap); + + /* Terminate the buffer, if there's room. */ + if (outStr.available > 0) { + buffer[outStr.len - outStr.available] = 0x0000; + } + + /* Release the cloned bundle, if we cloned it. */ + u_locbund_close(&outStr.fBundle); + + /* parsing error */ + if (result < 0) { + return result; + } + /* return # of UChars written */ + return written; +} + +#endif /* #if !UCONFIG_NO_FORMATTING */ + diff --git a/intl/icu/source/io/sscanf.c b/intl/icu/source/io/sscanf.c new file mode 100644 index 000000000..31ad8965f --- /dev/null +++ b/intl/icu/source/io/sscanf.c @@ -0,0 +1,130 @@ +// Copyright (C) 2016 and later: Unicode, Inc. and others. +// License & terms of use: http://www.unicode.org/copyright.html +/* +****************************************************************************** +* +* Copyright (C) 2000-2014, International Business Machines +* Corporation and others. All Rights Reserved. +* +****************************************************************************** +* +* File sscanf.c +* +* Modification History: +* +* Date Name Description +* 02/08/00 george Creation. Copied from uscanf.c +****************************************************************************** +*/ + +#include "unicode/utypes.h" + +#if !UCONFIG_NO_FORMATTING && !UCONFIG_NO_CONVERSION + +#include "unicode/putil.h" +#include "unicode/ustdio.h" +#include "unicode/ustring.h" +#include "uscanf.h" +#include "ufile.h" +#include "ufmt_cmn.h" + +#include "cmemory.h" +#include "cstring.h" + + +U_CAPI int32_t U_EXPORT2 +u_sscanf(const UChar *buffer, + const char *patternSpecification, + ... ) +{ + va_list ap; + int32_t converted; + + va_start(ap, patternSpecification); + converted = u_vsscanf(buffer, patternSpecification, ap); + va_end(ap); + + return converted; +} + +U_CAPI int32_t U_EXPORT2 +u_sscanf_u(const UChar *buffer, + const UChar *patternSpecification, + ... ) +{ + va_list ap; + int32_t converted; + + va_start(ap, patternSpecification); + converted = u_vsscanf_u(buffer, patternSpecification, ap); + va_end(ap); + + return converted; +} + +U_CAPI int32_t U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */ +u_vsscanf(const UChar *buffer, + const char *patternSpecification, + va_list ap) +{ + int32_t converted; + UChar *pattern; + UChar patBuffer[UFMT_DEFAULT_BUFFER_SIZE]; + int32_t size = (int32_t)uprv_strlen(patternSpecification) + 1; + + /* convert from the default codepage to Unicode */ + if (size >= (int32_t)MAX_UCHAR_BUFFER_SIZE(patBuffer)) { + pattern = (UChar *)uprv_malloc(size * sizeof(UChar)); + if(pattern == 0) { + return 0; + } + } + else { + pattern = patBuffer; + } + u_charsToUChars(patternSpecification, pattern, size); + + /* do the work */ + converted = u_vsscanf_u(buffer, pattern, ap); + + /* clean up */ + if (pattern != patBuffer) { + uprv_free(pattern); + } + + return converted; +} + +U_CAPI int32_t U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */ +u_vsscanf_u(const UChar *buffer, + const UChar *patternSpecification, + va_list ap) +{ + int32_t converted; + UFILE inStr; + + inStr.fConverter = NULL; + inStr.fFile = NULL; + inStr.fOwnFile = FALSE; +#if !UCONFIG_NO_TRANSLITERATION + inStr.fTranslit = NULL; +#endif + inStr.fUCBuffer[0] = 0; + inStr.str.fBuffer = (UChar *)buffer; + inStr.str.fPos = (UChar *)buffer; + inStr.str.fLimit = buffer + u_strlen(buffer); + + if(u_locbund_init(&inStr.str.fBundle, "en_US_POSIX") == 0) { + return 0; + } + + converted = u_scanf_parse(&inStr, patternSpecification, ap); + + u_locbund_close(&inStr.str.fBundle); + + /* return # of items converted */ + return converted; +} + +#endif /* #if !UCONFIG_NO_FORMATTING */ + diff --git a/intl/icu/source/io/ucln_io.cpp b/intl/icu/source/io/ucln_io.cpp new file mode 100644 index 000000000..21a0baf58 --- /dev/null +++ b/intl/icu/source/io/ucln_io.cpp @@ -0,0 +1,71 @@ +// Copyright (C) 2016 and later: Unicode, Inc. and others. +// License & terms of use: http://www.unicode.org/copyright.html +/* +****************************************************************************** +* * +* Copyright (C) 2001-2014, International Business Machines * +* Corporation and others. All Rights Reserved. * +* * +****************************************************************************** +* file name: ucln_io.cpp +* encoding: US-ASCII +* tab size: 8 (not used) +* indentation:4 +* +* created on: 2006August11 +* created by: George Rhoten +*/ + +#include "mutex.h" +#include "ucln.h" +#include "ucln_io.h" +#include "uassert.h" + +#ifndef U_IO_IMPLEMENTATION +#error U_IO_IMPLEMENTATION not set - must be set for all ICU source files in io/ - see http://userguide.icu-project.org/howtouseicu +#endif + + +/** Auto-client */ +#define UCLN_TYPE UCLN_IO +#include "ucln_imp.h" + +/* Leave this copyright notice here! It needs to go somewhere in this library. */ +static const char copyright[] = U_COPYRIGHT_STRING; + +static cleanupFunc *gCleanupFunctions[UCLN_IO_COUNT]; + +static UBool U_CALLCONV io_cleanup(void) +{ + int32_t libType = UCLN_IO_START; + + (void)copyright; // Suppress unused variable warning. + while (++libType<UCLN_IO_COUNT) { + if (gCleanupFunctions[libType]) + { + gCleanupFunctions[libType](); + gCleanupFunctions[libType] = NULL; + } + } +#if !UCLN_NO_AUTO_CLEANUP && (defined(UCLN_AUTO_ATEXIT) || defined(UCLN_AUTO_LOCAL)) + ucln_unRegisterAutomaticCleanup(); +#endif + return TRUE; +} + +void ucln_io_registerCleanup(ECleanupIOType type, + cleanupFunc *func) { + U_ASSERT(UCLN_IO_START < type && type < UCLN_IO_COUNT); + { + icu::Mutex m; // See ticket 10295 for discussion. + ucln_registerCleanup(UCLN_IO, io_cleanup); + if (UCLN_IO_START < type && type < UCLN_IO_COUNT) { + gCleanupFunctions[type] = func; + } + } + +#if !UCLN_NO_AUTO_CLEANUP && (defined(UCLN_AUTO_ATEXIT) || defined(UCLN_AUTO_LOCAL)) + ucln_registerAutomaticCleanup(); +#endif +} + diff --git a/intl/icu/source/io/ucln_io.h b/intl/icu/source/io/ucln_io.h new file mode 100644 index 000000000..97d06a12f --- /dev/null +++ b/intl/icu/source/io/ucln_io.h @@ -0,0 +1,40 @@ +// Copyright (C) 2016 and later: Unicode, Inc. and others. +// License & terms of use: http://www.unicode.org/copyright.html +/* +****************************************************************************** +* * +* Copyright (C) 2001-2011, International Business Machines * +* Corporation and others. All Rights Reserved. * +* * +****************************************************************************** +* file name: ucln_io.h +* encoding: US-ASCII +* tab size: 8 (not used) +* indentation:4 +* +* created on: 2006August11 +* created by: George Rhoten +*/ + +#ifndef __UCLN_IO_H__ +#define __UCLN_IO_H__ + +#include "unicode/utypes.h" +#include "ucln.h" + +/* +Please keep the order of enums declared in same order +as the functions are suppose to be called. */ +typedef enum ECleanupIOType { + UCLN_IO_START = -1, + UCLN_IO_LOCBUND, + UCLN_IO_PRINTF, + UCLN_IO_COUNT /* This must be last */ +} ECleanupIOType; + +/* Main library cleanup registration function. */ +/* See common/ucln.h for details on adding a cleanup function. */ +U_CFUNC void U_EXPORT2 ucln_io_registerCleanup(ECleanupIOType type, + cleanupFunc *func); + +#endif diff --git a/intl/icu/source/io/ufile.c b/intl/icu/source/io/ufile.c new file mode 100644 index 000000000..87ed3f1e9 --- /dev/null +++ b/intl/icu/source/io/ufile.c @@ -0,0 +1,362 @@ +// Copyright (C) 2016 and later: Unicode, Inc. and others. +// License & terms of use: http://www.unicode.org/copyright.html +/* +****************************************************************************** +* +* Copyright (C) 1998-2015, International Business Machines +* Corporation and others. All Rights Reserved. +* +****************************************************************************** +* +* File ufile.c +* +* Modification History: +* +* Date Name Description +* 11/19/98 stephen Creation. +* 03/12/99 stephen Modified for new C API. +* 06/16/99 stephen Changed T_LocaleBundle to u_locbund +* 07/19/99 stephen Fixed to use ucnv's default codepage. +****************************************************************************** +*/ + +/* + * fileno is not declared when building with GCC in strict mode. + */ +#if defined(__GNUC__) && defined(__STRICT_ANSI__) +#undef __STRICT_ANSI__ +#endif + +#include "locmap.h" +#include "unicode/ustdio.h" + +#if !UCONFIG_NO_CONVERSION + +#include "ufile.h" +#include "unicode/uloc.h" +#include "unicode/ures.h" +#include "unicode/ucnv.h" +#include "unicode/ustring.h" +#include "cstring.h" +#include "cmemory.h" + +#if U_PLATFORM_USES_ONLY_WIN32_API && !defined(fileno) +/* Windows likes to rename Unix-like functions */ +#define fileno _fileno +#endif + +static UFILE* +finit_owner(FILE *f, + const char *locale, + const char *codepage, + UBool takeOwnership + ) +{ + UErrorCode status = U_ZERO_ERROR; + UFILE *result; + if(f == NULL) { + return 0; + } + result = (UFILE*) uprv_malloc(sizeof(UFILE)); + if(result == NULL) { + return 0; + } + + uprv_memset(result, 0, sizeof(UFILE)); + result->fFileno = fileno(f); + +#if U_PLATFORM_USES_ONLY_WIN32_API && _MSC_VER < 1900 + /* + * Below is a very old workaround (ICU ticket:231). + * + * Previously, 'FILE*' from inside and outside ICU's DLL + * were different, because they pointed into local copies + * of the io block. At least by VS 2015 the implementation + * is something like: + * stdio = _acrt_iob_func(0) + * .. which is a function call, so should return the same pointer + * regardless of call site. + * As of _MSC_VER 1900 this patch is retired, at 16 years old. + */ + if (0 <= result->fFileno && result->fFileno <= 2) { + /* stdin, stdout and stderr need to be special cased for Windows 98 */ +#if _MSC_VER >= 1400 + result->fFile = &__iob_func()[_fileno(f)]; +#else + result->fFile = &_iob[_fileno(f)]; +#endif + } + else +#endif + { + result->fFile = f; + } + + result->str.fBuffer = result->fUCBuffer; + result->str.fPos = result->fUCBuffer; + result->str.fLimit = result->fUCBuffer; + +#if !UCONFIG_NO_FORMATTING + /* if locale is 0, use the default */ + if(u_locbund_init(&result->str.fBundle, locale) == 0) { + /* DO NOT FCLOSE HERE! */ + uprv_free(result); + return 0; + } +#endif + + /* If the codepage is not "" use the ucnv_open default behavior */ + if(codepage == NULL || *codepage != '\0') { + result->fConverter = ucnv_open(codepage, &status); + } + /* else result->fConverter is already memset'd to NULL. */ + + if(U_SUCCESS(status)) { + result->fOwnFile = takeOwnership; + } + else { +#if !UCONFIG_NO_FORMATTING + u_locbund_close(&result->str.fBundle); +#endif + /* DO NOT fclose here!!!!!! */ + uprv_free(result); + result = NULL; + } + + return result; +} + +U_CAPI UFILE* U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */ +u_finit(FILE *f, + const char *locale, + const char *codepage) +{ + return finit_owner(f, locale, codepage, FALSE); +} + +U_CAPI UFILE* U_EXPORT2 +u_fadopt(FILE *f, + const char *locale, + const char *codepage) +{ + return finit_owner(f, locale, codepage, TRUE); +} + +U_CAPI UFILE* U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */ +u_fopen(const char *filename, + const char *perm, + const char *locale, + const char *codepage) +{ + UFILE *result; + FILE *systemFile = fopen(filename, perm); + if(systemFile == 0) { + return 0; + } + + result = finit_owner(systemFile, locale, codepage, TRUE); + + if (!result) { + /* Something bad happened. + Maybe the converter couldn't be opened. */ + fclose(systemFile); + } + + return result; /* not a file leak */ +} + +U_CAPI UFILE* U_EXPORT2 +u_fopen_u(const UChar *filename, + const char *perm, + const char *locale, + const char *codepage) +{ + UFILE *result; + char buffer[256]; + + u_austrcpy(buffer, filename); + + result = u_fopen(buffer, perm, locale, codepage); +#if U_PLATFORM_USES_ONLY_WIN32_API + /* Try Windows API _wfopen if the above fails. */ + if (!result) { + FILE *systemFile = _wfopen(filename, (UChar*)perm); + if (systemFile) { + result = finit_owner(systemFile, locale, codepage, TRUE); + } + if (!result) { + /* Something bad happened. + Maybe the converter couldn't be opened. */ + fclose(systemFile); + } + } +#endif + return result; /* not a file leak */ +} + +U_CAPI UFILE* U_EXPORT2 +u_fstropen(UChar *stringBuf, + int32_t capacity, + const char *locale) +{ + UFILE *result; + + if (capacity < 0) { + return NULL; + } + + result = (UFILE*) uprv_malloc(sizeof(UFILE)); + /* Null pointer test */ + if (result == NULL) { + return NULL; /* Just get out. */ + } + uprv_memset(result, 0, sizeof(UFILE)); + result->str.fBuffer = stringBuf; + result->str.fPos = stringBuf; + result->str.fLimit = stringBuf+capacity; + +#if !UCONFIG_NO_FORMATTING + /* if locale is 0, use the default */ + if(u_locbund_init(&result->str.fBundle, locale) == 0) { + /* DO NOT FCLOSE HERE! */ + uprv_free(result); + return 0; + } +#endif + + return result; +} + +U_CAPI UBool U_EXPORT2 +u_feof(UFILE *f) +{ + UBool endOfBuffer; + if (f == NULL) { + return TRUE; + } + endOfBuffer = (UBool)(f->str.fPos >= f->str.fLimit); + if (f->fFile != NULL) { + return endOfBuffer && feof(f->fFile); + } + return endOfBuffer; +} + +U_CAPI void U_EXPORT2 +u_fflush(UFILE *file) +{ + ufile_flush_translit(file); + ufile_flush_io(file); + if (file->fFile) { + fflush(file->fFile); + } + else if (file->str.fPos < file->str.fLimit) { + *(file->str.fPos++) = 0; + } + /* TODO: flush input */ +} + +U_CAPI void +u_frewind(UFILE *file) +{ + u_fflush(file); + ucnv_reset(file->fConverter); + if (file->fFile) { + rewind(file->fFile); + file->str.fLimit = file->fUCBuffer; + file->str.fPos = file->fUCBuffer; + } + else { + file->str.fPos = file->str.fBuffer; + } +} + +U_CAPI void U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */ +u_fclose(UFILE *file) +{ + if (file) { + u_fflush(file); + ufile_close_translit(file); + + if(file->fOwnFile) + fclose(file->fFile); + +#if !UCONFIG_NO_FORMATTING + u_locbund_close(&file->str.fBundle); +#endif + + ucnv_close(file->fConverter); + uprv_free(file); + } +} + +U_CAPI FILE* U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */ +u_fgetfile( UFILE *f) +{ + return f->fFile; +} + +#if !UCONFIG_NO_FORMATTING + +U_CAPI const char* U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */ +u_fgetlocale( UFILE *file) +{ + return file->str.fBundle.fLocale; +} + +U_CAPI int32_t U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */ +u_fsetlocale(UFILE *file, + const char *locale) +{ + u_locbund_close(&file->str.fBundle); + + return u_locbund_init(&file->str.fBundle, locale) == 0 ? -1 : 0; +} + +#endif + +U_CAPI const char* U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */ +u_fgetcodepage(UFILE *file) +{ + UErrorCode status = U_ZERO_ERROR; + const char *codepage = NULL; + + if (file->fConverter) { + codepage = ucnv_getName(file->fConverter, &status); + if(U_FAILURE(status)) + return 0; + } + return codepage; +} + +U_CAPI int32_t U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */ +u_fsetcodepage( const char *codepage, + UFILE *file) +{ + UErrorCode status = U_ZERO_ERROR; + int32_t retVal = -1; + + /* We use the normal default codepage for this system, and not the one for the locale. */ + if ((file->str.fPos == file->str.fBuffer) && (file->str.fLimit == file->str.fBuffer)) { + ucnv_close(file->fConverter); + file->fConverter = ucnv_open(codepage, &status); + if(U_SUCCESS(status)) { + retVal = 0; + } + } + return retVal; +} + + +U_CAPI UConverter * U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */ +u_fgetConverter(UFILE *file) +{ + return file->fConverter; +} +#if !UCONFIG_NO_FORMATTING +U_CAPI const UNumberFormat* U_EXPORT2 u_fgetNumberFormat(UFILE *file) +{ + return u_locbund_getNumberFormat(&file->str.fBundle, UNUM_DECIMAL); +} +#endif + +#endif diff --git a/intl/icu/source/io/ufile.h b/intl/icu/source/io/ufile.h new file mode 100644 index 000000000..3a1a22335 --- /dev/null +++ b/intl/icu/source/io/ufile.h @@ -0,0 +1,138 @@ +// Copyright (C) 2016 and later: Unicode, Inc. and others. +// License & terms of use: http://www.unicode.org/copyright.html +/* + ******************************************************************************* + * + * Copyright (C) 1998-2014, International Business Machines + * Corporation and others. All Rights Reserved. + * + ******************************************************************************* + * + * File ufile.h + * + * Modification History: + * + * Date Name Description + * 12/01/98 stephen Creation. + * 03/12/99 stephen Modified for new C API. + ******************************************************************************* + */ + +#ifndef UFILE_H +#define UFILE_H + +#include "unicode/utypes.h" + +#if !UCONFIG_NO_CONVERSION + +#include "unicode/ucnv.h" +#include "unicode/utrans.h" +#include "locbund.h" + +/* The buffer size for fromUnicode calls */ +#define UFILE_CHARBUFFER_SIZE 1024 + +/* The buffer size for toUnicode calls */ +#define UFILE_UCHARBUFFER_SIZE 1024 + +/* A UFILE */ + +#if !UCONFIG_NO_TRANSLITERATION + +typedef struct { + UChar *buffer; /* Beginning of buffer */ + int32_t capacity; /* Capacity of buffer */ + int32_t pos; /* Beginning of untranslitted data */ + int32_t length; /* Length *from beginning of buffer* of untranslitted data */ + UTransliterator *translit; +} UFILETranslitBuffer; + +#endif + +typedef struct u_localized_string { + UChar *fPos; /* current pos in fUCBuffer */ + const UChar *fLimit; /* data limit in fUCBuffer */ + UChar *fBuffer; /* Place to write the string */ + +#if !UCONFIG_NO_FORMATTING + ULocaleBundle fBundle; /* formatters */ +#endif +} u_localized_string; + +struct UFILE { +#if !UCONFIG_NO_TRANSLITERATION + UFILETranslitBuffer *fTranslit; +#endif + + FILE *fFile; /* the actual filesystem interface */ + + UConverter *fConverter; /* for codeset conversion */ + + u_localized_string str; /* struct to handle strings for number formatting */ + + UChar fUCBuffer[UFILE_UCHARBUFFER_SIZE];/* buffer used for toUnicode */ + + UBool fOwnFile; /* TRUE if fFile should be closed */ + + int32_t fFileno; /* File number. Useful to determine if it's stdin. */ +}; + +/** + * Like u_file_write but takes a flush parameter + */ +U_CFUNC int32_t U_EXPORT2 +u_file_write_flush( const UChar *chars, + int32_t count, + UFILE *f, + UBool flushIO, + UBool flushTranslit); + +/** + * Fill a UFILE's buffer with converted codepage data. + * @param f The UFILE containing the buffer to fill. + */ +void +ufile_fill_uchar_buffer(UFILE *f); + +/** + * Get one code unit and detect whether the end of file has been reached. + * @param f The UFILE containing the characters. + * @param ch The read in character + * @return TRUE if the character is valid, or FALSE when EOF has been detected + */ +U_CFUNC UBool U_EXPORT2 +ufile_getch(UFILE *f, UChar *ch); + +/** + * Get one character and detect whether the end of file has been reached. + * @param f The UFILE containing the characters. + * @param ch The read in character + * @return TRUE if the character is valid, or FALSE when EOF has been detected + */ +U_CFUNC UBool U_EXPORT2 +ufile_getch32(UFILE *f, UChar32 *ch); + +/** + * Close out the transliterator and flush any data therein. + * @param f flu + */ +void +ufile_close_translit(UFILE *f); + +/** + * Flush the buffer in the transliterator + * @param f UFile to flush + */ +void +ufile_flush_translit(UFILE *f); + +/** + * Flush the IO buffer + * @param f UFile to flush + */ +void +ufile_flush_io(UFILE *f); + + +#endif +#endif diff --git a/intl/icu/source/io/ufmt_cmn.c b/intl/icu/source/io/ufmt_cmn.c new file mode 100644 index 000000000..e24d0d7a0 --- /dev/null +++ b/intl/icu/source/io/ufmt_cmn.c @@ -0,0 +1,259 @@ +// Copyright (C) 2016 and later: Unicode, Inc. and others. +// License & terms of use: http://www.unicode.org/copyright.html +/* +****************************************************************************** +* +* Copyright (C) 1998-2014, International Business Machines +* Corporation and others. All Rights Reserved. +* +****************************************************************************** +* +* File ufmt_cmn.c +* +* Modification History: +* +* Date Name Description +* 12/02/98 stephen Creation. +* 03/12/99 stephen Modified for new C API. +* 03/15/99 stephen Added defaultCPToUnicode, unicodeToDefaultCP +* 07/19/99 stephen Fixed bug in defaultCPToUnicode +****************************************************************************** +*/ + +#include "cstring.h" +#include "cmemory.h" +#include "ufmt_cmn.h" +#include "unicode/uchar.h" +#include "unicode/ucnv.h" +#include "ustr_cnv.h" + +#if !UCONFIG_NO_CONVERSION + + +#define DIGIT_0 0x0030 +#define DIGIT_9 0x0039 +#define LOWERCASE_A 0x0061 +#define UPPERCASE_A 0x0041 +#define LOWERCASE_Z 0x007A +#define UPPERCASE_Z 0x005A + +int +ufmt_digitvalue(UChar c) +{ + if( ((c>=DIGIT_0)&&(c<=DIGIT_9)) || + ((c>=LOWERCASE_A)&&(c<=LOWERCASE_Z)) || + ((c>=UPPERCASE_A)&&(c<=UPPERCASE_Z)) ) + { + return c - DIGIT_0 - (c >= 0x0041 ? (c >= 0x0061 ? 39 : 7) : 0); + } + else + { + return -1; + } +} + +UBool +ufmt_isdigit(UChar c, + int32_t radix) +{ + int digitVal = ufmt_digitvalue(c); + + return (UBool)(digitVal < radix && digitVal >= 0); +} + +#define TO_UC_DIGIT(a) a <= 9 ? (DIGIT_0 + a) : (0x0037 + a) +#define TO_LC_DIGIT(a) a <= 9 ? (DIGIT_0 + a) : (0x0057 + a) + +void +ufmt_64tou(UChar *buffer, + int32_t *len, + uint64_t value, + uint8_t radix, + UBool uselower, + int32_t minDigits) +{ + int32_t length = 0; + uint32_t digit; + UChar *left, *right, temp; + + do { + digit = (uint32_t)(value % radix); + value = value / radix; + buffer[length++] = (UChar)(uselower ? TO_LC_DIGIT(digit) + : TO_UC_DIGIT(digit)); + } while(value); + + /* pad with zeroes to make it minDigits long */ + if(minDigits != -1 && length < minDigits) { + while(length < minDigits && length < *len) + buffer[length++] = DIGIT_0; /*zero padding */ + } + + /* reverse the buffer */ + left = buffer; + right = buffer + length; + while(left < --right) { + temp = *left; + *left++ = *right; + *right = temp; + } + + *len = length; +} + +void +ufmt_ptou(UChar *buffer, + int32_t *len, + void *value, + UBool uselower) +{ + int32_t i; + int32_t length = 0; + uint8_t *ptrIdx = (uint8_t *)&value; + +#if U_IS_BIG_ENDIAN + for (i = 0; i < (int32_t)sizeof(void *); i++) +#else + for (i = (int32_t)sizeof(void *)-1; i >= 0 ; i--) +#endif + { + uint8_t byteVal = ptrIdx[i]; + uint16_t firstNibble = (uint16_t)(byteVal>>4); + uint16_t secondNibble = (uint16_t)(byteVal&0xF); + if (uselower) { + buffer[length++]=TO_LC_DIGIT(firstNibble); + buffer[length++]=TO_LC_DIGIT(secondNibble); + } + else { + buffer[length++]=TO_UC_DIGIT(firstNibble); + buffer[length++]=TO_UC_DIGIT(secondNibble); + } + } + + *len = length; +} + +int64_t +ufmt_uto64(const UChar *buffer, + int32_t *len, + int8_t radix) +{ + const UChar *limit; + int32_t count; + int64_t result; + + + /* intialize parameters */ + limit = buffer + *len; + count = 0; + result = 0; + + /* iterate through buffer */ + while(ufmt_isdigit(*buffer, radix) && buffer < limit) { + + /* read the next digit */ + result *= radix; + result += ufmt_digitvalue(*buffer++); + + /* increment our count */ + ++count; + } + + *len = count; + return result; +} + +#define NIBBLE_PER_BYTE 2 +void * +ufmt_utop(const UChar *buffer, + int32_t *len) +{ + int32_t count, resultIdx, incVal, offset; + /* This union allows the pointer to be written as an array. */ + union { + void *ptr; + uint8_t bytes[sizeof(void*)]; + } result; + + /* intialize variables */ + count = 0; + offset = 0; + result.ptr = NULL; + + /* Skip the leading zeros */ + while(buffer[count] == DIGIT_0 || u_isspace(buffer[count])) { + count++; + offset++; + } + + /* iterate through buffer, stop when you hit the end */ + while(ufmt_isdigit(buffer[count], 16) && count < *len) { + /* increment the count consumed */ + ++count; + } + + /* detect overflow */ + if (count - offset > (int32_t)(sizeof(void*)*NIBBLE_PER_BYTE)) { + offset = count - (int32_t)(sizeof(void*)*NIBBLE_PER_BYTE); + } + + /* Initialize the direction of the input */ +#if U_IS_BIG_ENDIAN + incVal = -1; + resultIdx = (int32_t)(sizeof(void*) - 1); +#else + incVal = 1; + resultIdx = 0; +#endif + /* Write how much was consumed. */ + *len = count; + while(--count >= offset) { + /* Get the first nibble of the byte */ + uint8_t byte = (uint8_t)ufmt_digitvalue(buffer[count]); + + if (count > offset) { + /* Get the second nibble of the byte when available */ + byte = (uint8_t)(byte + (ufmt_digitvalue(buffer[--count]) << 4)); + } + /* Write the byte into the array */ + result.bytes[resultIdx] = byte; + resultIdx += incVal; + } + + return result.ptr; +} + +UChar* +ufmt_defaultCPToUnicode(const char *s, int32_t sSize, + UChar *target, int32_t tSize) +{ + UChar *alias; + UErrorCode status = U_ZERO_ERROR; + UConverter *defConverter = u_getDefaultConverter(&status); + + if(U_FAILURE(status) || defConverter == 0) + return 0; + + if(sSize <= 0) { + sSize = uprv_strlen(s) + 1; + } + + /* perform the conversion in one swoop */ + if(target != 0) { + + alias = target; + ucnv_toUnicode(defConverter, &alias, alias + tSize, &s, s + sSize - 1, + NULL, TRUE, &status); + + + /* add the null terminator */ + *alias = 0x0000; + } + + u_releaseDefaultConverter(defConverter); + + return target; +} + + +#endif diff --git a/intl/icu/source/io/ufmt_cmn.h b/intl/icu/source/io/ufmt_cmn.h new file mode 100644 index 000000000..97079ed9d --- /dev/null +++ b/intl/icu/source/io/ufmt_cmn.h @@ -0,0 +1,162 @@ +// Copyright (C) 2016 and later: Unicode, Inc. and others. +// License & terms of use: http://www.unicode.org/copyright.html +/* +****************************************************************************** +* +* Copyright (C) 1998-2011, International Business Machines +* Corporation and others. All Rights Reserved. +* +****************************************************************************** +* +* File ufmt_cmn.h +* +* Modification History: +* +* Date Name Description +* 12/02/98 stephen Creation. +* 03/12/99 stephen Modified for new C API. +* 03/15/99 stephen Added defaultCPToUnicode, unicodeToDefaultCP +****************************************************************************** +*/ + +#ifndef UFMT_CMN_H +#define UFMT_CMN_H + +#include "unicode/utypes.h" +#include "unicode/utf16.h" + +#define UFMT_DEFAULT_BUFFER_SIZE 128 +#define MAX_UCHAR_BUFFER_SIZE(buffer) (sizeof(buffer)/(U16_MAX_LENGTH*sizeof(UChar))) +#define MAX_UCHAR_BUFFER_NEEDED(strLen) ((strLen+1)*U16_MAX_LENGTH*sizeof(UChar)) + +/** + * Enum representing the possible argument types for uprintf/uscanf + */ +typedef enum ufmt_type_info { + ufmt_empty = 0, + ufmt_simple_percent, /* %% do nothing */ + ufmt_count, /* special flag for count */ + ufmt_int, /* int */ + ufmt_char, /* int, cast to char */ + ufmt_string, /* char* */ + ufmt_pointer, /* void* */ + ufmt_float, /* float */ + ufmt_double, /* double */ + ufmt_uchar, /* int, cast to UChar */ + ufmt_ustring /* UChar* */ + /*ufmt_wchar,*/ /* wchar_t */ + /*ufmt_wstring,*/ /* wchar_t* */ + /*ufmt_date,*/ /* Date */ + /*ufmt_last*/ +} ufmt_type_info; + +/** + * Union representing a uprintf/uscanf argument + */ +typedef union ufmt_args { + int64_t int64Value; /* int, UChar */ + float floatValue; /* float */ + double doubleValue; /* double */ + void *ptrValue; /* any pointer - void*, char*, wchar_t*, UChar* */ + /*wchar_t wcharValue;*/ /* wchar_t */ /* TODO: Should wchar_t be used? */ + /*UDate dateValue;*/ /* Date */ +} ufmt_args; + +/** + * Macro for determining the minimum of two numbers. + * @param a An integer + * @param b An integer + * @return <TT>a</TT> if </TT>a < b</TT>, <TT>b</TT> otherwise + */ +#define ufmt_min(a,b) ((a) < (b) ? (a) : (b)) + +/** + * Convert a UChar in hex radix to an integer value. + * @param c The UChar to convert. + * @return The integer value of <TT>c</TT>. + */ +int +ufmt_digitvalue(UChar c); + +/** + * Determine if a UChar is a digit for a specified radix. + * @param c The UChar to check. + * @param radix The desired radix. + * @return TRUE if <TT>c</TT> is a digit in <TT>radix</TT>, FALSE otherwise. + */ +UBool +ufmt_isdigit(UChar c, + int32_t radix); + +/** + * Convert an int64_t to a UChar* in a specified radix + * @param buffer The target buffer + * @param len On input, the size of <TT>buffer</TT>. On output, + * the number of UChars written to <TT>buffer</TT>. + * @param value The value to be converted + * @param radix The desired radix + * @param uselower TRUE means lower case will be used, FALSE means upper case + * @param minDigits The minimum number of digits for for the formatted number, + * which will be padded with zeroes. -1 means do not pad. + */ +void +ufmt_64tou(UChar *buffer, + int32_t *len, + uint64_t value, + uint8_t radix, + UBool uselower, + int32_t minDigits); + +/** + * It's like ufmt_64tou, but with a pointer. + * This functions avoids size constraints of 64-bit types. + * Pointers can be at 32-128 bits in size. + */ +void +ufmt_ptou(UChar *buffer, + int32_t *len, + void *value, + UBool uselower); + +/** + * Convert a UChar* in a specified radix to an int64_t. + * @param buffer The target buffer + * @param len On input, the size of <TT>buffer</TT>. On output, + * the number of UChars read from <TT>buffer</TT>. + * @param radix The desired radix + * @return The numeric value. + */ +int64_t +ufmt_uto64(const UChar *buffer, + int32_t *len, + int8_t radix); + +/** + * Convert a UChar* in a specified radix to a pointer, + * @param buffer The target buffer + * @param len On input, the size of <TT>buffer</TT>. On output, + * the number of UChars read from <TT>buffer</TT>. + * @param radix The desired radix + * @return The pointer value. + */ +void * +ufmt_utop(const UChar *buffer, + int32_t *len); + +/** + * Convert a string from the default codepage to Unicode. + * @param s The string to convert, in the default codepage. + * @param sSize The size of s to convert. + * @param target The buffer to convert to. + * @param tSize The size of target + * @return A pointer to a newly allocated converted version of s, or 0 + * on error. + */ +UChar* +ufmt_defaultCPToUnicode(const char *s, int32_t sSize, + UChar *target, int32_t tSize); + + + +#endif + diff --git a/intl/icu/source/io/unicode/ustdio.h b/intl/icu/source/io/unicode/ustdio.h new file mode 100644 index 000000000..8f38398cd --- /dev/null +++ b/intl/icu/source/io/unicode/ustdio.h @@ -0,0 +1,1018 @@ +// Copyright (C) 2016 and later: Unicode, Inc. and others. +// License & terms of use: http://www.unicode.org/copyright.html +/* +****************************************************************************** +* +* Copyright (C) 1998-2015, International Business Machines +* Corporation and others. All Rights Reserved. +* +****************************************************************************** +* +* File ustdio.h +* +* Modification History: +* +* Date Name Description +* 10/16/98 stephen Creation. +* 11/06/98 stephen Modified per code review. +* 03/12/99 stephen Modified for new C API. +* 07/19/99 stephen Minor doc update. +* 02/01/01 george Added sprintf & sscanf with all of its variants +****************************************************************************** +*/ + +#ifndef USTDIO_H +#define USTDIO_H + +#include <stdio.h> +#include <stdarg.h> + +#include "unicode/utypes.h" +#include "unicode/ucnv.h" +#include "unicode/utrans.h" +#include "unicode/localpointer.h" +#include "unicode/unum.h" + +#if !UCONFIG_NO_CONVERSION + +/* + TODO + The following is a small list as to what is currently wrong/suggestions for + ustdio. + + * Make sure that * in the scanf format specification works for all formats. + * Each UFILE takes up at least 2KB. + Look into adding setvbuf() for configurable buffers. + * This library does buffering. The OS should do this for us already. Check on + this, and remove it from this library, if this is the case. Double buffering + wastes a lot of time and space. + * Test stdin and stdout with the u_f* functions + * Testing should be done for reading and writing multi-byte encodings, + and make sure that a character that is contained across buffer boundries + works even for incomplete characters. + * Make sure that the last character is flushed when the file/string is closed. + * snprintf should follow the C99 standard for the return value, which is + return the number of characters (excluding the trailing '\0') + which would have been written to the destination string regardless + of available space. This is like pre-flighting. + * Everything that uses %s should do what operator>> does for UnicodeString. + It should convert one byte at a time, and once a character is + converted then check to see if it's whitespace or in the scanset. + If it's whitespace or in the scanset, put all the bytes back (do nothing + for sprintf/sscanf). + * If bad string data is encountered, make sure that the function fails + without memory leaks and the unconvertable characters are valid + substitution or are escaped characters. + * u_fungetc() can't unget a character when it's at the beginning of the + internal conversion buffer. For example, read the buffer size # of + characters, and then ungetc to get the previous character that was + at the end of the last buffer. + * u_fflush() and u_fclose should return an int32_t like C99 functions. + 0 is returned if the operation was successful and EOF otherwise. + * u_fsettransliterator does not support U_READ side of transliteration. + * The format specifier should limit the size of a format or honor it in + order to prevent buffer overruns. (e.g. %256.256d). + * u_fread and u_fwrite don't exist. They're needed for reading and writing + data structures without any conversion. + * u_file_read and u_file_write are used for writing strings. u_fgets and + u_fputs or u_fread and u_fwrite should be used to do this. + * The width parameter for all scanf formats, including scanset, needs + better testing. This prevents buffer overflows. + * Figure out what is suppose to happen when a codepage is changed midstream. + Maybe a flush or a rewind are good enough. + * Make sure that a UFile opened with "rw" can be used after using + u_fflush with a u_frewind. + * scanf(%i) should detect what type of number to use. + * Add more testing of the alternate format, %# + * Look at newline handling of fputs/puts + * Think more about codeunit/codepoint error handling/support in %S,%s,%C,%c,%[] + * Complete the file documentation with proper doxygen formatting. + See http://oss.software.ibm.com/pipermail/icu/2003-July/005647.html +*/ + +/** + * \file + * \brief C API: Unicode stdio-like API + * + * <h2>Unicode stdio-like C API</h2> + * + * <p>This API provides an stdio-like API wrapper around ICU's other + * formatting and parsing APIs. It is meant to ease the transition of adding + * Unicode support to a preexisting applications using stdio. The following + * is a small list of noticable differences between stdio and ICU I/O's + * ustdio implementation.</p> + * + * <ul> + * <li>Locale specific formatting and parsing is only done with file IO.</li> + * <li>u_fstropen can be used to simulate file IO with strings. + * This is similar to the iostream API, and it allows locale specific + * formatting and parsing to be used.</li> + * <li>This API provides uniform formatting and parsing behavior between + * platforms (unlike the standard stdio implementations found on various + * platforms).</li> + * <li>This API is better suited for text data handling than binary data + * handling when compared to the typical stdio implementation.</li> + * <li>You can specify a Transliterator while using the file IO.</li> + * <li>You can specify a file's codepage separately from the default + * system codepage.</li> + * </ul> + * + * <h2>Formatting and Parsing Specification</h2> + * + * General printf format:<br> + * %[format modifier][width][.precision][type modifier][format] + * + * General scanf format:<br> + * %[*][format modifier][width][type modifier][format] + * +<table cellspacing="3"> +<tr><td>format</td><td>default<br>printf<br>type</td><td>default<br>scanf<br>type</td><td>description</td></tr> +<tr><td>%E</td><td>double</td><td>float</td><td>Scientific with an uppercase exponent</td></tr> +<tr><td>%e</td><td>double</td><td>float</td><td>Scientific with a lowercase exponent</td></tr> +<tr><td>%G</td><td>double</td><td>float</td><td>Use %E or %f for best format</td></tr> +<tr><td>%g</td><td>double</td><td>float</td><td>Use %e or %f for best format</td></tr> +<tr><td>%f</td><td>double</td><td>float</td><td>Simple floating point without the exponent</td></tr> +<tr><td>%X</td><td>int32_t</td><td>int32_t</td><td>ustdio special uppercase hex radix formatting</td></tr> +<tr><td>%x</td><td>int32_t</td><td>int32_t</td><td>ustdio special lowercase hex radix formatting</td></tr> +<tr><td>%d</td><td>int32_t</td><td>int32_t</td><td>Decimal format</td></tr> +<tr><td>%i</td><td>int32_t</td><td>int32_t</td><td>Same as %d</td></tr> +<tr><td>%n</td><td>int32_t</td><td>int32_t</td><td>count (write the number of UTF-16 codeunits read/written)</td></tr> +<tr><td>%o</td><td>int32_t</td><td>int32_t</td><td>ustdio special octal radix formatting</td></tr> +<tr><td>%u</td><td>uint32_t</td><td>uint32_t</td><td>Decimal format</td></tr> +<tr><td>%p</td><td>void *</td><td>void *</td><td>Prints the pointer value</td></tr> +<tr><td>%s</td><td>char *</td><td>char *</td><td>Use default converter or specified converter from fopen</td></tr> +<tr><td>%c</td><td>char</td><td>char</td><td>Use default converter or specified converter from fopen<br> +When width is specified for scanf, this acts like a non-NULL-terminated char * string.<br> +By default, only one char is written.</td></tr> +<tr><td>%S</td><td>UChar *</td><td>UChar *</td><td>Null terminated UTF-16 string</td></tr> +<tr><td>%C</td><td>UChar</td><td>UChar</td><td>16-bit Unicode code unit<br> +When width is specified for scanf, this acts like a non-NULL-terminated UChar * string<br> +By default, only one codepoint is written.</td></tr> +<tr><td>%[]</td><td> </td><td>UChar *</td><td>Null terminated UTF-16 string which contains the filtered set of characters specified by the UnicodeSet</td></tr> +<tr><td>%%</td><td> </td><td> </td><td>Show a percent sign</td></tr> +</table> + +Format modifiers +<table> +<tr><td>modifier</td><td>formats</td><td>type</td><td>comments</td></tr> +<tr><td>%h</td><td>%d, %i, %o, %x</td><td>int16_t</td><td>short format</td></tr> +<tr><td>%h</td><td>%u</td><td>uint16_t</td><td>short format</td></tr> +<tr><td>%h</td><td>c</td><td>char</td><td><b>(Unimplemented)</b> Use invariant converter</td></tr> +<tr><td>%h</td><td>s</td><td>char *</td><td><b>(Unimplemented)</b> Use invariant converter</td></tr> +<tr><td>%h</td><td>C</td><td>char</td><td><b>(Unimplemented)</b> 8-bit Unicode code unit</td></tr> +<tr><td>%h</td><td>S</td><td>char *</td><td><b>(Unimplemented)</b> Null terminated UTF-8 string</td></tr> +<tr><td>%l</td><td>%d, %i, %o, %x</td><td>int32_t</td><td>long format (no effect)</td></tr> +<tr><td>%l</td><td>%u</td><td>uint32_t</td><td>long format (no effect)</td></tr> +<tr><td>%l</td><td>c</td><td>N/A</td><td><b>(Unimplemented)</b> Reserved for future implementation</td></tr> +<tr><td>%l</td><td>s</td><td>N/A</td><td><b>(Unimplemented)</b> Reserved for future implementation</td></tr> +<tr><td>%l</td><td>C</td><td>UChar32</td><td><b>(Unimplemented)</b> 32-bit Unicode code unit</td></tr> +<tr><td>%l</td><td>S</td><td>UChar32 *</td><td><b>(Unimplemented)</b> Null terminated UTF-32 string</td></tr> +<tr><td>%ll</td><td>%d, %i, %o, %x</td><td>int64_t</td><td>long long format</td></tr> +<tr><td>%ll</td><td>%u</td><td>uint64_t</td><td><b>(Unimplemented)</b> long long format</td></tr> +<tr><td>%-</td><td><i>all</i></td><td>N/A</td><td>Left justify</td></tr> +<tr><td>%+</td><td>%d, %i, %o, %x, %e, %f, %g, %E, %G</td><td>N/A</td><td>Always show the plus or minus sign. Needs data for plus sign.</td></tr> +<tr><td>% </td><td>%d, %i, %o, %x, %e, %f, %g, %E, %G</td><td>N/A</td><td>Instead of a "+" output a blank character for positive numbers.</td></tr> +<tr><td>%#</td><td>%d, %i, %o, %x, %e, %f, %g, %E, %G</td><td>N/A</td><td>Precede octal value with 0, hex with 0x and show the + decimal point for floats.</td></tr> +<tr><td>%<i>n</i></td><td><i>all</i></td><td>N/A</td><td>Width of input/output. num is an actual number from 0 to + some large number.</td></tr> +<tr><td>%.<i>n</i></td><td>%e, %f, %g, %E, %F, %G</td><td>N/A</td><td>Significant digits precision. num is an actual number from + 0 to some large number.<br>If * is used in printf, then the precision is passed in as an argument before the number to be formatted.</td></tr> +</table> + +printf modifier +%* int32_t Next argument after this one specifies the width + +scanf modifier +%* N/A This field is scanned, but not stored + +<p>If you are using this C API instead of the ustream.h API for C++, +you can use one of the following u_fprintf examples to display a UnicodeString.</p> + +<pre><code> + UFILE *out = u_finit(stdout, NULL, NULL); + UnicodeString string1("string 1"); + UnicodeString string2("string 2"); + u_fprintf(out, "%S\n", string1.getTerminatedBuffer()); + u_fprintf(out, "%.*S\n", string2.length(), string2.getBuffer()); + u_fclose(out); +</code></pre> + + */ + + +/** + * When an end of file is encountered, this value can be returned. + * @see u_fgetc + * @stable 3.0 + */ +#define U_EOF 0xFFFF + +/** Forward declaration of a Unicode-aware file @stable 3.0 */ +typedef struct UFILE UFILE; + +/** + * Enum for which direction of stream a transliterator applies to. + * @see u_fsettransliterator + * @stable ICU 3.0 + */ +typedef enum { + U_READ = 1, + U_WRITE = 2, + U_READWRITE =3 /* == (U_READ | U_WRITE) */ +} UFileDirection; + +/** + * Open a UFILE. + * A UFILE is a wrapper around a FILE* that is locale and codepage aware. + * That is, data written to a UFILE will be formatted using the conventions + * specified by that UFILE's Locale; this data will be in the character set + * specified by that UFILE's codepage. + * @param filename The name of the file to open. + * @param perm The read/write permission for the UFILE; one of "r", "w", "rw" + * @param locale The locale whose conventions will be used to format + * and parse output. If this parameter is NULL, the default locale will + * be used. + * @param codepage The codepage in which data will be written to and + * read from the file. If this paramter is NULL the system default codepage + * will be used. + * @return A new UFILE, or NULL if an error occurred. + * @stable ICU 3.0 + */ +U_STABLE UFILE* U_EXPORT2 +u_fopen(const char *filename, + const char *perm, + const char *locale, + const char *codepage); + +/** + * Open a UFILE with a UChar* filename + * A UFILE is a wrapper around a FILE* that is locale and codepage aware. + * That is, data written to a UFILE will be formatted using the conventions + * specified by that UFILE's Locale; this data will be in the character set + * specified by that UFILE's codepage. + * @param filename The name of the file to open. + * @param perm The read/write permission for the UFILE; one of "r", "w", "rw" + * @param locale The locale whose conventions will be used to format + * and parse output. If this parameter is NULL, the default locale will + * be used. + * @param codepage The codepage in which data will be written to and + * read from the file. If this paramter is NULL the system default codepage + * will be used. + * @return A new UFILE, or NULL if an error occurred. + * @stable ICU 54 + */ +U_STABLE UFILE* U_EXPORT2 +u_fopen_u(const UChar *filename, + const char *perm, + const char *locale, + const char *codepage); + +/** + * Open a UFILE on top of an existing FILE* stream. The FILE* stream + * ownership remains with the caller. To have the UFILE take over + * ownership and responsibility for the FILE* stream, use the + * function u_fadopt. + * @param f The FILE* to which this UFILE will attach and use. + * @param locale The locale whose conventions will be used to format + * and parse output. If this parameter is NULL, the default locale will + * be used. + * @param codepage The codepage in which data will be written to and + * read from the file. If this paramter is NULL, data will be written and + * read using the default codepage for <TT>locale</TT>, unless <TT>locale</TT> + * is NULL, in which case the system default codepage will be used. + * @return A new UFILE, or NULL if an error occurred. + * @stable ICU 3.0 + */ +U_STABLE UFILE* U_EXPORT2 +u_finit(FILE *f, + const char *locale, + const char *codepage); + +/** + * Open a UFILE on top of an existing FILE* stream. The FILE* stream + * ownership is transferred to the new UFILE. It will be closed when the + * UFILE is closed. + * @param f The FILE* which this UFILE will take ownership of. + * @param locale The locale whose conventions will be used to format + * and parse output. If this parameter is NULL, the default locale will + * be used. + * @param codepage The codepage in which data will be written to and + * read from the file. If this paramter is NULL, data will be written and + * read using the default codepage for <TT>locale</TT>, unless <TT>locale</TT> + * is NULL, in which case the system default codepage will be used. + * @return A new UFILE, or NULL if an error occurred. If an error occurs + * the ownership of the FILE* stream remains with the caller. + * @stable ICU 4.4 + */ +U_STABLE UFILE* U_EXPORT2 +u_fadopt(FILE *f, + const char *locale, + const char *codepage); + +/** + * Create a UFILE that can be used for localized formatting or parsing. + * The u_sprintf and u_sscanf functions do not read or write numbers for a + * specific locale. The ustdio.h file functions can be used on this UFILE. + * The string is usable once u_fclose or u_fflush has been called on the + * returned UFILE. + * @param stringBuf The string used for reading or writing. + * @param capacity The number of code units available for use in stringBuf + * @param locale The locale whose conventions will be used to format + * and parse output. If this parameter is NULL, the default locale will + * be used. + * @return A new UFILE, or NULL if an error occurred. + * @stable ICU 3.0 + */ +U_STABLE UFILE* U_EXPORT2 +u_fstropen(UChar *stringBuf, + int32_t capacity, + const char *locale); + +/** + * Close a UFILE. Implies u_fflush first. + * @param file The UFILE to close. + * @stable ICU 3.0 + * @see u_fflush + */ +U_STABLE void U_EXPORT2 +u_fclose(UFILE *file); + +#if U_SHOW_CPLUSPLUS_API + +U_NAMESPACE_BEGIN + +/** + * \class LocalUFILEPointer + * "Smart pointer" class, closes a UFILE via u_fclose(). + * For most methods see the LocalPointerBase base class. + * + * @see LocalPointerBase + * @see LocalPointer + * @stable ICU 4.4 + */ +U_DEFINE_LOCAL_OPEN_POINTER(LocalUFILEPointer, UFILE, u_fclose); + +U_NAMESPACE_END + +#endif + +/** + * Tests if the UFILE is at the end of the file stream. + * @param f The UFILE from which to read. + * @return Returns TRUE after the first read operation that attempts to + * read past the end of the file. It returns FALSE if the current position is + * not end of file. + * @stable ICU 3.0 +*/ +U_STABLE UBool U_EXPORT2 +u_feof(UFILE *f); + +/** + * Flush output of a UFILE. Implies a flush of + * converter/transliterator state. (That is, a logical break is + * made in the output stream - for example if a different type of + * output is desired.) The underlying OS level file is also flushed. + * Note that for a stateful encoding, the converter may write additional + * bytes to return the stream to default state. + * @param file The UFILE to flush. + * @stable ICU 3.0 + */ +U_STABLE void U_EXPORT2 +u_fflush(UFILE *file); + +/** + * Rewind the file pointer to the beginning of the file. + * @param file The UFILE to rewind. + * @stable ICU 3.0 + */ +U_STABLE void +u_frewind(UFILE *file); + +/** + * Get the FILE* associated with a UFILE. + * @param f The UFILE + * @return A FILE*, owned by the UFILE. (The FILE <EM>must not</EM> be modified or closed) + * @stable ICU 3.0 + */ +U_STABLE FILE* U_EXPORT2 +u_fgetfile(UFILE *f); + +#if !UCONFIG_NO_FORMATTING + +/** + * Get the locale whose conventions are used to format and parse output. + * This is the same locale passed in the preceding call to<TT>u_fsetlocale</TT> + * or <TT>u_fopen</TT>. + * @param file The UFILE to set. + * @return The locale whose conventions are used to format and parse output. + * @stable ICU 3.0 + */ +U_STABLE const char* U_EXPORT2 +u_fgetlocale(UFILE *file); + +/** + * Set the locale whose conventions will be used to format and parse output. + * @param locale The locale whose conventions will be used to format + * and parse output. + * @param file The UFILE to query. + * @return NULL if successful, otherwise a negative number. + * @stable ICU 3.0 + */ +U_STABLE int32_t U_EXPORT2 +u_fsetlocale(UFILE *file, + const char *locale); + +#endif + +/** + * Get the codepage in which data is written to and read from the UFILE. + * This is the same codepage passed in the preceding call to + * <TT>u_fsetcodepage</TT> or <TT>u_fopen</TT>. + * @param file The UFILE to query. + * @return The codepage in which data is written to and read from the UFILE, + * or NULL if an error occurred. + * @stable ICU 3.0 + */ +U_STABLE const char* U_EXPORT2 +u_fgetcodepage(UFILE *file); + +/** + * Set the codepage in which data will be written to and read from the UFILE. + * All Unicode data written to the UFILE will be converted to this codepage + * before it is written to the underlying FILE*. It it generally a bad idea to + * mix codepages within a file. This should only be called right + * after opening the <TT>UFile</TT>, or after calling <TT>u_frewind</TT>. + * @param codepage The codepage in which data will be written to + * and read from the file. For example <TT>"latin-1"</TT> or <TT>"ibm-943"</TT>. + * A value of NULL means the default codepage for the UFILE's current + * locale will be used. + * @param file The UFILE to set. + * @return 0 if successful, otherwise a negative number. + * @see u_frewind + * @stable ICU 3.0 + */ +U_STABLE int32_t U_EXPORT2 +u_fsetcodepage(const char *codepage, + UFILE *file); + + +/** + * Returns an alias to the converter being used for this file. + * @param f The UFILE to get the value from + * @return alias to the converter (The converter <EM>must not</EM> be modified or closed) + * @stable ICU 3.0 + */ +U_STABLE UConverter* U_EXPORT2 u_fgetConverter(UFILE *f); + +#if !UCONFIG_NO_FORMATTING +/** + * Returns an alias to the number formatter being used for this file. + * @param f The UFILE to get the value from + * @return alias to the number formatter (The formatter <EM>must not</EM> be modified or closed) + * @stable ICU 51 +*/ + U_STABLE const UNumberFormat* U_EXPORT2 u_fgetNumberFormat(UFILE *f); + +/* Output functions */ + +/** + * Write formatted data to <TT>stdout</TT>. + * @param patternSpecification A pattern specifying how <TT>u_printf</TT> will + * interpret the variable arguments received and format the data. + * @return The number of Unicode characters written to <TT>stdout</TT> + * @stable ICU 49 + */ +U_STABLE int32_t U_EXPORT2 +u_printf(const char *patternSpecification, + ... ); + +/** + * Write formatted data to a UFILE. + * @param f The UFILE to which to write. + * @param patternSpecification A pattern specifying how <TT>u_fprintf</TT> will + * interpret the variable arguments received and format the data. + * @return The number of Unicode characters written to <TT>f</TT>. + * @stable ICU 3.0 + */ +U_STABLE int32_t U_EXPORT2 +u_fprintf(UFILE *f, + const char *patternSpecification, + ... ); + +/** + * Write formatted data to a UFILE. + * This is identical to <TT>u_fprintf</TT>, except that it will + * <EM>not</EM> call <TT>va_start</TT> and <TT>va_end</TT>. + * @param f The UFILE to which to write. + * @param patternSpecification A pattern specifying how <TT>u_fprintf</TT> will + * interpret the variable arguments received and format the data. + * @param ap The argument list to use. + * @return The number of Unicode characters written to <TT>f</TT>. + * @see u_fprintf + * @stable ICU 3.0 + */ +U_STABLE int32_t U_EXPORT2 +u_vfprintf(UFILE *f, + const char *patternSpecification, + va_list ap); + +/** + * Write formatted data to <TT>stdout</TT>. + * @param patternSpecification A pattern specifying how <TT>u_printf_u</TT> will + * interpret the variable arguments received and format the data. + * @return The number of Unicode characters written to <TT>stdout</TT> + * @stable ICU 49 + */ +U_STABLE int32_t U_EXPORT2 +u_printf_u(const UChar *patternSpecification, + ... ); + +/** + * Get a UFILE for <TT>stdout</TT>. + * @return UFILE that writes to <TT>stdout</TT> + * @stable ICU 49 + */ +U_STABLE UFILE * U_EXPORT2 +u_get_stdout(void); + +/** + * Write formatted data to a UFILE. + * @param f The UFILE to which to write. + * @param patternSpecification A pattern specifying how <TT>u_fprintf</TT> will + * interpret the variable arguments received and format the data. + * @return The number of Unicode characters written to <TT>f</TT>. + * @stable ICU 3.0 + */ +U_STABLE int32_t U_EXPORT2 +u_fprintf_u(UFILE *f, + const UChar *patternSpecification, + ... ); + +/** + * Write formatted data to a UFILE. + * This is identical to <TT>u_fprintf_u</TT>, except that it will + * <EM>not</EM> call <TT>va_start</TT> and <TT>va_end</TT>. + * @param f The UFILE to which to write. + * @param patternSpecification A pattern specifying how <TT>u_fprintf</TT> will + * interpret the variable arguments received and format the data. + * @param ap The argument list to use. + * @return The number of Unicode characters written to <TT>f</TT>. + * @see u_fprintf_u + * @stable ICU 3.0 + */ +U_STABLE int32_t U_EXPORT2 +u_vfprintf_u(UFILE *f, + const UChar *patternSpecification, + va_list ap); +#endif +/** + * Write a Unicode to a UFILE. The null (U+0000) terminated UChar* + * <TT>s</TT> will be written to <TT>f</TT>, excluding the NULL terminator. + * A newline will be added to <TT>f</TT>. + * @param s The UChar* to write. + * @param f The UFILE to which to write. + * @return A non-negative number if successful, EOF otherwise. + * @see u_file_write + * @stable ICU 3.0 + */ +U_STABLE int32_t U_EXPORT2 +u_fputs(const UChar *s, + UFILE *f); + +/** + * Write a UChar to a UFILE. + * @param uc The UChar to write. + * @param f The UFILE to which to write. + * @return The character written if successful, EOF otherwise. + * @stable ICU 3.0 + */ +U_STABLE UChar32 U_EXPORT2 +u_fputc(UChar32 uc, + UFILE *f); + +/** + * Write Unicode to a UFILE. + * The ustring passed in will be converted to the UFILE's underlying + * codepage before it is written. + * @param ustring A pointer to the Unicode data to write. + * @param count The number of Unicode characters to write + * @param f The UFILE to which to write. + * @return The number of Unicode characters written. + * @see u_fputs + * @stable ICU 3.0 + */ +U_STABLE int32_t U_EXPORT2 +u_file_write(const UChar *ustring, + int32_t count, + UFILE *f); + + +/* Input functions */ +#if !UCONFIG_NO_FORMATTING + +/** + * Read formatted data from a UFILE. + * @param f The UFILE from which to read. + * @param patternSpecification A pattern specifying how <TT>u_fscanf</TT> will + * interpret the variable arguments received and parse the data. + * @return The number of items successfully converted and assigned, or EOF + * if an error occurred. + * @stable ICU 3.0 + */ +U_STABLE int32_t U_EXPORT2 +u_fscanf(UFILE *f, + const char *patternSpecification, + ... ); + +/** + * Read formatted data from a UFILE. + * This is identical to <TT>u_fscanf</TT>, except that it will + * <EM>not</EM> call <TT>va_start</TT> and <TT>va_end</TT>. + * @param f The UFILE from which to read. + * @param patternSpecification A pattern specifying how <TT>u_fscanf</TT> will + * interpret the variable arguments received and parse the data. + * @param ap The argument list to use. + * @return The number of items successfully converted and assigned, or EOF + * if an error occurred. + * @see u_fscanf + * @stable ICU 3.0 + */ +U_STABLE int32_t U_EXPORT2 +u_vfscanf(UFILE *f, + const char *patternSpecification, + va_list ap); + +/** + * Read formatted data from a UFILE. + * @param f The UFILE from which to read. + * @param patternSpecification A pattern specifying how <TT>u_fscanf</TT> will + * interpret the variable arguments received and parse the data. + * @return The number of items successfully converted and assigned, or EOF + * if an error occurred. + * @stable ICU 3.0 + */ +U_STABLE int32_t U_EXPORT2 +u_fscanf_u(UFILE *f, + const UChar *patternSpecification, + ... ); + +/** + * Read formatted data from a UFILE. + * This is identical to <TT>u_fscanf_u</TT>, except that it will + * <EM>not</EM> call <TT>va_start</TT> and <TT>va_end</TT>. + * @param f The UFILE from which to read. + * @param patternSpecification A pattern specifying how <TT>u_fscanf</TT> will + * interpret the variable arguments received and parse the data. + * @param ap The argument list to use. + * @return The number of items successfully converted and assigned, or EOF + * if an error occurred. + * @see u_fscanf_u + * @stable ICU 3.0 + */ +U_STABLE int32_t U_EXPORT2 +u_vfscanf_u(UFILE *f, + const UChar *patternSpecification, + va_list ap); +#endif + +/** + * Read one line of text into a UChar* string from a UFILE. The newline + * at the end of the line is read into the string. The string is always + * null terminated + * @param f The UFILE from which to read. + * @param n The maximum number of characters - 1 to read. + * @param s The UChar* to receive the read data. Characters will be + * stored successively in <TT>s</TT> until a newline or EOF is + * reached. A null character (U+0000) will be appended to <TT>s</TT>. + * @return A pointer to <TT>s</TT>, or NULL if no characters were available. + * @stable ICU 3.0 + */ +U_STABLE UChar* U_EXPORT2 +u_fgets(UChar *s, + int32_t n, + UFILE *f); + +/** + * Read a UChar from a UFILE. It is recommended that <TT>u_fgetcx</TT> + * used instead for proper parsing functions, but sometimes reading + * code units is needed instead of codepoints. + * + * @param f The UFILE from which to read. + * @return The UChar value read, or U+FFFF if no character was available. + * @stable ICU 3.0 + */ +U_STABLE UChar U_EXPORT2 +u_fgetc(UFILE *f); + +/** + * Read a UChar32 from a UFILE. + * + * @param f The UFILE from which to read. + * @return The UChar32 value read, or U_EOF if no character was + * available, or U+FFFFFFFF if an ill-formed character was + * encountered. + * @see u_unescape() + * @stable ICU 3.0 + */ +U_STABLE UChar32 U_EXPORT2 +u_fgetcx(UFILE *f); + +/** + * Unget a UChar from a UFILE. + * If this function is not the first to operate on <TT>f</TT> after a call + * to <TT>u_fgetc</TT>, the results are undefined. + * If this function is passed a character that was not recieved from the + * previous <TT>u_fgetc</TT> or <TT>u_fgetcx</TT> call, the results are undefined. + * @param c The UChar to put back on the stream. + * @param f The UFILE to receive <TT>c</TT>. + * @return The UChar32 value put back if successful, U_EOF otherwise. + * @stable ICU 3.0 + */ +U_STABLE UChar32 U_EXPORT2 +u_fungetc(UChar32 c, + UFILE *f); + +/** + * Read Unicode from a UFILE. + * Bytes will be converted from the UFILE's underlying codepage, with + * subsequent conversion to Unicode. The data will not be NULL terminated. + * @param chars A pointer to receive the Unicode data. + * @param count The number of Unicode characters to read. + * @param f The UFILE from which to read. + * @return The number of Unicode characters read. + * @stable ICU 3.0 + */ +U_STABLE int32_t U_EXPORT2 +u_file_read(UChar *chars, + int32_t count, + UFILE *f); + +#if !UCONFIG_NO_TRANSLITERATION + +/** + * Set a transliterator on the UFILE. The transliterator will be owned by the + * UFILE. + * @param file The UFILE to set transliteration on + * @param adopt The UTransliterator to set. Can be NULL, which will + * mean that no transliteration is used. + * @param direction either U_READ, U_WRITE, or U_READWRITE - sets + * which direction the transliterator is to be applied to. If + * U_READWRITE, the "Read" transliteration will be in the inverse + * direction. + * @param status ICU error code. + * @return The previously set transliterator, owned by the + * caller. If U_READWRITE is specified, only the WRITE transliterator + * is returned. In most cases, the caller should call utrans_close() + * on the result of this function. + * @stable ICU 3.0 + */ +U_STABLE UTransliterator* U_EXPORT2 +u_fsettransliterator(UFILE *file, UFileDirection direction, + UTransliterator *adopt, UErrorCode *status); + +#endif + + +/* Output string functions */ +#if !UCONFIG_NO_FORMATTING + + +/** + * Write formatted data to a Unicode string. + * + * @param buffer The Unicode String to which to write. + * @param patternSpecification A pattern specifying how <TT>u_sprintf</TT> will + * interpret the variable arguments received and format the data. + * @return The number of Unicode code units written to <TT>buffer</TT>. This + * does not include the terminating null character. + * @stable ICU 3.0 + */ +U_STABLE int32_t U_EXPORT2 +u_sprintf(UChar *buffer, + const char *patternSpecification, + ... ); + +/** + * Write formatted data to a Unicode string. When the number of code units + * required to store the data exceeds <TT>count</TT>, then <TT>count</TT> code + * units of data are stored in <TT>buffer</TT> and a negative value is + * returned. When the number of code units required to store the data equals + * <TT>count</TT>, the string is not null terminated and <TT>count</TT> is + * returned. + * + * @param buffer The Unicode String to which to write. + * @param count The number of code units to read. + * @param patternSpecification A pattern specifying how <TT>u_sprintf</TT> will + * interpret the variable arguments received and format the data. + * @return The number of Unicode characters that would have been written to + * <TT>buffer</TT> had count been sufficiently large. This does not include + * the terminating null character. + * @stable ICU 3.0 + */ +U_STABLE int32_t U_EXPORT2 +u_snprintf(UChar *buffer, + int32_t count, + const char *patternSpecification, + ... ); + +/** + * Write formatted data to a Unicode string. + * This is identical to <TT>u_sprintf</TT>, except that it will + * <EM>not</EM> call <TT>va_start</TT> and <TT>va_end</TT>. + * + * @param buffer The Unicode string to which to write. + * @param patternSpecification A pattern specifying how <TT>u_sprintf</TT> will + * interpret the variable arguments received and format the data. + * @param ap The argument list to use. + * @return The number of Unicode characters written to <TT>buffer</TT>. + * @see u_sprintf + * @stable ICU 3.0 + */ +U_STABLE int32_t U_EXPORT2 +u_vsprintf(UChar *buffer, + const char *patternSpecification, + va_list ap); + +/** + * Write formatted data to a Unicode string. + * This is identical to <TT>u_snprintf</TT>, except that it will + * <EM>not</EM> call <TT>va_start</TT> and <TT>va_end</TT>.<br><br> + * When the number of code units required to store the data exceeds + * <TT>count</TT>, then <TT>count</TT> code units of data are stored in + * <TT>buffer</TT> and a negative value is returned. When the number of code + * units required to store the data equals <TT>count</TT>, the string is not + * null terminated and <TT>count</TT> is returned. + * + * @param buffer The Unicode string to which to write. + * @param count The number of code units to read. + * @param patternSpecification A pattern specifying how <TT>u_sprintf</TT> will + * interpret the variable arguments received and format the data. + * @param ap The argument list to use. + * @return The number of Unicode characters that would have been written to + * <TT>buffer</TT> had count been sufficiently large. + * @see u_sprintf + * @stable ICU 3.0 + */ +U_STABLE int32_t U_EXPORT2 +u_vsnprintf(UChar *buffer, + int32_t count, + const char *patternSpecification, + va_list ap); + +/** + * Write formatted data to a Unicode string. + * + * @param buffer The Unicode string to which to write. + * @param patternSpecification A pattern specifying how <TT>u_sprintf</TT> will + * interpret the variable arguments received and format the data. + * @return The number of Unicode characters written to <TT>buffer</TT>. + * @stable ICU 3.0 + */ +U_STABLE int32_t U_EXPORT2 +u_sprintf_u(UChar *buffer, + const UChar *patternSpecification, + ... ); + +/** + * Write formatted data to a Unicode string. When the number of code units + * required to store the data exceeds <TT>count</TT>, then <TT>count</TT> code + * units of data are stored in <TT>buffer</TT> and a negative value is + * returned. When the number of code units required to store the data equals + * <TT>count</TT>, the string is not null terminated and <TT>count</TT> is + * returned. + * + * @param buffer The Unicode string to which to write. + * @param count The number of code units to read. + * @param patternSpecification A pattern specifying how <TT>u_sprintf</TT> will + * interpret the variable arguments received and format the data. + * @return The number of Unicode characters that would have been written to + * <TT>buffer</TT> had count been sufficiently large. + * @stable ICU 3.0 + */ +U_STABLE int32_t U_EXPORT2 +u_snprintf_u(UChar *buffer, + int32_t count, + const UChar *patternSpecification, + ... ); + +/** + * Write formatted data to a Unicode string. + * This is identical to <TT>u_sprintf_u</TT>, except that it will + * <EM>not</EM> call <TT>va_start</TT> and <TT>va_end</TT>. + * + * @param buffer The Unicode string to which to write. + * @param patternSpecification A pattern specifying how <TT>u_sprintf</TT> will + * interpret the variable arguments received and format the data. + * @param ap The argument list to use. + * @return The number of Unicode characters written to <TT>f</TT>. + * @see u_sprintf_u + * @stable ICU 3.0 + */ +U_STABLE int32_t U_EXPORT2 +u_vsprintf_u(UChar *buffer, + const UChar *patternSpecification, + va_list ap); + +/** + * Write formatted data to a Unicode string. + * This is identical to <TT>u_snprintf_u</TT>, except that it will + * <EM>not</EM> call <TT>va_start</TT> and <TT>va_end</TT>. + * When the number of code units required to store the data exceeds + * <TT>count</TT>, then <TT>count</TT> code units of data are stored in + * <TT>buffer</TT> and a negative value is returned. When the number of code + * units required to store the data equals <TT>count</TT>, the string is not + * null terminated and <TT>count</TT> is returned. + * + * @param buffer The Unicode string to which to write. + * @param count The number of code units to read. + * @param patternSpecification A pattern specifying how <TT>u_sprintf</TT> will + * interpret the variable arguments received and format the data. + * @param ap The argument list to use. + * @return The number of Unicode characters that would have been written to + * <TT>f</TT> had count been sufficiently large. + * @see u_sprintf_u + * @stable ICU 3.0 + */ +U_STABLE int32_t U_EXPORT2 +u_vsnprintf_u(UChar *buffer, + int32_t count, + const UChar *patternSpecification, + va_list ap); + +/* Input string functions */ + +/** + * Read formatted data from a Unicode string. + * + * @param buffer The Unicode string from which to read. + * @param patternSpecification A pattern specifying how <TT>u_sscanf</TT> will + * interpret the variable arguments received and parse the data. + * @return The number of items successfully converted and assigned, or EOF + * if an error occurred. + * @stable ICU 3.0 + */ +U_STABLE int32_t U_EXPORT2 +u_sscanf(const UChar *buffer, + const char *patternSpecification, + ... ); + +/** + * Read formatted data from a Unicode string. + * This is identical to <TT>u_sscanf</TT>, except that it will + * <EM>not</EM> call <TT>va_start</TT> and <TT>va_end</TT>. + * + * @param buffer The Unicode string from which to read. + * @param patternSpecification A pattern specifying how <TT>u_sscanf</TT> will + * interpret the variable arguments received and parse the data. + * @param ap The argument list to use. + * @return The number of items successfully converted and assigned, or EOF + * if an error occurred. + * @see u_sscanf + * @stable ICU 3.0 + */ +U_STABLE int32_t U_EXPORT2 +u_vsscanf(const UChar *buffer, + const char *patternSpecification, + va_list ap); + +/** + * Read formatted data from a Unicode string. + * + * @param buffer The Unicode string from which to read. + * @param patternSpecification A pattern specifying how <TT>u_sscanf</TT> will + * interpret the variable arguments received and parse the data. + * @return The number of items successfully converted and assigned, or EOF + * if an error occurred. + * @stable ICU 3.0 + */ +U_STABLE int32_t U_EXPORT2 +u_sscanf_u(const UChar *buffer, + const UChar *patternSpecification, + ... ); + +/** + * Read formatted data from a Unicode string. + * This is identical to <TT>u_sscanf_u</TT>, except that it will + * <EM>not</EM> call <TT>va_start</TT> and <TT>va_end</TT>. + * + * @param buffer The Unicode string from which to read. + * @param patternSpecification A pattern specifying how <TT>u_sscanf</TT> will + * interpret the variable arguments received and parse the data. + * @param ap The argument list to use. + * @return The number of items successfully converted and assigned, or EOF + * if an error occurred. + * @see u_sscanf_u + * @stable ICU 3.0 + */ +U_STABLE int32_t U_EXPORT2 +u_vsscanf_u(const UChar *buffer, + const UChar *patternSpecification, + va_list ap); + + +#endif +#endif +#endif + + diff --git a/intl/icu/source/io/unicode/ustream.h b/intl/icu/source/io/unicode/ustream.h new file mode 100644 index 000000000..0eb13b548 --- /dev/null +++ b/intl/icu/source/io/unicode/ustream.h @@ -0,0 +1,70 @@ +// Copyright (C) 2016 and later: Unicode, Inc. and others. +// License & terms of use: http://www.unicode.org/copyright.html +/* +********************************************************************** +* Copyright (C) 2001-2014 International Business Machines +* Corporation and others. All Rights Reserved. +********************************************************************** +* FILE NAME : ustream.h +* +* Modification History: +* +* Date Name Description +* 06/25/2001 grhoten Move iostream from unistr.h +****************************************************************************** +*/ + +#ifndef USTREAM_H +#define USTREAM_H + +#include "unicode/unistr.h" + +#if !UCONFIG_NO_CONVERSION // not available without conversion + +/** + * \file + * \brief C++ API: Unicode iostream like API + * + * At this time, this API is very limited. It contains + * operator<< and operator>> for UnicodeString manipulation with the + * C++ I/O stream API. + */ + +#if defined(__GLIBCXX__) +namespace std { class type_info; } // WORKAROUND: http://llvm.org/bugs/show_bug.cgi?id=13364 +#endif + +#if U_IOSTREAM_SOURCE >= 199711 +#if (__GNUC__ == 2) +#include <iostream> +#else +#include <istream> +#include <ostream> +#endif + +U_NAMESPACE_BEGIN + +/** + * Write the contents of a UnicodeString to a C++ ostream. This functions writes + * the characters in a UnicodeString to an ostream. The UChars in the + * UnicodeString are converted to the char based ostream with the default + * converter. + * @stable 3.0 + */ +U_IO_API std::ostream & U_EXPORT2 operator<<(std::ostream& stream, const UnicodeString& s); + +/** + * Write the contents from a C++ istream to a UnicodeString. The UChars in the + * UnicodeString are converted from the char based istream with the default + * converter. + * @stable 3.0 + */ +U_IO_API std::istream & U_EXPORT2 operator>>(std::istream& stream, UnicodeString& s); +U_NAMESPACE_END + +#endif + +/* No operator for UChar because it can conflict with wchar_t */ + +#endif +#endif diff --git a/intl/icu/source/io/uprintf.cpp b/intl/icu/source/io/uprintf.cpp new file mode 100644 index 000000000..b12ef0efe --- /dev/null +++ b/intl/icu/source/io/uprintf.cpp @@ -0,0 +1,219 @@ +// Copyright (C) 2016 and later: Unicode, Inc. and others. +// License & terms of use: http://www.unicode.org/copyright.html +/* +****************************************************************************** +* +* Copyright (C) 1998-2014, International Business Machines +* Corporation and others. All Rights Reserved. +* +****************************************************************************** +* +* File uprintf.cpp +* +* Modification History: +* +* Date Name Description +* 11/19/98 stephen Creation. +* 03/12/99 stephen Modified for new C API. +* Added conversion from default codepage. +* 08/07/2003 george Reunify printf implementations +****************************************************************************** +*/ + +#include "unicode/utypes.h" + +#if !UCONFIG_NO_FORMATTING && !UCONFIG_NO_CONVERSION + +#include "unicode/ustdio.h" +#include "unicode/ustring.h" +#include "unicode/unum.h" +#include "unicode/udat.h" +#include "unicode/putil.h" + +#include "cmemory.h" +#include "locbund.h" +#include "mutex.h" +#include "uassert.h" +#include "uprintf.h" +#include "ufile.h" +#include "ucln_io.h" + +U_NAMESPACE_USE + +static UFILE *gStdOut = NULL; +static UInitOnce gStdOutInitOnce = U_INITONCE_INITIALIZER; + +static UBool U_CALLCONV uprintf_cleanup(void) +{ + if (gStdOut != NULL) { + u_fclose(gStdOut); + gStdOut = NULL; + } + gStdOutInitOnce.reset(); + return TRUE; +} + +static void U_CALLCONV u_stdout_init() { + U_ASSERT(gStdOut == NULL); + gStdOut = u_finit(stdout, NULL, NULL); + ucln_io_registerCleanup(UCLN_IO_PRINTF, &uprintf_cleanup); +} + +U_CAPI UFILE * U_EXPORT2 +u_get_stdout() +{ + umtx_initOnce(gStdOutInitOnce, &u_stdout_init); + return gStdOut; +} + +static int32_t U_EXPORT2 +u_printf_write(void *context, + const UChar *str, + int32_t count) +{ + return u_file_write(str, count, (UFILE *)context); +} + +static int32_t +u_printf_pad_and_justify(void *context, + const u_printf_spec_info *info, + const UChar *result, + int32_t resultLen) +{ + UFILE *output = (UFILE *)context; + int32_t written, i; + + /* pad and justify, if needed */ + if(info->fWidth != -1 && resultLen < info->fWidth) { + /* left justify */ + if(info->fLeft) { + written = u_file_write(result, resultLen, output); + for(i = 0; i < info->fWidth - resultLen; ++i) { + written += u_file_write(&info->fPadChar, 1, output); + } + } + /* right justify */ + else { + written = 0; + for(i = 0; i < info->fWidth - resultLen; ++i) { + written += u_file_write(&info->fPadChar, 1, output); + } + written += u_file_write(result, resultLen, output); + } + } + /* just write the formatted output */ + else { + written = u_file_write(result, resultLen, output); + } + + return written; +} + +U_CAPI int32_t U_EXPORT2 +u_fprintf( UFILE *f, + const char *patternSpecification, + ... ) +{ + va_list ap; + int32_t count; + + va_start(ap, patternSpecification); + count = u_vfprintf(f, patternSpecification, ap); + va_end(ap); + + return count; +} + +U_CAPI int32_t U_EXPORT2 +u_printf(const char *patternSpecification, + ...) +{ + va_list ap; + int32_t count; + va_start(ap, patternSpecification); + count = u_vfprintf(u_get_stdout(), patternSpecification, ap); + va_end(ap); + return count; +} + +U_CAPI int32_t U_EXPORT2 +u_fprintf_u( UFILE *f, + const UChar *patternSpecification, + ... ) +{ + va_list ap; + int32_t count; + + va_start(ap, patternSpecification); + count = u_vfprintf_u(f, patternSpecification, ap); + va_end(ap); + + return count; +} + +U_CAPI int32_t U_EXPORT2 +u_printf_u(const UChar *patternSpecification, + ...) +{ + va_list ap; + int32_t count; + va_start(ap, patternSpecification); + count = u_vfprintf_u(u_get_stdout(), patternSpecification, ap); + va_end(ap); + return count; +} + +U_CAPI int32_t U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */ +u_vfprintf( UFILE *f, + const char *patternSpecification, + va_list ap) +{ + int32_t count; + UChar *pattern; + UChar buffer[UFMT_DEFAULT_BUFFER_SIZE]; + size_t size = strlen(patternSpecification) + 1; + + /* convert from the default codepage to Unicode */ + if (size >= MAX_UCHAR_BUFFER_SIZE(buffer)) { + pattern = (UChar *)uprv_malloc(size * sizeof(UChar)); + if(pattern == 0) { + return 0; + } + } + else { + pattern = buffer; + } + u_charsToUChars(patternSpecification, pattern, size); + + /* do the work */ + count = u_vfprintf_u(f, pattern, ap); + + /* clean up */ + if (pattern != buffer) { + uprv_free(pattern); + } + + return count; +} + +static const u_printf_stream_handler g_stream_handler = { + u_printf_write, + u_printf_pad_and_justify +}; + +U_CAPI int32_t U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */ +u_vfprintf_u( UFILE *f, + const UChar *patternSpecification, + va_list ap) +{ + int32_t written = 0; /* haven't written anything yet */ + + /* parse and print the whole format string */ + u_printf_parse(&g_stream_handler, patternSpecification, f, NULL, &f->str.fBundle, &written, ap); + + /* return # of UChars written */ + return written; +} + +#endif /* #if !UCONFIG_NO_FORMATTING */ + diff --git a/intl/icu/source/io/uprintf.h b/intl/icu/source/io/uprintf.h new file mode 100644 index 000000000..0a07e6b7d --- /dev/null +++ b/intl/icu/source/io/uprintf.h @@ -0,0 +1,103 @@ +// Copyright (C) 2016 and later: Unicode, Inc. and others. +// License & terms of use: http://www.unicode.org/copyright.html +/* +****************************************************************************** +* +* Copyright (C) 1998-2006, International Business Machines +* Corporation and others. All Rights Reserved. +* +****************************************************************************** +* +* File uprintf.h +* +* Modification History: +* +* Date Name Description +* 11/19/98 stephen Creation. +* 03/12/99 stephen Modified for new C API. +****************************************************************************** +*/ + +#ifndef UPRINTF_H +#define UPRINTF_H + +#include "unicode/utypes.h" + +#if !UCONFIG_NO_FORMATTING + +#include "unicode/ustdio.h" +#include "ufmt_cmn.h" +#include "locbund.h" + +/** + * Struct encapsulating a single uprintf format specification. + */ +typedef struct u_printf_spec_info { + int32_t fPrecision; /* Precision */ + int32_t fWidth; /* Width */ + + UChar fOrigSpec; /* Conversion specification */ + UChar fSpec; /* Conversion specification */ + UChar fPadChar; /* Padding character */ + + UBool fAlt; /* # flag */ + UBool fSpace; /* Space flag */ + UBool fLeft; /* - flag */ + UBool fShowSign; /* + flag */ + UBool fZero; /* 0 flag */ + + UBool fIsLongDouble; /* L flag */ + UBool fIsShort; /* h flag */ + UBool fIsLong; /* l flag */ + UBool fIsLongLong; /* ll flag */ +} u_printf_spec_info; + +typedef int32_t U_EXPORT2 +u_printf_write_stream(void *context, + const UChar *str, + int32_t count); + +typedef int32_t U_EXPORT2 +u_printf_pad_and_justify_stream(void *context, + const u_printf_spec_info *info, + const UChar *result, + int32_t resultLen); + +typedef struct u_printf_stream_handler { + u_printf_write_stream *write; + u_printf_pad_and_justify_stream *pad_and_justify; +} u_printf_stream_handler; + +/* Used by sprintf */ +typedef struct u_localized_print_string { + UChar *str; /* Place to write the string */ + int32_t available;/* Number of codeunits available to write to */ + int32_t len; /* Maximum number of code units that can be written to output */ + + ULocaleBundle fBundle; /* formatters */ +} u_localized_print_string; + +#define UP_PERCENT 0x0025 + +/** + * Parse a single u_printf format string. + * @param fmt A pointer to a '%' character in a u_printf format specification. + * @param spec A pointer to a <TT>u_printf_spec</TT> to receive the parsed + * format specifier. + * @param locStringContext If present, will make sure that it will only write + * to the buffer when space is available. It's done this way because + * va_list sometimes can't be passed by pointer. + * @return The number of characters contained in this specifier. + */ +U_CFUNC int32_t +u_printf_parse(const u_printf_stream_handler *streamHandler, + const UChar *fmt, + void *context, + u_localized_print_string *locStringContext, + ULocaleBundle *formatBundle, + int32_t *written, + va_list ap); + +#endif /* #if !UCONFIG_NO_FORMATTING */ + +#endif diff --git a/intl/icu/source/io/uprntf_p.c b/intl/icu/source/io/uprntf_p.c new file mode 100644 index 000000000..9890b29b9 --- /dev/null +++ b/intl/icu/source/io/uprntf_p.c @@ -0,0 +1,1593 @@ +// Copyright (C) 2016 and later: Unicode, Inc. and others. +// License & terms of use: http://www.unicode.org/copyright.html +/* +****************************************************************************** +* +* Copyright (C) 1998-2016, International Business Machines +* Corporation and others. All Rights Reserved. +* +****************************************************************************** +* +* File uprntf_p.c +* +* Modification History: +* +* Date Name Description +* 11/23/98 stephen Creation. +* 03/12/99 stephen Modified for new C API. +* 08/07/2003 george Reunify printf implementations +****************************************************************************** +*/ + +#include "unicode/utypes.h" + +#if !UCONFIG_NO_FORMATTING && !UCONFIG_NO_CONVERSION + +#include "unicode/ustring.h" +#include "unicode/utf16.h" +#include "uprintf.h" +#include "ufmt_cmn.h" +#include "cmemory.h" +#include "putilimp.h" + +/* ANSI style formatting */ +/* Use US-ASCII characters only for formatting */ + +/* % */ +#define UFMT_SIMPLE_PERCENT {ufmt_simple_percent, u_printf_simple_percent_handler} +/* s */ +#define UFMT_STRING {ufmt_string, u_printf_string_handler} +/* c */ +#define UFMT_CHAR {ufmt_char, u_printf_char_handler} +/* d, i */ +#define UFMT_INT {ufmt_int, u_printf_integer_handler} +/* u */ +#define UFMT_UINT {ufmt_int, u_printf_uinteger_handler} +/* o */ +#define UFMT_OCTAL {ufmt_int, u_printf_octal_handler} +/* x, X */ +#define UFMT_HEX {ufmt_int, u_printf_hex_handler} +/* f */ +#define UFMT_DOUBLE {ufmt_double, u_printf_double_handler} +/* e, E */ +#define UFMT_SCIENTIFIC {ufmt_double, u_printf_scientific_handler} +/* g, G */ +#define UFMT_SCIDBL {ufmt_double, u_printf_scidbl_handler} +/* n */ +#define UFMT_COUNT {ufmt_count, u_printf_count_handler} + +/* non-ANSI extensions */ +/* Use US-ASCII characters only for formatting */ + +/* p */ +#define UFMT_POINTER {ufmt_pointer, u_printf_pointer_handler} +/* V */ +#define UFMT_SPELLOUT {ufmt_double, u_printf_spellout_handler} +/* P */ +#define UFMT_PERCENT {ufmt_double, u_printf_percent_handler} +/* C K is old format */ +#define UFMT_UCHAR {ufmt_uchar, u_printf_uchar_handler} +/* S U is old format */ +#define UFMT_USTRING {ufmt_ustring, u_printf_ustring_handler} + + +#define UFMT_EMPTY {ufmt_empty, NULL} + +/** + * A u_printf handler function. + * A u_printf handler is responsible for handling a single u_printf + * format specification, for example 'd' or 's'. + * @param stream The UFILE to which to write output. + * @param info A pointer to a <TT>u_printf_spec_info</TT> struct containing + * information on the format specification. + * @param args A pointer to the argument data + * @return The number of Unicode characters written to <TT>stream</TT>. + */ +typedef int32_t U_EXPORT2 +u_printf_handler(const u_printf_stream_handler *handler, + + void *context, + ULocaleBundle *formatBundle, + const u_printf_spec_info *info, + const ufmt_args *args); + +typedef struct u_printf_info { + ufmt_type_info info; + u_printf_handler *handler; +} u_printf_info; + +/** + * Struct encapsulating a single uprintf format specification. + */ +typedef struct u_printf_spec { + u_printf_spec_info fInfo; /* Information on this spec */ + int32_t fWidthPos; /* Position of width in arg list */ + int32_t fPrecisionPos; /* Position of precision in arg list */ + int32_t fArgPos; /* Position of data in arg list */ +} u_printf_spec; + +#define UPRINTF_NUM_FMT_HANDLERS 108 + +/* We do not use handlers for 0-0x1f */ +#define UPRINTF_BASE_FMT_HANDLERS 0x20 + +/* buffer size for formatting */ +#define UPRINTF_BUFFER_SIZE 1024 +#define UPRINTF_SYMBOL_BUFFER_SIZE 8 + +static const UChar gNullStr[] = {0x28, 0x6E, 0x75, 0x6C, 0x6C, 0x29, 0}; /* "(null)" */ +static const UChar gSpaceStr[] = {0x20, 0}; /* " " */ + +/* Sets the sign of a format based on u_printf_spec_info */ +/* TODO: Is setting the prefix symbol to a positive sign a good idea in all locales? */ +static void +u_printf_set_sign(UNumberFormat *format, + const u_printf_spec_info *info, + UChar *prefixBuffer, + int32_t *prefixBufLen, + UErrorCode *status) +{ + if(info->fShowSign) { + *prefixBufLen = unum_getTextAttribute(format, + UNUM_POSITIVE_PREFIX, + prefixBuffer, + *prefixBufLen, + status); + if (info->fSpace) { + /* Setting UNUM_PLUS_SIGN_SYMBOL affects the exponent too. */ + /* unum_setSymbol(format, UNUM_PLUS_SIGN_SYMBOL, gSpaceStr, 1, &status); */ + unum_setTextAttribute(format, UNUM_POSITIVE_PREFIX, gSpaceStr, 1, status); + } + else { + UChar plusSymbol[UPRINTF_SYMBOL_BUFFER_SIZE]; + int32_t symbolLen; + + symbolLen = unum_getSymbol(format, + UNUM_PLUS_SIGN_SYMBOL, + plusSymbol, + UPRV_LENGTHOF(plusSymbol), + status); + unum_setTextAttribute(format, + UNUM_POSITIVE_PREFIX, + plusSymbol, + symbolLen, + status); + } + } + else { + *prefixBufLen = 0; + } +} + +static void +u_printf_reset_sign(UNumberFormat *format, + const u_printf_spec_info *info, + UChar *prefixBuffer, + int32_t *prefixBufLen, + UErrorCode *status) +{ + if(info->fShowSign) { + unum_setTextAttribute(format, + UNUM_POSITIVE_PREFIX, + prefixBuffer, + *prefixBufLen, + status); + } +} + + +/* handle a '%' */ +static int32_t +u_printf_simple_percent_handler(const u_printf_stream_handler *handler, + void *context, + ULocaleBundle *formatBundle, + const u_printf_spec_info *info, + const ufmt_args *args) +{ + static const UChar PERCENT[] = { UP_PERCENT }; + + /* put a single '%' onto the output */ + return handler->write(context, PERCENT, 1); +} + +/* handle 's' */ +static int32_t +u_printf_string_handler(const u_printf_stream_handler *handler, + void *context, + ULocaleBundle *formatBundle, + const u_printf_spec_info *info, + const ufmt_args *args) +{ + UChar *s; + UChar buffer[UFMT_DEFAULT_BUFFER_SIZE]; + int32_t len, written; + int32_t argSize; + const char *arg = (const char*)(args[0].ptrValue); + + /* convert from the default codepage to Unicode */ + if (arg) { + argSize = (int32_t)strlen(arg) + 1; + if (argSize >= MAX_UCHAR_BUFFER_SIZE(buffer)) { + s = ufmt_defaultCPToUnicode(arg, argSize, + (UChar *)uprv_malloc(MAX_UCHAR_BUFFER_NEEDED(argSize)), + MAX_UCHAR_BUFFER_NEEDED(argSize)); + if(s == NULL) { + return 0; + } + } + else { + s = ufmt_defaultCPToUnicode(arg, argSize, buffer, + UPRV_LENGTHOF(buffer)); + } + } + else { + s = (UChar *)gNullStr; + } + len = u_strlen(s); + + /* width = minimum # of characters to write */ + /* precision = maximum # of characters to write */ + if (info->fPrecision != -1 && info->fPrecision < len) { + len = info->fPrecision; + } + + written = handler->pad_and_justify(context, info, s, len); + + /* clean up */ + if (gNullStr != s && buffer != s) { + uprv_free(s); + } + + return written; +} + +static int32_t +u_printf_char_handler(const u_printf_stream_handler *handler, + void *context, + ULocaleBundle *formatBundle, + const u_printf_spec_info *info, + const ufmt_args *args) +{ + UChar s[U16_MAX_LENGTH+1]; + int32_t len = 1, written; + unsigned char arg = (unsigned char)(args[0].int64Value); + + /* convert from default codepage to Unicode */ + ufmt_defaultCPToUnicode((const char *)&arg, 2, s, UPRV_LENGTHOF(s)); + + /* Remember that this may be an MBCS character */ + if (arg != 0) { + len = u_strlen(s); + } + + /* width = minimum # of characters to write */ + /* precision = maximum # of characters to write */ + /* precision is ignored when handling a char */ + + written = handler->pad_and_justify(context, info, s, len); + + return written; +} + +static int32_t +u_printf_double_handler(const u_printf_stream_handler *handler, + void *context, + ULocaleBundle *formatBundle, + const u_printf_spec_info *info, + const ufmt_args *args) +{ + double num = (double) (args[0].doubleValue); + UNumberFormat *format; + UChar result[UPRINTF_BUFFER_SIZE]; + UChar prefixBuffer[UPRINTF_BUFFER_SIZE]; + int32_t prefixBufferLen = sizeof(prefixBuffer); + int32_t minDecimalDigits; + int32_t maxDecimalDigits; + int32_t resultLen; + UErrorCode status = U_ZERO_ERROR; + + prefixBuffer[0] = 0; + + /* mask off any necessary bits */ + /* if(! info->fIsLongDouble) + num &= DBL_MAX;*/ + + /* get the formatter */ + format = u_locbund_getNumberFormat(formatBundle, UNUM_DECIMAL); + + /* handle error */ + if(format == 0) + return 0; + + /* save the formatter's state */ + minDecimalDigits = unum_getAttribute(format, UNUM_MIN_FRACTION_DIGITS); + maxDecimalDigits = unum_getAttribute(format, UNUM_MAX_FRACTION_DIGITS); + + /* set the appropriate flags and number of decimal digits on the formatter */ + if(info->fPrecision != -1) { + /* set the # of decimal digits */ + unum_setAttribute(format, UNUM_FRACTION_DIGITS, info->fPrecision); + } + else if(info->fAlt) { + /* '#' means always show decimal point */ + /* copy of printf behavior on Solaris - '#' shows 6 digits */ + unum_setAttribute(format, UNUM_FRACTION_DIGITS, 6); + } + else { + /* # of decimal digits is 6 if precision not specified regardless of locale */ + unum_setAttribute(format, UNUM_FRACTION_DIGITS, 6); + } + + /* set whether to show the sign */ + if (info->fShowSign) { + u_printf_set_sign(format, info, prefixBuffer, &prefixBufferLen, &status); + } + + /* format the number */ + resultLen = unum_formatDouble(format, num, result, UPRINTF_BUFFER_SIZE, 0, &status); + + if (U_FAILURE(status)) { + resultLen = 0; + } + + /* restore the number format */ + /* TODO: Is this needed? */ + unum_setAttribute(format, UNUM_MIN_FRACTION_DIGITS, minDecimalDigits); + unum_setAttribute(format, UNUM_MAX_FRACTION_DIGITS, maxDecimalDigits); + + if (info->fShowSign) { + /* Reset back to original value regardless of what the error was */ + UErrorCode localStatus = U_ZERO_ERROR; + u_printf_reset_sign(format, info, prefixBuffer, &prefixBufferLen, &localStatus); + } + + return handler->pad_and_justify(context, info, result, resultLen); +} + +/* HSYS */ +static int32_t +u_printf_integer_handler(const u_printf_stream_handler *handler, + void *context, + ULocaleBundle *formatBundle, + const u_printf_spec_info *info, + const ufmt_args *args) +{ + int64_t num = args[0].int64Value; + UNumberFormat *format; + UChar result[UPRINTF_BUFFER_SIZE]; + UChar prefixBuffer[UPRINTF_BUFFER_SIZE]; + int32_t prefixBufferLen = sizeof(prefixBuffer); + int32_t minDigits = -1; + int32_t resultLen; + UErrorCode status = U_ZERO_ERROR; + + prefixBuffer[0] = 0; + + /* mask off any necessary bits */ + if (info->fIsShort) + num = (int16_t)num; + else if (!info->fIsLongLong) + num = (int32_t)num; + + /* get the formatter */ + format = u_locbund_getNumberFormat(formatBundle, UNUM_DECIMAL); + + /* handle error */ + if(format == 0) + return 0; + + /* set the appropriate flags on the formatter */ + + /* set the minimum integer digits */ + if(info->fPrecision != -1) { + /* set the minimum # of digits */ + minDigits = unum_getAttribute(format, UNUM_MIN_INTEGER_DIGITS); + unum_setAttribute(format, UNUM_MIN_INTEGER_DIGITS, info->fPrecision); + } + + /* set whether to show the sign */ + if(info->fShowSign) { + u_printf_set_sign(format, info, prefixBuffer, &prefixBufferLen, &status); + } + + /* format the number */ + resultLen = unum_formatInt64(format, num, result, UPRINTF_BUFFER_SIZE, 0, &status); + + if (U_FAILURE(status)) { + resultLen = 0; + } + + /* restore the number format */ + if (minDigits != -1) { + unum_setAttribute(format, UNUM_MIN_INTEGER_DIGITS, minDigits); + } + + if (info->fShowSign) { + /* Reset back to original value regardless of what the error was */ + UErrorCode localStatus = U_ZERO_ERROR; + u_printf_reset_sign(format, info, prefixBuffer, &prefixBufferLen, &localStatus); + } + + return handler->pad_and_justify(context, info, result, resultLen); +} + +static int32_t +u_printf_hex_handler(const u_printf_stream_handler *handler, + void *context, + ULocaleBundle *formatBundle, + const u_printf_spec_info *info, + const ufmt_args *args) +{ + int64_t num = args[0].int64Value; + UChar result[UPRINTF_BUFFER_SIZE]; + int32_t len = UPRINTF_BUFFER_SIZE; + + + /* mask off any necessary bits */ + if (info->fIsShort) + num &= UINT16_MAX; + else if (!info->fIsLongLong) + num &= UINT32_MAX; + + /* format the number, preserving the minimum # of digits */ + ufmt_64tou(result, &len, num, 16, + (UBool)(info->fSpec == 0x0078), + (info->fPrecision == -1 && info->fZero) ? info->fWidth : info->fPrecision); + + /* convert to alt form, if desired */ + if(num != 0 && info->fAlt && len < UPRINTF_BUFFER_SIZE - 2) { + /* shift the formatted string right by 2 chars */ + memmove(result + 2, result, len * sizeof(UChar)); + result[0] = 0x0030; + result[1] = info->fSpec; + len += 2; + } + + return handler->pad_and_justify(context, info, result, len); +} + +static int32_t +u_printf_octal_handler(const u_printf_stream_handler *handler, + void *context, + ULocaleBundle *formatBundle, + const u_printf_spec_info *info, + const ufmt_args *args) +{ + int64_t num = args[0].int64Value; + UChar result[UPRINTF_BUFFER_SIZE]; + int32_t len = UPRINTF_BUFFER_SIZE; + + + /* mask off any necessary bits */ + if (info->fIsShort) + num &= UINT16_MAX; + else if (!info->fIsLongLong) + num &= UINT32_MAX; + + /* format the number, preserving the minimum # of digits */ + ufmt_64tou(result, &len, num, 8, + FALSE, /* doesn't matter for octal */ + info->fPrecision == -1 && info->fZero ? info->fWidth : info->fPrecision); + + /* convert to alt form, if desired */ + if(info->fAlt && result[0] != 0x0030 && len < UPRINTF_BUFFER_SIZE - 1) { + /* shift the formatted string right by 1 char */ + memmove(result + 1, result, len * sizeof(UChar)); + result[0] = 0x0030; + len += 1; + } + + return handler->pad_and_justify(context, info, result, len); +} + +static int32_t +u_printf_uinteger_handler(const u_printf_stream_handler *handler, + void *context, + ULocaleBundle *formatBundle, + const u_printf_spec_info *info, + const ufmt_args *args) +{ + int64_t num = args[0].int64Value; + UNumberFormat *format; + UChar result[UPRINTF_BUFFER_SIZE]; + int32_t minDigits = -1; + int32_t resultLen; + UErrorCode status = U_ZERO_ERROR; + + /* TODO: Fix this once uint64_t can be formatted. */ + if (info->fIsShort) + num &= UINT16_MAX; + else if (!info->fIsLongLong) + num &= UINT32_MAX; + + /* get the formatter */ + format = u_locbund_getNumberFormat(formatBundle, UNUM_DECIMAL); + + /* handle error */ + if(format == 0) + return 0; + + /* set the appropriate flags on the formatter */ + + /* set the minimum integer digits */ + if(info->fPrecision != -1) { + /* set the minimum # of digits */ + minDigits = unum_getAttribute(format, UNUM_MIN_INTEGER_DIGITS); + unum_setAttribute(format, UNUM_MIN_INTEGER_DIGITS, info->fPrecision); + } + + /* To mirror other stdio implementations, we ignore the sign argument */ + + /* format the number */ + resultLen = unum_formatInt64(format, num, result, UPRINTF_BUFFER_SIZE, 0, &status); + + if (U_FAILURE(status)) { + resultLen = 0; + } + + /* restore the number format */ + if (minDigits != -1) { + unum_setAttribute(format, UNUM_MIN_INTEGER_DIGITS, minDigits); + } + + return handler->pad_and_justify(context, info, result, resultLen); +} + +static int32_t +u_printf_pointer_handler(const u_printf_stream_handler *handler, + void *context, + ULocaleBundle *formatBundle, + const u_printf_spec_info *info, + const ufmt_args *args) +{ + UChar result[UPRINTF_BUFFER_SIZE]; + int32_t len = UPRINTF_BUFFER_SIZE; + + /* format the pointer in hex */ + ufmt_ptou(result, &len, args[0].ptrValue, TRUE/*, info->fPrecision*/); + + return handler->pad_and_justify(context, info, result, len); +} + +static int32_t +u_printf_scientific_handler(const u_printf_stream_handler *handler, + void *context, + ULocaleBundle *formatBundle, + const u_printf_spec_info *info, + const ufmt_args *args) +{ + double num = (double) (args[0].doubleValue); + UNumberFormat *format; + UChar result[UPRINTF_BUFFER_SIZE]; + UChar prefixBuffer[UPRINTF_BUFFER_SIZE]; + int32_t prefixBufferLen = sizeof(prefixBuffer); + int32_t minDecimalDigits; + int32_t maxDecimalDigits; + UErrorCode status = U_ZERO_ERROR; + UChar srcExpBuf[UPRINTF_SYMBOL_BUFFER_SIZE]; + int32_t srcLen, expLen; + int32_t resultLen; + UChar expBuf[UPRINTF_SYMBOL_BUFFER_SIZE]; + + prefixBuffer[0] = 0; + + /* mask off any necessary bits */ + /* if(! info->fIsLongDouble) + num &= DBL_MAX;*/ + + /* get the formatter */ + format = u_locbund_getNumberFormat(formatBundle, UNUM_SCIENTIFIC); + + /* handle error */ + if(format == 0) + return 0; + + /* set the appropriate flags on the formatter */ + + srcLen = unum_getSymbol(format, + UNUM_EXPONENTIAL_SYMBOL, + srcExpBuf, + sizeof(srcExpBuf), + &status); + + /* Upper/lower case the e */ + if (info->fSpec == (UChar)0x65 /* e */) { + expLen = u_strToLower(expBuf, (int32_t)sizeof(expBuf), + srcExpBuf, srcLen, + formatBundle->fLocale, + &status); + } + else { + expLen = u_strToUpper(expBuf, (int32_t)sizeof(expBuf), + srcExpBuf, srcLen, + formatBundle->fLocale, + &status); + } + + unum_setSymbol(format, + UNUM_EXPONENTIAL_SYMBOL, + expBuf, + expLen, + &status); + + /* save the formatter's state */ + minDecimalDigits = unum_getAttribute(format, UNUM_MIN_FRACTION_DIGITS); + maxDecimalDigits = unum_getAttribute(format, UNUM_MAX_FRACTION_DIGITS); + + /* set the appropriate flags and number of decimal digits on the formatter */ + if(info->fPrecision != -1) { + /* set the # of decimal digits */ + if (info->fOrigSpec == (UChar)0x65 /* e */ || info->fOrigSpec == (UChar)0x45 /* E */) { + unum_setAttribute(format, UNUM_FRACTION_DIGITS, info->fPrecision); + } + else { + unum_setAttribute(format, UNUM_MIN_FRACTION_DIGITS, 1); + unum_setAttribute(format, UNUM_MAX_FRACTION_DIGITS, info->fPrecision); + } + } + else if(info->fAlt) { + /* '#' means always show decimal point */ + /* copy of printf behavior on Solaris - '#' shows 6 digits */ + unum_setAttribute(format, UNUM_FRACTION_DIGITS, 6); + } + else { + /* # of decimal digits is 6 if precision not specified */ + unum_setAttribute(format, UNUM_FRACTION_DIGITS, 6); + } + + /* set whether to show the sign */ + if (info->fShowSign) { + u_printf_set_sign(format, info, prefixBuffer, &prefixBufferLen, &status); + } + + /* format the number */ + resultLen = unum_formatDouble(format, num, result, UPRINTF_BUFFER_SIZE, 0, &status); + + if (U_FAILURE(status)) { + resultLen = 0; + } + + /* restore the number format */ + /* TODO: Is this needed? */ + unum_setAttribute(format, UNUM_MIN_FRACTION_DIGITS, minDecimalDigits); + unum_setAttribute(format, UNUM_MAX_FRACTION_DIGITS, maxDecimalDigits); + + /* Since we're the only one using the scientific + format, we don't need to save the old exponent value. */ + /*unum_setSymbol(format, + UNUM_EXPONENTIAL_SYMBOL, + srcExpBuf, + srcLen, + &status);*/ + + if (info->fShowSign) { + /* Reset back to original value regardless of what the error was */ + UErrorCode localStatus = U_ZERO_ERROR; + u_printf_reset_sign(format, info, prefixBuffer, &prefixBufferLen, &localStatus); + } + + return handler->pad_and_justify(context, info, result, resultLen); +} + +static int32_t +u_printf_percent_handler(const u_printf_stream_handler *handler, + void *context, + ULocaleBundle *formatBundle, + const u_printf_spec_info *info, + const ufmt_args *args) +{ + double num = (double) (args[0].doubleValue); + UNumberFormat *format; + UChar result[UPRINTF_BUFFER_SIZE]; + UChar prefixBuffer[UPRINTF_BUFFER_SIZE]; + int32_t prefixBufferLen = sizeof(prefixBuffer); + int32_t minDecimalDigits; + int32_t maxDecimalDigits; + int32_t resultLen; + UErrorCode status = U_ZERO_ERROR; + + prefixBuffer[0] = 0; + + /* mask off any necessary bits */ + /* if(! info->fIsLongDouble) + num &= DBL_MAX;*/ + + /* get the formatter */ + format = u_locbund_getNumberFormat(formatBundle, UNUM_PERCENT); + + /* handle error */ + if(format == 0) + return 0; + + /* save the formatter's state */ + minDecimalDigits = unum_getAttribute(format, UNUM_MIN_FRACTION_DIGITS); + maxDecimalDigits = unum_getAttribute(format, UNUM_MAX_FRACTION_DIGITS); + + /* set the appropriate flags and number of decimal digits on the formatter */ + if(info->fPrecision != -1) { + /* set the # of decimal digits */ + unum_setAttribute(format, UNUM_FRACTION_DIGITS, info->fPrecision); + } + else if(info->fAlt) { + /* '#' means always show decimal point */ + /* copy of printf behavior on Solaris - '#' shows 6 digits */ + unum_setAttribute(format, UNUM_FRACTION_DIGITS, 6); + } + else { + /* # of decimal digits is 6 if precision not specified */ + unum_setAttribute(format, UNUM_FRACTION_DIGITS, 6); + } + + /* set whether to show the sign */ + if (info->fShowSign) { + u_printf_set_sign(format, info, prefixBuffer, &prefixBufferLen, &status); + } + + /* format the number */ + resultLen = unum_formatDouble(format, num, result, UPRINTF_BUFFER_SIZE, 0, &status); + + if (U_FAILURE(status)) { + resultLen = 0; + } + + /* restore the number format */ + /* TODO: Is this needed? */ + unum_setAttribute(format, UNUM_MIN_FRACTION_DIGITS, minDecimalDigits); + unum_setAttribute(format, UNUM_MAX_FRACTION_DIGITS, maxDecimalDigits); + + if (info->fShowSign) { + /* Reset back to original value regardless of what the error was */ + UErrorCode localStatus = U_ZERO_ERROR; + u_printf_reset_sign(format, info, prefixBuffer, &prefixBufferLen, &localStatus); + } + + return handler->pad_and_justify(context, info, result, resultLen); +} + +static int32_t +u_printf_ustring_handler(const u_printf_stream_handler *handler, + void *context, + ULocaleBundle *formatBundle, + const u_printf_spec_info *info, + const ufmt_args *args) +{ + int32_t len, written; + const UChar *arg = (const UChar*)(args[0].ptrValue); + + /* allocate enough space for the buffer */ + if (arg == NULL) { + arg = gNullStr; + } + len = u_strlen(arg); + + /* width = minimum # of characters to write */ + /* precision = maximum # of characters to write */ + if (info->fPrecision != -1 && info->fPrecision < len) { + len = info->fPrecision; + } + + /* determine if the string should be padded */ + written = handler->pad_and_justify(context, info, arg, len); + + return written; +} + +static int32_t +u_printf_uchar_handler(const u_printf_stream_handler *handler, + void *context, + ULocaleBundle *formatBundle, + const u_printf_spec_info *info, + const ufmt_args *args) +{ + int32_t written = 0; + UChar arg = (UChar)(args[0].int64Value); + + /* width = minimum # of characters to write */ + /* precision = maximum # of characters to write */ + /* precision is ignored when handling a uchar */ + + /* determine if the string should be padded */ + written = handler->pad_and_justify(context, info, &arg, 1); + + return written; +} + +static int32_t +u_printf_scidbl_handler(const u_printf_stream_handler *handler, + void *context, + ULocaleBundle *formatBundle, + const u_printf_spec_info *info, + const ufmt_args *args) +{ + u_printf_spec_info scidbl_info; + double num = args[0].doubleValue; + int32_t retVal; + UNumberFormat *format; + int32_t maxSigDecimalDigits, significantDigits; + + memcpy(&scidbl_info, info, sizeof(u_printf_spec_info)); + + /* determine whether to use 'd', 'e' or 'f' notation */ + if (scidbl_info.fPrecision == -1 && num == uprv_trunc(num)) + { + /* use 'f' notation */ + scidbl_info.fSpec = 0x0066; + scidbl_info.fPrecision = 0; + /* call the double handler */ + retVal = u_printf_double_handler(handler, context, formatBundle, &scidbl_info, args); + } + else if(num < 0.0001 || (scidbl_info.fPrecision < 1 && 1000000.0 <= num) + || (scidbl_info.fPrecision != -1 && num > uprv_pow10(scidbl_info.fPrecision))) + { + /* use 'e' or 'E' notation */ + scidbl_info.fSpec = scidbl_info.fSpec - 2; + if (scidbl_info.fPrecision == -1) { + scidbl_info.fPrecision = 5; + } + /* call the scientific handler */ + retVal = u_printf_scientific_handler(handler, context, formatBundle, &scidbl_info, args); + } + else { + format = u_locbund_getNumberFormat(formatBundle, UNUM_DECIMAL); + /* Check for null pointer */ + if (format == NULL) { + return 0; + } + maxSigDecimalDigits = unum_getAttribute(format, UNUM_MAX_SIGNIFICANT_DIGITS); + significantDigits = scidbl_info.fPrecision; + + /* use 'f' notation */ + scidbl_info.fSpec = 0x0066; + if (significantDigits == -1) { + significantDigits = 6; + } + unum_setAttribute(format, UNUM_SIGNIFICANT_DIGITS_USED, TRUE); + unum_setAttribute(format, UNUM_MAX_SIGNIFICANT_DIGITS, significantDigits); + /* call the double handler */ + retVal = u_printf_double_handler(handler, context, formatBundle, &scidbl_info, args); + unum_setAttribute(format, UNUM_MAX_SIGNIFICANT_DIGITS, maxSigDecimalDigits); + unum_setAttribute(format, UNUM_SIGNIFICANT_DIGITS_USED, FALSE); + } + return retVal; +} + +static int32_t +u_printf_count_handler(const u_printf_stream_handler *handler, + void *context, + ULocaleBundle *formatBundle, + const u_printf_spec_info *info, + const ufmt_args *args) +{ + int32_t *count = (int32_t*)(args[0].ptrValue); + + /* in the special case of count, the u_printf_spec_info's width */ + /* will contain the # of chars written thus far */ + *count = info->fWidth; + + return 0; +} + +static int32_t +u_printf_spellout_handler(const u_printf_stream_handler *handler, + void *context, + ULocaleBundle *formatBundle, + const u_printf_spec_info *info, + const ufmt_args *args) +{ + double num = (double) (args[0].doubleValue); + UNumberFormat *format; + UChar result[UPRINTF_BUFFER_SIZE]; + UChar prefixBuffer[UPRINTF_BUFFER_SIZE]; + int32_t prefixBufferLen = sizeof(prefixBuffer); + int32_t minDecimalDigits; + int32_t maxDecimalDigits; + int32_t resultLen; + UErrorCode status = U_ZERO_ERROR; + + prefixBuffer[0] = 0; + + /* mask off any necessary bits */ + /* if(! info->fIsLongDouble) + num &= DBL_MAX;*/ + + /* get the formatter */ + format = u_locbund_getNumberFormat(formatBundle, UNUM_SPELLOUT); + + /* handle error */ + if(format == 0) + return 0; + + /* save the formatter's state */ + minDecimalDigits = unum_getAttribute(format, UNUM_MIN_FRACTION_DIGITS); + maxDecimalDigits = unum_getAttribute(format, UNUM_MAX_FRACTION_DIGITS); + + /* set the appropriate flags and number of decimal digits on the formatter */ + if(info->fPrecision != -1) { + /* set the # of decimal digits */ + unum_setAttribute(format, UNUM_FRACTION_DIGITS, info->fPrecision); + } + else if(info->fAlt) { + /* '#' means always show decimal point */ + /* copy of printf behavior on Solaris - '#' shows 6 digits */ + unum_setAttribute(format, UNUM_FRACTION_DIGITS, 6); + } + else { + /* # of decimal digits is 6 if precision not specified */ + unum_setAttribute(format, UNUM_FRACTION_DIGITS, 6); + } + + /* set whether to show the sign */ + if (info->fShowSign) { + u_printf_set_sign(format, info, prefixBuffer, &prefixBufferLen, &status); + } + + /* format the number */ + resultLen = unum_formatDouble(format, num, result, UPRINTF_BUFFER_SIZE, 0, &status); + + if (U_FAILURE(status)) { + resultLen = 0; + } + + /* restore the number format */ + /* TODO: Is this needed? */ + unum_setAttribute(format, UNUM_MIN_FRACTION_DIGITS, minDecimalDigits); + unum_setAttribute(format, UNUM_MAX_FRACTION_DIGITS, maxDecimalDigits); + + if (info->fShowSign) { + /* Reset back to original value regardless of what the error was */ + UErrorCode localStatus = U_ZERO_ERROR; + u_printf_reset_sign(format, info, prefixBuffer, &prefixBufferLen, &localStatus); + } + + return handler->pad_and_justify(context, info, result, resultLen); +} + +/* Use US-ASCII characters only for formatting. Most codepages have + characters 20-7F from Unicode. Using any other codepage specific + characters will make it very difficult to format the string on + non-Unicode machines */ +static const u_printf_info g_u_printf_infos[UPRINTF_NUM_FMT_HANDLERS] = { +/* 0x20 */ + UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, + UFMT_EMPTY, UFMT_SIMPLE_PERCENT,UFMT_EMPTY, UFMT_EMPTY, + UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, + UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, + +/* 0x30 */ + UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, + UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, + UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, + UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, + +/* 0x40 */ + UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_UCHAR, + UFMT_EMPTY, UFMT_SCIENTIFIC, UFMT_EMPTY, UFMT_SCIDBL, +#ifdef U_USE_OBSOLETE_IO_FORMATTING + UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_UCHAR/*deprecated*/, +#else + UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, +#endif + UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, + +/* 0x50 */ + UFMT_PERCENT, UFMT_EMPTY, UFMT_EMPTY, UFMT_USTRING, +#ifdef U_USE_OBSOLETE_IO_FORMATTING + UFMT_EMPTY, UFMT_USTRING/*deprecated*/,UFMT_SPELLOUT, UFMT_EMPTY, +#else + UFMT_EMPTY, UFMT_EMPTY, UFMT_SPELLOUT, UFMT_EMPTY, +#endif + UFMT_HEX, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, + UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, + +/* 0x60 */ + UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_CHAR, + UFMT_INT, UFMT_SCIENTIFIC, UFMT_DOUBLE, UFMT_SCIDBL, + UFMT_EMPTY, UFMT_INT, UFMT_EMPTY, UFMT_EMPTY, + UFMT_EMPTY, UFMT_EMPTY, UFMT_COUNT, UFMT_OCTAL, + +/* 0x70 */ + UFMT_POINTER, UFMT_EMPTY, UFMT_EMPTY, UFMT_STRING, + UFMT_EMPTY, UFMT_UINT, UFMT_EMPTY, UFMT_EMPTY, + UFMT_HEX, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, + UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, +}; + +/* flag characters for uprintf */ +#define FLAG_MINUS 0x002D +#define FLAG_PLUS 0x002B +#define FLAG_SPACE 0x0020 +#define FLAG_POUND 0x0023 +#define FLAG_ZERO 0x0030 +#define FLAG_PAREN 0x0028 + +#define ISFLAG(s) (s) == FLAG_MINUS || \ + (s) == FLAG_PLUS || \ + (s) == FLAG_SPACE || \ + (s) == FLAG_POUND || \ + (s) == FLAG_ZERO || \ + (s) == FLAG_PAREN + +/* special characters for uprintf */ +#define SPEC_ASTERISK 0x002A +#define SPEC_DOLLARSIGN 0x0024 +#define SPEC_PERIOD 0x002E +#define SPEC_PERCENT 0x0025 + +/* unicode digits */ +#define DIGIT_ZERO 0x0030 +#define DIGIT_ONE 0x0031 +#define DIGIT_TWO 0x0032 +#define DIGIT_THREE 0x0033 +#define DIGIT_FOUR 0x0034 +#define DIGIT_FIVE 0x0035 +#define DIGIT_SIX 0x0036 +#define DIGIT_SEVEN 0x0037 +#define DIGIT_EIGHT 0x0038 +#define DIGIT_NINE 0x0039 + +#define ISDIGIT(s) (s) == DIGIT_ZERO || \ + (s) == DIGIT_ONE || \ + (s) == DIGIT_TWO || \ + (s) == DIGIT_THREE || \ + (s) == DIGIT_FOUR || \ + (s) == DIGIT_FIVE || \ + (s) == DIGIT_SIX || \ + (s) == DIGIT_SEVEN || \ + (s) == DIGIT_EIGHT || \ + (s) == DIGIT_NINE + +/* u_printf modifiers */ +#define MOD_H 0x0068 +#define MOD_LOWERL 0x006C +#define MOD_L 0x004C + +#define ISMOD(s) (s) == MOD_H || \ + (s) == MOD_LOWERL || \ + (s) == MOD_L +/* Returns an array of the parsed argument type given in the format string. */ +static ufmt_args* parseArguments(const UChar *alias, va_list ap, UErrorCode *status) { + ufmt_args *arglist = NULL; + ufmt_type_info *typelist = NULL; + UBool *islonglong = NULL; + int32_t size = 0; + int32_t pos = 0; + UChar type; + uint16_t handlerNum; + const UChar *aliasStart = alias; + + /* get maximum number of arguments */ + for(;;) { + /* find % */ + while(*alias != UP_PERCENT && *alias != 0x0000) { + alias++; + } + + if(*alias == 0x0000) { + break; + } + + alias++; + + /* handle the pos number */ + if(ISDIGIT(*alias)) { + + /* handle positional parameters */ + if(ISDIGIT(*alias)) { + pos = (int) (*alias++ - DIGIT_ZERO); + + while(ISDIGIT(*alias)) { + pos *= 10; + pos += (int) (*alias++ - DIGIT_ZERO); + } + } + + /* if there is no '$', don't read anything */ + if(*alias != SPEC_DOLLARSIGN) { + return NULL; + } + } else { + return NULL; + } + + if (pos > size) { + size = pos; + } + } + + /* create the parsed argument list */ + typelist = (ufmt_type_info*)uprv_malloc(sizeof(ufmt_type_info) * size); + islonglong = (UBool*)uprv_malloc(sizeof(UBool) * size); + arglist = (ufmt_args*)uprv_malloc(sizeof(ufmt_args) * size); + + /* If malloc failed, return NULL */ + if (!typelist || !islonglong || !arglist) { + if (typelist) { + uprv_free(typelist); + } + + if (islonglong) { + uprv_free(islonglong); + } + + if (arglist) { + uprv_free(arglist); + } + + *status = U_MEMORY_ALLOCATION_ERROR; + return NULL; + } + + /* reset alias back to the beginning */ + alias = aliasStart; + + for(;;) { + /* find % */ + while(*alias != UP_PERCENT && *alias != 0x0000) { + alias++; + } + + if(*alias == 0x0000) { + break; + } + + alias++; + + /* handle positional parameters */ + if(ISDIGIT(*alias)) { + pos = (int) (*alias++ - DIGIT_ZERO); + + while(ISDIGIT(*alias)) { + pos *= 10; + pos += (int) (*alias++ - DIGIT_ZERO); + } + } + /* offset position by 1 */ + pos--; + + /* skip over everything except for the type */ + while (ISMOD(*alias) || ISFLAG(*alias) || ISDIGIT(*alias) || + *alias == SPEC_ASTERISK || *alias == SPEC_PERIOD || *alias == SPEC_DOLLARSIGN) { + islonglong[pos] = FALSE; + if (ISMOD(*alias)) { + alias++; + if (*alias == MOD_LOWERL) { + islonglong[pos] = TRUE; + } + } + alias++; + } + type = *alias; + + /* store the argument type in the correct position of the parsed argument list */ + handlerNum = (uint16_t)(type - UPRINTF_BASE_FMT_HANDLERS); + if (handlerNum < UPRINTF_NUM_FMT_HANDLERS) { + typelist[pos] = g_u_printf_infos[ handlerNum ].info; + } else { + typelist[pos] = ufmt_empty; + } + } + + /* store argument in arglist */ + for (pos = 0; pos < size; pos++) { + switch (typelist[pos]) { + case ufmt_string: + case ufmt_ustring: + case ufmt_pointer: + arglist[pos].ptrValue = va_arg(ap, void*); + break; + case ufmt_char: + case ufmt_uchar: + case ufmt_int: + if (islonglong[pos]) { + arglist[pos].int64Value = va_arg(ap, int64_t); + } + else { + arglist[pos].int64Value = va_arg(ap, int32_t); + } + break; + case ufmt_float: + arglist[pos].floatValue = (float) va_arg(ap, double); + break; + case ufmt_double: + arglist[pos].doubleValue = va_arg(ap, double); + break; + default: + /* else args is ignored */ + arglist[pos].ptrValue = NULL; + break; + } + } + + uprv_free(typelist); + uprv_free(islonglong); + + return arglist; +} + +/* We parse the argument list in Unicode */ +U_CFUNC int32_t +u_printf_parse(const u_printf_stream_handler *streamHandler, + const UChar *fmt, + void *context, + u_localized_print_string *locStringContext, + ULocaleBundle *formatBundle, + int32_t *written, + va_list ap) +{ + uint16_t handlerNum; + ufmt_args args; + ufmt_type_info argType; + u_printf_handler *handler; + u_printf_spec spec; + u_printf_spec_info *info = &(spec.fInfo); + + const UChar *alias = fmt; + const UChar *backup; + const UChar *lastAlias; + const UChar *orgAlias = fmt; + /* parsed argument list */ + ufmt_args *arglist = NULL; /* initialized it to avoid compiler warnings */ + UErrorCode status = U_ZERO_ERROR; + if (!locStringContext || locStringContext->available >= 0) { + /* get the parsed list of argument types */ + arglist = parseArguments(orgAlias, ap, &status); + + /* Return error if parsing failed. */ + if (U_FAILURE(status)) { + return -1; + } + } + + /* iterate through the pattern */ + while(!locStringContext || locStringContext->available >= 0) { + + /* find the next '%' */ + lastAlias = alias; + while(*alias != UP_PERCENT && *alias != 0x0000) { + alias++; + } + + /* write any characters before the '%' */ + if(alias > lastAlias) { + *written += (streamHandler->write)(context, lastAlias, (int32_t)(alias - lastAlias)); + } + + /* break if at end of string */ + if(*alias == 0x0000) { + break; + } + + /* initialize spec to default values */ + spec.fWidthPos = -1; + spec.fPrecisionPos = -1; + spec.fArgPos = -1; + + uprv_memset(info, 0, sizeof(*info)); + info->fPrecision = -1; + info->fWidth = -1; + info->fPadChar = 0x0020; + + /* skip over the initial '%' */ + alias++; + + /* Check for positional argument */ + if(ISDIGIT(*alias)) { + + /* Save the current position */ + backup = alias; + + /* handle positional parameters */ + if(ISDIGIT(*alias)) { + spec.fArgPos = (int) (*alias++ - DIGIT_ZERO); + + while(ISDIGIT(*alias)) { + spec.fArgPos *= 10; + spec.fArgPos += (int) (*alias++ - DIGIT_ZERO); + } + } + + /* if there is no '$', don't read anything */ + if(*alias != SPEC_DOLLARSIGN) { + spec.fArgPos = -1; + alias = backup; + } + /* munge the '$' */ + else + alias++; + } + + /* Get any format flags */ + while(ISFLAG(*alias)) { + switch(*alias++) { + + /* left justify */ + case FLAG_MINUS: + info->fLeft = TRUE; + break; + + /* always show sign */ + case FLAG_PLUS: + info->fShowSign = TRUE; + break; + + /* use space if no sign present */ + case FLAG_SPACE: + info->fShowSign = TRUE; + info->fSpace = TRUE; + break; + + /* use alternate form */ + case FLAG_POUND: + info->fAlt = TRUE; + break; + + /* pad with leading zeroes */ + case FLAG_ZERO: + info->fZero = TRUE; + info->fPadChar = 0x0030; + break; + + /* pad character specified */ + case FLAG_PAREN: + + /* TODO test that all four are numbers */ + /* first four characters are hex values for pad char */ + info->fPadChar = (UChar)ufmt_digitvalue(*alias++); + info->fPadChar = (UChar)((info->fPadChar * 16) + ufmt_digitvalue(*alias++)); + info->fPadChar = (UChar)((info->fPadChar * 16) + ufmt_digitvalue(*alias++)); + info->fPadChar = (UChar)((info->fPadChar * 16) + ufmt_digitvalue(*alias++)); + + /* final character is ignored */ + alias++; + + break; + } + } + + /* Get the width */ + + /* width is specified out of line */ + if(*alias == SPEC_ASTERISK) { + + info->fWidth = -2; + + /* Skip the '*' */ + alias++; + + /* Save the current position */ + backup = alias; + + /* handle positional parameters */ + if(ISDIGIT(*alias)) { + spec.fWidthPos = (int) (*alias++ - DIGIT_ZERO); + + while(ISDIGIT(*alias)) { + spec.fWidthPos *= 10; + spec.fWidthPos += (int) (*alias++ - DIGIT_ZERO); + } + } + + /* if there is no '$', don't read anything */ + if(*alias != SPEC_DOLLARSIGN) { + spec.fWidthPos = -1; + alias = backup; + } + /* munge the '$' */ + else + alias++; + } + /* read the width, if present */ + else if(ISDIGIT(*alias)){ + info->fWidth = (int) (*alias++ - DIGIT_ZERO); + + while(ISDIGIT(*alias)) { + info->fWidth *= 10; + info->fWidth += (int) (*alias++ - DIGIT_ZERO); + } + } + + /* Get the precision */ + + if(*alias == SPEC_PERIOD) { + + /* eat up the '.' */ + alias++; + + /* precision is specified out of line */ + if(*alias == SPEC_ASTERISK) { + + info->fPrecision = -2; + + /* Skip the '*' */ + alias++; + + /* save the current position */ + backup = alias; + + /* handle positional parameters */ + if(ISDIGIT(*alias)) { + spec.fPrecisionPos = (int) (*alias++ - DIGIT_ZERO); + + while(ISDIGIT(*alias)) { + spec.fPrecisionPos *= 10; + spec.fPrecisionPos += (int) (*alias++ - DIGIT_ZERO); + } + + /* if there is no '$', don't read anything */ + if(*alias != SPEC_DOLLARSIGN) { + spec.fPrecisionPos = -1; + alias = backup; + } + else { + /* munge the '$' */ + alias++; + } + } + } + /* read the precision */ + else if(ISDIGIT(*alias)){ + info->fPrecision = (int) (*alias++ - DIGIT_ZERO); + + while(ISDIGIT(*alias)) { + info->fPrecision *= 10; + info->fPrecision += (int) (*alias++ - DIGIT_ZERO); + } + } + } + + /* Get any modifiers */ + if(ISMOD(*alias)) { + switch(*alias++) { + + /* short */ + case MOD_H: + info->fIsShort = TRUE; + break; + + /* long or long long */ + case MOD_LOWERL: + if(*alias == MOD_LOWERL) { + info->fIsLongLong = TRUE; + /* skip over the next 'l' */ + alias++; + } + else + info->fIsLong = TRUE; + break; + + /* long double */ + case MOD_L: + info->fIsLongDouble = TRUE; + break; + } + } + + /* finally, get the specifier letter */ + info->fSpec = *alias++; + info->fOrigSpec = info->fSpec; + + /* fill in the precision and width, if specified out of line */ + + /* width specified out of line */ + if(spec.fInfo.fWidth == -2) { + if(spec.fWidthPos == -1) { + /* read the width from the argument list */ + info->fWidth = va_arg(ap, int32_t); + } + /* else handle positional parameter */ + + /* if it's negative, take the absolute value and set left alignment */ + if(info->fWidth < 0) { + info->fWidth *= -1; /* Make positive */ + info->fLeft = TRUE; + } + } + + /* precision specified out of line */ + if(info->fPrecision == -2) { + if(spec.fPrecisionPos == -1) { + /* read the precision from the argument list */ + info->fPrecision = va_arg(ap, int32_t); + } + /* else handle positional parameter */ + + /* if it's negative, set it to zero */ + if(info->fPrecision < 0) + info->fPrecision = 0; + } + + handlerNum = (uint16_t)(info->fSpec - UPRINTF_BASE_FMT_HANDLERS); + if (handlerNum < UPRINTF_NUM_FMT_HANDLERS) { + /* query the info function for argument information */ + argType = g_u_printf_infos[ handlerNum ].info; + + /* goto the correct argument on arg_list if position is specified */ + if (spec.fArgPos > 0) { + /* offset position by 1 */ + spec.fArgPos--; + switch(argType) { + case ufmt_count: + /* set the spec's width to the # of chars written */ + info->fWidth = *written; + /* fall through to set the pointer */ + U_FALLTHROUGH; + case ufmt_string: + case ufmt_ustring: + case ufmt_pointer: + args.ptrValue = arglist[spec.fArgPos].ptrValue; + break; + case ufmt_char: + case ufmt_uchar: + case ufmt_int: + args.int64Value = arglist[spec.fArgPos].int64Value; + break; + case ufmt_float: + args.floatValue = arglist[spec.fArgPos].floatValue; + break; + case ufmt_double: + args.doubleValue = arglist[spec.fArgPos].doubleValue; + break; + default: + /* else args is ignored */ + args.ptrValue = NULL; + break; + } + } else { /* no positional argument specified */ + switch(argType) { + case ufmt_count: + /* set the spec's width to the # of chars written */ + info->fWidth = *written; + /* fall through to set the pointer */ + U_FALLTHROUGH; + case ufmt_string: + case ufmt_ustring: + case ufmt_pointer: + args.ptrValue = va_arg(ap, void*); + break; + case ufmt_char: + case ufmt_uchar: + case ufmt_int: + if (info->fIsLongLong) { + args.int64Value = va_arg(ap, int64_t); + } + else { + args.int64Value = va_arg(ap, int32_t); + } + break; + case ufmt_float: + args.floatValue = (float) va_arg(ap, double); + break; + case ufmt_double: + args.doubleValue = va_arg(ap, double); + break; + default: + /* else args is ignored */ + args.ptrValue = NULL; + break; + } + } + + /* call the handler function */ + handler = g_u_printf_infos[ handlerNum ].handler; + if(handler != 0) { + *written += (*handler)(streamHandler, context, formatBundle, info, &args); + } + else { + /* just echo unknown tags */ + *written += (streamHandler->write)(context, fmt, (int32_t)(alias - lastAlias)); + } + } + else { + /* just echo unknown tags */ + *written += (streamHandler->write)(context, fmt, (int32_t)(alias - lastAlias)); + } + } + /* delete parsed argument list */ + if (arglist != NULL) { + uprv_free(arglist); + } + /* return # of characters in this format that have been parsed. */ + return (int32_t)(alias - fmt); +} + +#endif /* #if !UCONFIG_NO_FORMATTING */ diff --git a/intl/icu/source/io/uscanf.c b/intl/icu/source/io/uscanf.c new file mode 100644 index 000000000..34368dade --- /dev/null +++ b/intl/icu/source/io/uscanf.c @@ -0,0 +1,108 @@ +// Copyright (C) 2016 and later: Unicode, Inc. and others. +// License & terms of use: http://www.unicode.org/copyright.html +/* +****************************************************************************** +* +* Copyright (C) 1998-2014, International Business Machines +* Corporation and others. All Rights Reserved. +* +****************************************************************************** +* +* File uscanf.c +* +* Modification History: +* +* Date Name Description +* 12/02/98 stephen Creation. +* 03/13/99 stephen Modified for new C API. +****************************************************************************** +*/ + +#include "unicode/utypes.h" + +#if !UCONFIG_NO_FORMATTING && !UCONFIG_NO_CONVERSION + +#include "unicode/putil.h" +#include "unicode/ustdio.h" +#include "unicode/ustring.h" +#include "uscanf.h" +#include "ufile.h" +#include "ufmt_cmn.h" + +#include "cmemory.h" +#include "cstring.h" + + +U_CAPI int32_t U_EXPORT2 +u_fscanf(UFILE *f, + const char *patternSpecification, + ... ) +{ + va_list ap; + int32_t converted; + + va_start(ap, patternSpecification); + converted = u_vfscanf(f, patternSpecification, ap); + va_end(ap); + + return converted; +} + +U_CAPI int32_t U_EXPORT2 +u_fscanf_u(UFILE *f, + const UChar *patternSpecification, + ... ) +{ + va_list ap; + int32_t converted; + + va_start(ap, patternSpecification); + converted = u_vfscanf_u(f, patternSpecification, ap); + va_end(ap); + + return converted; +} + +U_CAPI int32_t U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */ +u_vfscanf(UFILE *f, + const char *patternSpecification, + va_list ap) +{ + int32_t converted; + UChar *pattern; + UChar patBuffer[UFMT_DEFAULT_BUFFER_SIZE]; + int32_t size = (int32_t)uprv_strlen(patternSpecification) + 1; + + /* convert from the default codepage to Unicode */ + if (size >= MAX_UCHAR_BUFFER_SIZE(patBuffer)) { + pattern = (UChar *)uprv_malloc(size * sizeof(UChar)); + if(pattern == 0) { + return 0; + } + } + else { + pattern = patBuffer; + } + u_charsToUChars(patternSpecification, pattern, size); + + /* do the work */ + converted = u_vfscanf_u(f, pattern, ap); + + /* clean up */ + if (pattern != patBuffer) { + uprv_free(pattern); + } + + return converted; +} + +U_CAPI int32_t U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */ +u_vfscanf_u(UFILE *f, + const UChar *patternSpecification, + va_list ap) +{ + return u_scanf_parse(f, patternSpecification, ap); +} + +#endif /* #if !UCONFIG_NO_FORMATTING */ + diff --git a/intl/icu/source/io/uscanf.h b/intl/icu/source/io/uscanf.h new file mode 100644 index 000000000..1140eee1c --- /dev/null +++ b/intl/icu/source/io/uscanf.h @@ -0,0 +1,38 @@ +// Copyright (C) 2016 and later: Unicode, Inc. and others. +// License & terms of use: http://www.unicode.org/copyright.html +/* +****************************************************************************** +* +* Copyright (C) 1998-2014, International Business Machines +* Corporation and others. All Rights Reserved. +* +****************************************************************************** +* +* File uscanf.h +* +* Modification History: +* +* Date Name Description +* 12/02/98 stephen Creation. +* 03/13/99 stephen Modified for new C API. +****************************************************************************** +*/ + +#ifndef USCANF_H +#define USCANF_H + +#include "unicode/utypes.h" + +#if !UCONFIG_NO_FORMATTING && !UCONFIG_NO_CONVERSION + +#include "unicode/ustdio.h" + +U_CFUNC int32_t +u_scanf_parse(UFILE *f, + const UChar *patternSpecification, + va_list ap); + +#endif /* #if !UCONFIG_NO_FORMATTING */ + +#endif + diff --git a/intl/icu/source/io/uscanf_p.c b/intl/icu/source/io/uscanf_p.c new file mode 100644 index 000000000..ded984cef --- /dev/null +++ b/intl/icu/source/io/uscanf_p.c @@ -0,0 +1,1408 @@ +// Copyright (C) 2016 and later: Unicode, Inc. and others. +// License & terms of use: http://www.unicode.org/copyright.html +/* +******************************************************************************* +* +* Copyright (C) 1998-2016, International Business Machines +* Corporation and others. All Rights Reserved. +* +******************************************************************************* +* +* File uscnnf_p.c +* +* Modification History: +* +* Date Name Description +* 12/02/98 stephen Creation. +* 03/13/99 stephen Modified for new C API. +******************************************************************************* +*/ + +#include "unicode/utypes.h" + +#if !UCONFIG_NO_FORMATTING && !UCONFIG_NO_CONVERSION + +#include "unicode/uchar.h" +#include "unicode/ustring.h" +#include "unicode/unum.h" +#include "unicode/udat.h" +#include "unicode/uset.h" +#include "uscanf.h" +#include "ufmt_cmn.h" +#include "ufile.h" +#include "locbund.h" + +#include "cmemory.h" +#include "ustr_cnv.h" + +/* flag characters for u_scanf */ +#define FLAG_ASTERISK 0x002A +#define FLAG_PAREN 0x0028 + +#define ISFLAG(s) (s) == FLAG_ASTERISK || \ + (s) == FLAG_PAREN + +/* special characters for u_scanf */ +#define SPEC_DOLLARSIGN 0x0024 + +/* unicode digits */ +#define DIGIT_ZERO 0x0030 +#define DIGIT_ONE 0x0031 +#define DIGIT_TWO 0x0032 +#define DIGIT_THREE 0x0033 +#define DIGIT_FOUR 0x0034 +#define DIGIT_FIVE 0x0035 +#define DIGIT_SIX 0x0036 +#define DIGIT_SEVEN 0x0037 +#define DIGIT_EIGHT 0x0038 +#define DIGIT_NINE 0x0039 + +#define ISDIGIT(s) (s) == DIGIT_ZERO || \ + (s) == DIGIT_ONE || \ + (s) == DIGIT_TWO || \ + (s) == DIGIT_THREE || \ + (s) == DIGIT_FOUR || \ + (s) == DIGIT_FIVE || \ + (s) == DIGIT_SIX || \ + (s) == DIGIT_SEVEN || \ + (s) == DIGIT_EIGHT || \ + (s) == DIGIT_NINE + +/* u_scanf modifiers */ +#define MOD_H 0x0068 +#define MOD_LOWERL 0x006C +#define MOD_L 0x004C + +#define ISMOD(s) (s) == MOD_H || \ + (s) == MOD_LOWERL || \ + (s) == MOD_L + +/** + * Struct encapsulating a single uscanf format specification. + */ +typedef struct u_scanf_spec_info { + int32_t fWidth; /* Width */ + + UChar fSpec; /* Format specification */ + + UChar fPadChar; /* Padding character */ + + UBool fSkipArg; /* TRUE if arg should be skipped */ + UBool fIsLongDouble; /* L flag */ + UBool fIsShort; /* h flag */ + UBool fIsLong; /* l flag */ + UBool fIsLongLong; /* ll flag */ + UBool fIsString; /* TRUE if this is a NULL-terminated string. */ +} u_scanf_spec_info; + + +/** + * Struct encapsulating a single u_scanf format specification. + */ +typedef struct u_scanf_spec { + u_scanf_spec_info fInfo; /* Information on this spec */ + int32_t fArgPos; /* Position of data in arg list */ +} u_scanf_spec; + +/** + * Parse a single u_scanf format specifier in Unicode. + * @param fmt A pointer to a '%' character in a u_scanf format specification. + * @param spec A pointer to a <TT>u_scanf_spec</TT> to receive the parsed + * format specifier. + * @return The number of characters contained in this specifier. + */ +static int32_t +u_scanf_parse_spec (const UChar *fmt, + u_scanf_spec *spec) +{ + const UChar *s = fmt; + const UChar *backup; + u_scanf_spec_info *info = &(spec->fInfo); + + /* initialize spec to default values */ + spec->fArgPos = -1; + + info->fWidth = -1; + info->fSpec = 0x0000; + info->fPadChar = 0x0020; + info->fSkipArg = FALSE; + info->fIsLongDouble = FALSE; + info->fIsShort = FALSE; + info->fIsLong = FALSE; + info->fIsLongLong = FALSE; + info->fIsString = TRUE; + + + /* skip over the initial '%' */ + s++; + + /* Check for positional argument */ + if(ISDIGIT(*s)) { + + /* Save the current position */ + backup = s; + + /* handle positional parameters */ + if(ISDIGIT(*s)) { + spec->fArgPos = (int) (*s++ - DIGIT_ZERO); + + while(ISDIGIT(*s)) { + spec->fArgPos *= 10; + spec->fArgPos += (int) (*s++ - DIGIT_ZERO); + } + } + + /* if there is no '$', don't read anything */ + if(*s != SPEC_DOLLARSIGN) { + spec->fArgPos = -1; + s = backup; + } + /* munge the '$' */ + else + s++; + } + + /* Get any format flags */ + while(ISFLAG(*s)) { + switch(*s++) { + + /* skip argument */ + case FLAG_ASTERISK: + info->fSkipArg = TRUE; + break; + + /* pad character specified */ + case FLAG_PAREN: + + /* first four characters are hex values for pad char */ + info->fPadChar = (UChar)ufmt_digitvalue(*s++); + info->fPadChar = (UChar)((info->fPadChar * 16) + ufmt_digitvalue(*s++)); + info->fPadChar = (UChar)((info->fPadChar * 16) + ufmt_digitvalue(*s++)); + info->fPadChar = (UChar)((info->fPadChar * 16) + ufmt_digitvalue(*s++)); + + /* final character is ignored */ + s++; + + break; + } + } + + /* Get the width */ + if(ISDIGIT(*s)){ + info->fWidth = (int) (*s++ - DIGIT_ZERO); + + while(ISDIGIT(*s)) { + info->fWidth *= 10; + info->fWidth += (int) (*s++ - DIGIT_ZERO); + } + } + + /* Get any modifiers */ + if(ISMOD(*s)) { + switch(*s++) { + + /* short */ + case MOD_H: + info->fIsShort = TRUE; + break; + + /* long or long long */ + case MOD_LOWERL: + if(*s == MOD_LOWERL) { + info->fIsLongLong = TRUE; + /* skip over the next 'l' */ + s++; + } + else + info->fIsLong = TRUE; + break; + + /* long double */ + case MOD_L: + info->fIsLongDouble = TRUE; + break; + } + } + + /* finally, get the specifier letter */ + info->fSpec = *s++; + + /* return # of characters in this specifier */ + return (int32_t)(s - fmt); +} + +#define UP_PERCENT 0x0025 + + +/* ANSI style formatting */ +/* Use US-ASCII characters only for formatting */ + +/* % */ +#define UFMT_SIMPLE_PERCENT {ufmt_simple_percent, u_scanf_simple_percent_handler} +/* s */ +#define UFMT_STRING {ufmt_string, u_scanf_string_handler} +/* c */ +#define UFMT_CHAR {ufmt_string, u_scanf_char_handler} +/* d, i */ +#define UFMT_INT {ufmt_int, u_scanf_integer_handler} +/* u */ +#define UFMT_UINT {ufmt_int, u_scanf_uinteger_handler} +/* o */ +#define UFMT_OCTAL {ufmt_int, u_scanf_octal_handler} +/* x, X */ +#define UFMT_HEX {ufmt_int, u_scanf_hex_handler} +/* f */ +#define UFMT_DOUBLE {ufmt_double, u_scanf_double_handler} +/* e, E */ +#define UFMT_SCIENTIFIC {ufmt_double, u_scanf_scientific_handler} +/* g, G */ +#define UFMT_SCIDBL {ufmt_double, u_scanf_scidbl_handler} +/* n */ +#define UFMT_COUNT {ufmt_count, u_scanf_count_handler} +/* [ */ +#define UFMT_SCANSET {ufmt_string, u_scanf_scanset_handler} + +/* non-ANSI extensions */ +/* Use US-ASCII characters only for formatting */ + +/* p */ +#define UFMT_POINTER {ufmt_pointer, u_scanf_pointer_handler} +/* V */ +#define UFMT_SPELLOUT {ufmt_double, u_scanf_spellout_handler} +/* P */ +#define UFMT_PERCENT {ufmt_double, u_scanf_percent_handler} +/* C K is old format */ +#define UFMT_UCHAR {ufmt_uchar, u_scanf_uchar_handler} +/* S U is old format */ +#define UFMT_USTRING {ufmt_ustring, u_scanf_ustring_handler} + + +#define UFMT_EMPTY {ufmt_empty, NULL} + +/** + * A u_scanf handler function. + * A u_scanf handler is responsible for handling a single u_scanf + * format specification, for example 'd' or 's'. + * @param stream The UFILE to which to write output. + * @param info A pointer to a <TT>u_scanf_spec_info</TT> struct containing + * information on the format specification. + * @param args A pointer to the argument data + * @param fmt A pointer to the first character in the format string + * following the spec. + * @param fmtConsumed On output, set to the number of characters consumed + * in <TT>fmt</TT>. Do nothing, if the argument isn't variable width. + * @param argConverted The number of arguments converted and assigned, or -1 if an + * error occurred. + * @return The number of code points consumed during reading. + */ +typedef int32_t (*u_scanf_handler) (UFILE *stream, + u_scanf_spec_info *info, + ufmt_args *args, + const UChar *fmt, + int32_t *fmtConsumed, + int32_t *argConverted); + +typedef struct u_scanf_info { + ufmt_type_info info; + u_scanf_handler handler; +} u_scanf_info; + +#define USCANF_NUM_FMT_HANDLERS 108 +#define USCANF_SYMBOL_BUFFER_SIZE 8 + +/* We do not use handlers for 0-0x1f */ +#define USCANF_BASE_FMT_HANDLERS 0x20 + + +static int32_t +u_scanf_skip_leading_ws(UFILE *input, + UChar pad) +{ + UChar c; + int32_t count = 0; + UBool isNotEOF; + + /* skip all leading ws in the input */ + while( (isNotEOF = ufile_getch(input, &c)) && (c == pad || u_isWhitespace(c)) ) + { + count++; + } + + /* put the final character back on the input */ + if(isNotEOF) + u_fungetc(c, input); + + return count; +} + +/* TODO: Is always skipping the prefix symbol as a positive sign a good idea in all locales? */ +static int32_t +u_scanf_skip_leading_positive_sign(UFILE *input, + UNumberFormat *format, + UErrorCode *status) +{ + UChar c; + int32_t count = 0; + UBool isNotEOF; + UChar plusSymbol[USCANF_SYMBOL_BUFFER_SIZE]; + int32_t symbolLen; + UErrorCode localStatus = U_ZERO_ERROR; + + if (U_SUCCESS(*status)) { + symbolLen = unum_getSymbol(format, + UNUM_PLUS_SIGN_SYMBOL, + plusSymbol, + UPRV_LENGTHOF(plusSymbol), + &localStatus); + + if (U_SUCCESS(localStatus)) { + /* skip all leading ws in the input */ + while( (isNotEOF = ufile_getch(input, &c)) && (count < symbolLen && c == plusSymbol[count]) ) + { + count++; + } + + /* put the final character back on the input */ + if(isNotEOF) { + u_fungetc(c, input); + } + } + } + + return count; +} + +static int32_t +u_scanf_simple_percent_handler(UFILE *input, + u_scanf_spec_info *info, + ufmt_args *args, + const UChar *fmt, + int32_t *fmtConsumed, + int32_t *argConverted) +{ + /* make sure the next character in the input is a percent */ + *argConverted = 0; + if(u_fgetc(input) != 0x0025) { + *argConverted = -1; + } + return 1; +} + +static int32_t +u_scanf_count_handler(UFILE *input, + u_scanf_spec_info *info, + ufmt_args *args, + const UChar *fmt, + int32_t *fmtConsumed, + int32_t *argConverted) +{ + /* in the special case of count, the u_scanf_spec_info's width */ + /* will contain the # of items converted thus far */ + if (!info->fSkipArg) { + if (info->fIsShort) + *(int16_t*)(args[0].ptrValue) = (int16_t)(UINT16_MAX & info->fWidth); + else if (info->fIsLongLong) + *(int64_t*)(args[0].ptrValue) = info->fWidth; + else + *(int32_t*)(args[0].ptrValue) = (int32_t)(UINT32_MAX & info->fWidth); + } + *argConverted = 0; + + /* we converted 0 args */ + return 0; +} + +static int32_t +u_scanf_double_handler(UFILE *input, + u_scanf_spec_info *info, + ufmt_args *args, + const UChar *fmt, + int32_t *fmtConsumed, + int32_t *argConverted) +{ + int32_t len; + double num; + UNumberFormat *format; + int32_t parsePos = 0; + int32_t skipped; + UErrorCode status = U_ZERO_ERROR; + + + /* skip all ws in the input */ + skipped = u_scanf_skip_leading_ws(input, info->fPadChar); + + /* fill the input's internal buffer */ + ufile_fill_uchar_buffer(input); + + /* determine the size of the input's buffer */ + len = (int32_t)(input->str.fLimit - input->str.fPos); + + /* truncate to the width, if specified */ + if(info->fWidth != -1) + len = ufmt_min(len, info->fWidth); + + /* get the formatter */ + format = u_locbund_getNumberFormat(&input->str.fBundle, UNUM_DECIMAL); + + /* handle error */ + if(format == 0) + return 0; + + /* Skip the positive prefix. ICU normally can't handle this due to strict parsing. */ + skipped += u_scanf_skip_leading_positive_sign(input, format, &status); + + /* parse the number */ + num = unum_parseDouble(format, input->str.fPos, len, &parsePos, &status); + + if (!info->fSkipArg) { + if (info->fIsLong) + *(double*)(args[0].ptrValue) = num; + else if (info->fIsLongDouble) + *(long double*)(args[0].ptrValue) = num; + else + *(float*)(args[0].ptrValue) = (float)num; + } + + /* mask off any necessary bits */ + /* if(! info->fIsLong_double) + num &= DBL_MAX;*/ + + /* update the input's position to reflect consumed data */ + input->str.fPos += parsePos; + + /* we converted 1 arg */ + *argConverted = !info->fSkipArg; + return parsePos + skipped; +} + +#define UPRINTF_SYMBOL_BUFFER_SIZE 8 + +static int32_t +u_scanf_scientific_handler(UFILE *input, + u_scanf_spec_info *info, + ufmt_args *args, + const UChar *fmt, + int32_t *fmtConsumed, + int32_t *argConverted) +{ + int32_t len; + double num; + UNumberFormat *format; + int32_t parsePos = 0; + int32_t skipped; + UErrorCode status = U_ZERO_ERROR; + UChar srcExpBuf[UPRINTF_SYMBOL_BUFFER_SIZE]; + int32_t srcLen, expLen; + UChar expBuf[UPRINTF_SYMBOL_BUFFER_SIZE]; + + + /* skip all ws in the input */ + skipped = u_scanf_skip_leading_ws(input, info->fPadChar); + + /* fill the input's internal buffer */ + ufile_fill_uchar_buffer(input); + + /* determine the size of the input's buffer */ + len = (int32_t)(input->str.fLimit - input->str.fPos); + + /* truncate to the width, if specified */ + if(info->fWidth != -1) + len = ufmt_min(len, info->fWidth); + + /* get the formatter */ + format = u_locbund_getNumberFormat(&input->str.fBundle, UNUM_SCIENTIFIC); + + /* handle error */ + if(format == 0) + return 0; + + /* set the appropriate flags on the formatter */ + + srcLen = unum_getSymbol(format, + UNUM_EXPONENTIAL_SYMBOL, + srcExpBuf, + sizeof(srcExpBuf), + &status); + + /* Upper/lower case the e */ + if (info->fSpec == (UChar)0x65 /* e */) { + expLen = u_strToLower(expBuf, (int32_t)sizeof(expBuf), + srcExpBuf, srcLen, + input->str.fBundle.fLocale, + &status); + } + else { + expLen = u_strToUpper(expBuf, (int32_t)sizeof(expBuf), + srcExpBuf, srcLen, + input->str.fBundle.fLocale, + &status); + } + + unum_setSymbol(format, + UNUM_EXPONENTIAL_SYMBOL, + expBuf, + expLen, + &status); + + + + + /* Skip the positive prefix. ICU normally can't handle this due to strict parsing. */ + skipped += u_scanf_skip_leading_positive_sign(input, format, &status); + + /* parse the number */ + num = unum_parseDouble(format, input->str.fPos, len, &parsePos, &status); + + if (!info->fSkipArg) { + if (info->fIsLong) + *(double*)(args[0].ptrValue) = num; + else if (info->fIsLongDouble) + *(long double*)(args[0].ptrValue) = num; + else + *(float*)(args[0].ptrValue) = (float)num; + } + + /* mask off any necessary bits */ + /* if(! info->fIsLong_double) + num &= DBL_MAX;*/ + + /* update the input's position to reflect consumed data */ + input->str.fPos += parsePos; + + /* we converted 1 arg */ + *argConverted = !info->fSkipArg; + return parsePos + skipped; +} + +static int32_t +u_scanf_scidbl_handler(UFILE *input, + u_scanf_spec_info *info, + ufmt_args *args, + const UChar *fmt, + int32_t *fmtConsumed, + int32_t *argConverted) +{ + int32_t len; + double num; + UNumberFormat *scientificFormat, *genericFormat; + /*int32_t scientificResult, genericResult;*/ + double scientificResult, genericResult; + int32_t scientificParsePos = 0, genericParsePos = 0, parsePos = 0; + int32_t skipped; + UErrorCode scientificStatus = U_ZERO_ERROR; + UErrorCode genericStatus = U_ZERO_ERROR; + + + /* since we can't determine by scanning the characters whether */ + /* a number was formatted in the 'f' or 'g' styles, parse the */ + /* string with both formatters, and assume whichever one */ + /* parsed the most is the correct formatter to use */ + + + /* skip all ws in the input */ + skipped = u_scanf_skip_leading_ws(input, info->fPadChar); + + /* fill the input's internal buffer */ + ufile_fill_uchar_buffer(input); + + /* determine the size of the input's buffer */ + len = (int32_t)(input->str.fLimit - input->str.fPos); + + /* truncate to the width, if specified */ + if(info->fWidth != -1) + len = ufmt_min(len, info->fWidth); + + /* get the formatters */ + scientificFormat = u_locbund_getNumberFormat(&input->str.fBundle, UNUM_SCIENTIFIC); + genericFormat = u_locbund_getNumberFormat(&input->str.fBundle, UNUM_DECIMAL); + + /* handle error */ + if(scientificFormat == 0 || genericFormat == 0) + return 0; + + /* Skip the positive prefix. ICU normally can't handle this due to strict parsing. */ + skipped += u_scanf_skip_leading_positive_sign(input, genericFormat, &genericStatus); + + /* parse the number using each format*/ + + scientificResult = unum_parseDouble(scientificFormat, input->str.fPos, len, + &scientificParsePos, &scientificStatus); + + genericResult = unum_parseDouble(genericFormat, input->str.fPos, len, + &genericParsePos, &genericStatus); + + /* determine which parse made it farther */ + if(scientificParsePos > genericParsePos) { + /* stash the result in num */ + num = scientificResult; + /* update the input's position to reflect consumed data */ + parsePos += scientificParsePos; + } + else { + /* stash the result in num */ + num = genericResult; + /* update the input's position to reflect consumed data */ + parsePos += genericParsePos; + } + input->str.fPos += parsePos; + + if (!info->fSkipArg) { + if (info->fIsLong) + *(double*)(args[0].ptrValue) = num; + else if (info->fIsLongDouble) + *(long double*)(args[0].ptrValue) = num; + else + *(float*)(args[0].ptrValue) = (float)num; + } + + /* mask off any necessary bits */ + /* if(! info->fIsLong_double) + num &= DBL_MAX;*/ + + /* we converted 1 arg */ + *argConverted = !info->fSkipArg; + return parsePos + skipped; +} + +static int32_t +u_scanf_integer_handler(UFILE *input, + u_scanf_spec_info *info, + ufmt_args *args, + const UChar *fmt, + int32_t *fmtConsumed, + int32_t *argConverted) +{ + int32_t len; + void *num = (void*) (args[0].ptrValue); + UNumberFormat *format; + int32_t parsePos = 0; + int32_t skipped; + UErrorCode status = U_ZERO_ERROR; + int64_t result; + + + /* skip all ws in the input */ + skipped = u_scanf_skip_leading_ws(input, info->fPadChar); + + /* fill the input's internal buffer */ + ufile_fill_uchar_buffer(input); + + /* determine the size of the input's buffer */ + len = (int32_t)(input->str.fLimit - input->str.fPos); + + /* truncate to the width, if specified */ + if(info->fWidth != -1) + len = ufmt_min(len, info->fWidth); + + /* get the formatter */ + format = u_locbund_getNumberFormat(&input->str.fBundle, UNUM_DECIMAL); + + /* handle error */ + if(format == 0) + return 0; + + /* Skip the positive prefix. ICU normally can't handle this due to strict parsing. */ + skipped += u_scanf_skip_leading_positive_sign(input, format, &status); + + /* parse the number */ + result = unum_parseInt64(format, input->str.fPos, len, &parsePos, &status); + + /* mask off any necessary bits */ + if (!info->fSkipArg) { + if (info->fIsShort) + *(int16_t*)num = (int16_t)(UINT16_MAX & result); + else if (info->fIsLongLong) + *(int64_t*)num = result; + else + *(int32_t*)num = (int32_t)(UINT32_MAX & result); + } + + /* update the input's position to reflect consumed data */ + input->str.fPos += parsePos; + + /* we converted 1 arg */ + *argConverted = !info->fSkipArg; + return parsePos + skipped; +} + +static int32_t +u_scanf_uinteger_handler(UFILE *input, + u_scanf_spec_info *info, + ufmt_args *args, + const UChar *fmt, + int32_t *fmtConsumed, + int32_t *argConverted) +{ + /* TODO Fix this when Numberformat handles uint64_t */ + return u_scanf_integer_handler(input, info, args, fmt, fmtConsumed, argConverted); +} + +static int32_t +u_scanf_percent_handler(UFILE *input, + u_scanf_spec_info *info, + ufmt_args *args, + const UChar *fmt, + int32_t *fmtConsumed, + int32_t *argConverted) +{ + int32_t len; + double num; + UNumberFormat *format; + int32_t parsePos = 0; + UErrorCode status = U_ZERO_ERROR; + + + /* skip all ws in the input */ + u_scanf_skip_leading_ws(input, info->fPadChar); + + /* fill the input's internal buffer */ + ufile_fill_uchar_buffer(input); + + /* determine the size of the input's buffer */ + len = (int32_t)(input->str.fLimit - input->str.fPos); + + /* truncate to the width, if specified */ + if(info->fWidth != -1) + len = ufmt_min(len, info->fWidth); + + /* get the formatter */ + format = u_locbund_getNumberFormat(&input->str.fBundle, UNUM_PERCENT); + + /* handle error */ + if(format == 0) + return 0; + + /* Skip the positive prefix. ICU normally can't handle this due to strict parsing. */ + u_scanf_skip_leading_positive_sign(input, format, &status); + + /* parse the number */ + num = unum_parseDouble(format, input->str.fPos, len, &parsePos, &status); + + if (!info->fSkipArg) { + *(double*)(args[0].ptrValue) = num; + } + + /* mask off any necessary bits */ + /* if(! info->fIsLong_double) + num &= DBL_MAX;*/ + + /* update the input's position to reflect consumed data */ + input->str.fPos += parsePos; + + /* we converted 1 arg */ + *argConverted = !info->fSkipArg; + return parsePos; +} + +static int32_t +u_scanf_string_handler(UFILE *input, + u_scanf_spec_info *info, + ufmt_args *args, + const UChar *fmt, + int32_t *fmtConsumed, + int32_t *argConverted) +{ + const UChar *source; + UConverter *conv; + char *arg = (char*)(args[0].ptrValue); + char *alias = arg; + char *limit; + UErrorCode status = U_ZERO_ERROR; + int32_t count; + int32_t skipped = 0; + UChar c; + UBool isNotEOF = FALSE; + + /* skip all ws in the input */ + if (info->fIsString) { + skipped = u_scanf_skip_leading_ws(input, info->fPadChar); + } + + /* get the string one character at a time, truncating to the width */ + count = 0; + + /* open the default converter */ + conv = u_getDefaultConverter(&status); + + if(U_FAILURE(status)) + return -1; + + while( (info->fWidth == -1 || count < info->fWidth) + && (isNotEOF = ufile_getch(input, &c)) + && (!info->fIsString || (c != info->fPadChar && !u_isWhitespace(c)))) + { + + if (!info->fSkipArg) { + /* put the character from the input onto the target */ + source = &c; + /* Since we do this one character at a time, do it this way. */ + if (info->fWidth > 0) { + limit = alias + info->fWidth - count; + } + else { + limit = alias + ucnv_getMaxCharSize(conv); + } + + /* convert the character to the default codepage */ + ucnv_fromUnicode(conv, &alias, limit, &source, source + 1, + NULL, TRUE, &status); + + if(U_FAILURE(status)) { + /* clean up */ + u_releaseDefaultConverter(conv); + return -1; + } + } + + /* increment the count */ + ++count; + } + + /* put the final character we read back on the input */ + if (!info->fSkipArg) { + if ((info->fWidth == -1 || count < info->fWidth) && isNotEOF) + u_fungetc(c, input); + + /* add the terminator */ + if (info->fIsString) { + *alias = 0x00; + } + } + + /* clean up */ + u_releaseDefaultConverter(conv); + + /* we converted 1 arg */ + *argConverted = !info->fSkipArg; + return count + skipped; +} + +static int32_t +u_scanf_char_handler(UFILE *input, + u_scanf_spec_info *info, + ufmt_args *args, + const UChar *fmt, + int32_t *fmtConsumed, + int32_t *argConverted) +{ + if (info->fWidth < 0) { + info->fWidth = 1; + } + info->fIsString = FALSE; + return u_scanf_string_handler(input, info, args, fmt, fmtConsumed, argConverted); +} + +static int32_t +u_scanf_ustring_handler(UFILE *input, + u_scanf_spec_info *info, + ufmt_args *args, + const UChar *fmt, + int32_t *fmtConsumed, + int32_t *argConverted) +{ + UChar *arg = (UChar*)(args[0].ptrValue); + UChar *alias = arg; + int32_t count; + int32_t skipped = 0; + UChar c; + UBool isNotEOF = FALSE; + + /* skip all ws in the input */ + if (info->fIsString) { + skipped = u_scanf_skip_leading_ws(input, info->fPadChar); + } + + /* get the string one character at a time, truncating to the width */ + count = 0; + + while( (info->fWidth == -1 || count < info->fWidth) + && (isNotEOF = ufile_getch(input, &c)) + && (!info->fIsString || (c != info->fPadChar && !u_isWhitespace(c)))) + { + + /* put the character from the input onto the target */ + if (!info->fSkipArg) { + *alias++ = c; + } + + /* increment the count */ + ++count; + } + + /* put the final character we read back on the input */ + if (!info->fSkipArg) { + if((info->fWidth == -1 || count < info->fWidth) && isNotEOF) { + u_fungetc(c, input); + } + + /* add the terminator */ + if (info->fIsString) { + *alias = 0x0000; + } + } + + /* we converted 1 arg */ + *argConverted = !info->fSkipArg; + return count + skipped; +} + +static int32_t +u_scanf_uchar_handler(UFILE *input, + u_scanf_spec_info *info, + ufmt_args *args, + const UChar *fmt, + int32_t *fmtConsumed, + int32_t *argConverted) +{ + if (info->fWidth < 0) { + info->fWidth = 1; + } + info->fIsString = FALSE; + return u_scanf_ustring_handler(input, info, args, fmt, fmtConsumed, argConverted); +} + +static int32_t +u_scanf_spellout_handler(UFILE *input, + u_scanf_spec_info *info, + ufmt_args *args, + const UChar *fmt, + int32_t *fmtConsumed, + int32_t *argConverted) +{ + int32_t len; + double num; + UNumberFormat *format; + int32_t parsePos = 0; + int32_t skipped; + UErrorCode status = U_ZERO_ERROR; + + + /* skip all ws in the input */ + skipped = u_scanf_skip_leading_ws(input, info->fPadChar); + + /* fill the input's internal buffer */ + ufile_fill_uchar_buffer(input); + + /* determine the size of the input's buffer */ + len = (int32_t)(input->str.fLimit - input->str.fPos); + + /* truncate to the width, if specified */ + if(info->fWidth != -1) + len = ufmt_min(len, info->fWidth); + + /* get the formatter */ + format = u_locbund_getNumberFormat(&input->str.fBundle, UNUM_SPELLOUT); + + /* handle error */ + if(format == 0) + return 0; + + /* Skip the positive prefix. ICU normally can't handle this due to strict parsing. */ + /* This is not applicable to RBNF. */ + /*skipped += u_scanf_skip_leading_positive_sign(input, format, &status);*/ + + /* parse the number */ + num = unum_parseDouble(format, input->str.fPos, len, &parsePos, &status); + + if (!info->fSkipArg) { + *(double*)(args[0].ptrValue) = num; + } + + /* mask off any necessary bits */ + /* if(! info->fIsLong_double) + num &= DBL_MAX;*/ + + /* update the input's position to reflect consumed data */ + input->str.fPos += parsePos; + + /* we converted 1 arg */ + *argConverted = !info->fSkipArg; + return parsePos + skipped; +} + +static int32_t +u_scanf_hex_handler(UFILE *input, + u_scanf_spec_info *info, + ufmt_args *args, + const UChar *fmt, + int32_t *fmtConsumed, + int32_t *argConverted) +{ + int32_t len; + int32_t skipped; + void *num = (void*) (args[0].ptrValue); + int64_t result; + + /* skip all ws in the input */ + skipped = u_scanf_skip_leading_ws(input, info->fPadChar); + + /* fill the input's internal buffer */ + ufile_fill_uchar_buffer(input); + + /* determine the size of the input's buffer */ + len = (int32_t)(input->str.fLimit - input->str.fPos); + + /* truncate to the width, if specified */ + if(info->fWidth != -1) + len = ufmt_min(len, info->fWidth); + + /* check for alternate form */ + if( *(input->str.fPos) == 0x0030 && + (*(input->str.fPos + 1) == 0x0078 || *(input->str.fPos + 1) == 0x0058) ) { + + /* skip the '0' and 'x' or 'X' if present */ + input->str.fPos += 2; + len -= 2; + } + + /* parse the number */ + result = ufmt_uto64(input->str.fPos, &len, 16); + + /* update the input's position to reflect consumed data */ + input->str.fPos += len; + + /* mask off any necessary bits */ + if (!info->fSkipArg) { + if (info->fIsShort) + *(int16_t*)num = (int16_t)(UINT16_MAX & result); + else if (info->fIsLongLong) + *(int64_t*)num = result; + else + *(int32_t*)num = (int32_t)(UINT32_MAX & result); + } + + /* we converted 1 arg */ + *argConverted = !info->fSkipArg; + return len + skipped; +} + +static int32_t +u_scanf_octal_handler(UFILE *input, + u_scanf_spec_info *info, + ufmt_args *args, + const UChar *fmt, + int32_t *fmtConsumed, + int32_t *argConverted) +{ + int32_t len; + int32_t skipped; + void *num = (void*) (args[0].ptrValue); + int64_t result; + + /* skip all ws in the input */ + skipped = u_scanf_skip_leading_ws(input, info->fPadChar); + + /* fill the input's internal buffer */ + ufile_fill_uchar_buffer(input); + + /* determine the size of the input's buffer */ + len = (int32_t)(input->str.fLimit - input->str.fPos); + + /* truncate to the width, if specified */ + if(info->fWidth != -1) + len = ufmt_min(len, info->fWidth); + + /* parse the number */ + result = ufmt_uto64(input->str.fPos, &len, 8); + + /* update the input's position to reflect consumed data */ + input->str.fPos += len; + + /* mask off any necessary bits */ + if (!info->fSkipArg) { + if (info->fIsShort) + *(int16_t*)num = (int16_t)(UINT16_MAX & result); + else if (info->fIsLongLong) + *(int64_t*)num = result; + else + *(int32_t*)num = (int32_t)(UINT32_MAX & result); + } + + /* we converted 1 arg */ + *argConverted = !info->fSkipArg; + return len + skipped; +} + +static int32_t +u_scanf_pointer_handler(UFILE *input, + u_scanf_spec_info *info, + ufmt_args *args, + const UChar *fmt, + int32_t *fmtConsumed, + int32_t *argConverted) +{ + int32_t len; + int32_t skipped; + void *result; + void **p = (void**)(args[0].ptrValue); + + + /* skip all ws in the input */ + skipped = u_scanf_skip_leading_ws(input, info->fPadChar); + + /* fill the input's internal buffer */ + ufile_fill_uchar_buffer(input); + + /* determine the size of the input's buffer */ + len = (int32_t)(input->str.fLimit - input->str.fPos); + + /* truncate to the width, if specified */ + if(info->fWidth != -1) { + len = ufmt_min(len, info->fWidth); + } + + /* Make sure that we don't consume too much */ + if (len > (int32_t)(sizeof(void*)*2)) { + len = (int32_t)(sizeof(void*)*2); + } + + /* parse the pointer - assign to temporary value */ + result = ufmt_utop(input->str.fPos, &len); + + if (!info->fSkipArg) { + *p = result; + } + + /* update the input's position to reflect consumed data */ + input->str.fPos += len; + + /* we converted 1 arg */ + *argConverted = !info->fSkipArg; + return len + skipped; +} + +static int32_t +u_scanf_scanset_handler(UFILE *input, + u_scanf_spec_info *info, + ufmt_args *args, + const UChar *fmt, + int32_t *fmtConsumed, + int32_t *argConverted) +{ + USet *scanset; + UErrorCode status = U_ZERO_ERROR; + int32_t chLeft = INT32_MAX; + UChar32 c; + UChar *alias = (UChar*) (args[0].ptrValue); + UBool isNotEOF = FALSE; + UBool readCharacter = FALSE; + + /* Create an empty set */ + scanset = uset_open(0, -1); + + /* Back up one to get the [ */ + fmt--; + + /* truncate to the width, if specified and alias the target */ + if(info->fWidth >= 0) { + chLeft = info->fWidth; + } + + /* parse the scanset from the fmt string */ + *fmtConsumed = uset_applyPattern(scanset, fmt, -1, 0, &status); + + /* verify that the parse was successful */ + if (U_SUCCESS(status)) { + c=0; + + /* grab characters one at a time and make sure they are in the scanset */ + while(chLeft > 0) { + if ((isNotEOF = ufile_getch32(input, &c)) && uset_contains(scanset, c)) { + readCharacter = TRUE; + if (!info->fSkipArg) { + int32_t idx = 0; + UBool isError = FALSE; + + U16_APPEND(alias, idx, chLeft, c, isError); + if (isError) { + break; + } + alias += idx; + } + chLeft -= (1 + U_IS_SUPPLEMENTARY(c)); + } + else { + /* if the character's not in the scanset, break out */ + break; + } + } + + /* put the final character we read back on the input */ + if(isNotEOF && chLeft > 0) { + u_fungetc(c, input); + } + } + + uset_close(scanset); + + /* if we didn't match at least 1 character, fail */ + if(!readCharacter) + return -1; + /* otherwise, add the terminator */ + else if (!info->fSkipArg) { + *alias = 0x00; + } + + /* we converted 1 arg */ + *argConverted = !info->fSkipArg; + return (info->fWidth >= 0 ? info->fWidth : INT32_MAX) - chLeft; +} + +/* Use US-ASCII characters only for formatting. Most codepages have + characters 20-7F from Unicode. Using any other codepage specific + characters will make it very difficult to format the string on + non-Unicode machines */ +static const u_scanf_info g_u_scanf_infos[USCANF_NUM_FMT_HANDLERS] = { +/* 0x20 */ + UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, + UFMT_EMPTY, UFMT_SIMPLE_PERCENT,UFMT_EMPTY, UFMT_EMPTY, + UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, + UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, + +/* 0x30 */ + UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, + UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, + UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, + UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, + +/* 0x40 */ + UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_UCHAR, + UFMT_EMPTY, UFMT_SCIENTIFIC, UFMT_EMPTY, UFMT_SCIDBL, +#ifdef U_USE_OBSOLETE_IO_FORMATTING + UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_UCHAR/*deprecated*/, +#else + UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, +#endif + UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, + +/* 0x50 */ + UFMT_PERCENT, UFMT_EMPTY, UFMT_EMPTY, UFMT_USTRING, +#ifdef U_USE_OBSOLETE_IO_FORMATTING + UFMT_EMPTY, UFMT_USTRING/*deprecated*/,UFMT_SPELLOUT, UFMT_EMPTY, +#else + UFMT_EMPTY, UFMT_EMPTY, UFMT_SPELLOUT, UFMT_EMPTY, +#endif + UFMT_HEX, UFMT_EMPTY, UFMT_EMPTY, UFMT_SCANSET, + UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, + +/* 0x60 */ + UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_CHAR, + UFMT_INT, UFMT_SCIENTIFIC, UFMT_DOUBLE, UFMT_SCIDBL, + UFMT_EMPTY, UFMT_INT, UFMT_EMPTY, UFMT_EMPTY, + UFMT_EMPTY, UFMT_EMPTY, UFMT_COUNT, UFMT_OCTAL, + +/* 0x70 */ + UFMT_POINTER, UFMT_EMPTY, UFMT_EMPTY, UFMT_STRING, + UFMT_EMPTY, UFMT_UINT, UFMT_EMPTY, UFMT_EMPTY, + UFMT_HEX, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, + UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, +}; + +U_CFUNC int32_t +u_scanf_parse(UFILE *f, + const UChar *patternSpecification, + va_list ap) +{ + const UChar *alias; + int32_t count, converted, argConsumed, cpConsumed; + uint16_t handlerNum; + + ufmt_args args; + u_scanf_spec spec; + ufmt_type_info info; + u_scanf_handler handler; + + /* alias the pattern */ + alias = patternSpecification; + + /* haven't converted anything yet */ + argConsumed = 0; + converted = 0; + cpConsumed = 0; + + /* iterate through the pattern */ + for(;;) { + + /* match any characters up to the next '%' */ + while(*alias != UP_PERCENT && *alias != 0x0000 && u_fgetc(f) == *alias) { + alias++; + } + + /* if we aren't at a '%', or if we're at end of string, break*/ + if(*alias != UP_PERCENT || *alias == 0x0000) + break; + + /* parse the specifier */ + count = u_scanf_parse_spec(alias, &spec); + + /* update the pointer in pattern */ + alias += count; + + handlerNum = (uint16_t)(spec.fInfo.fSpec - USCANF_BASE_FMT_HANDLERS); + if (handlerNum < USCANF_NUM_FMT_HANDLERS) { + /* skip the argument, if necessary */ + /* query the info function for argument information */ + info = g_u_scanf_infos[ handlerNum ].info; + if (info != ufmt_count && u_feof(f)) { + break; + } + else if(spec.fInfo.fSkipArg) { + args.ptrValue = NULL; + } + else { + switch(info) { + case ufmt_count: + /* set the spec's width to the # of items converted */ + spec.fInfo.fWidth = cpConsumed; + U_FALLTHROUGH; + case ufmt_char: + case ufmt_uchar: + case ufmt_int: + case ufmt_string: + case ufmt_ustring: + case ufmt_pointer: + case ufmt_float: + case ufmt_double: + args.ptrValue = va_arg(ap, void*); + break; + + default: + /* else args is ignored */ + args.ptrValue = NULL; + break; + } + } + + /* call the handler function */ + handler = g_u_scanf_infos[ handlerNum ].handler; + if(handler != 0) { + + /* reset count to 1 so that += for alias works. */ + count = 1; + + cpConsumed += (*handler)(f, &spec.fInfo, &args, alias, &count, &argConsumed); + + /* if the handler encountered an error condition, break */ + if(argConsumed < 0) { + converted = -1; + break; + } + + /* add to the # of items converted */ + converted += argConsumed; + + /* update the pointer in pattern */ + alias += count-1; + } + /* else do nothing */ + } + /* else do nothing */ + + /* just ignore unknown tags */ + } + + /* return # of items converted */ + return converted; +} + +#endif /* #if !UCONFIG_NO_FORMATTING */ diff --git a/intl/icu/source/io/ustdio.c b/intl/icu/source/io/ustdio.c new file mode 100644 index 000000000..1c2225a94 --- /dev/null +++ b/intl/icu/source/io/ustdio.c @@ -0,0 +1,732 @@ +// Copyright (C) 2016 and later: Unicode, Inc. and others. +// License & terms of use: http://www.unicode.org/copyright.html +/* + ****************************************************************************** + * + * Copyright (C) 1998-2016, International Business Machines + * Corporation and others. All Rights Reserved. + * + ****************************************************************************** + * + * File ustdio.c + * + * Modification History: + * + * Date Name Description + * 11/18/98 stephen Creation. + * 03/12/99 stephen Modified for new C API. + * 07/19/99 stephen Fixed read() and gets() + ****************************************************************************** + */ + +#include "unicode/ustdio.h" + +#if !UCONFIG_NO_CONVERSION + +#include "unicode/putil.h" +#include "cmemory.h" +#include "cstring.h" +#include "ufile.h" +#include "ufmt_cmn.h" +#include "unicode/ucnv.h" +#include "unicode/ustring.h" + +#include <string.h> + +#define DELIM_LF 0x000A +#define DELIM_VT 0x000B +#define DELIM_FF 0x000C +#define DELIM_CR 0x000D +#define DELIM_NEL 0x0085 +#define DELIM_LS 0x2028 +#define DELIM_PS 0x2029 + +/* TODO: is this correct for all codepages? Should we just use \n and let the converter handle it? */ +#if U_PLATFORM_USES_ONLY_WIN32_API +static const UChar DELIMITERS [] = { DELIM_CR, DELIM_LF, 0x0000 }; +static const uint32_t DELIMITERS_LEN = 2; +/* TODO: Default newline writing should be detected based upon the converter being used. */ +#else +static const UChar DELIMITERS [] = { DELIM_LF, 0x0000 }; +static const uint32_t DELIMITERS_LEN = 1; +#endif + +#define IS_FIRST_STRING_DELIMITER(c1) \ + (UBool)((DELIM_LF <= (c1) && (c1) <= DELIM_CR) \ + || (c1) == DELIM_NEL \ + || (c1) == DELIM_LS \ + || (c1) == DELIM_PS) +#define CAN_HAVE_COMBINED_STRING_DELIMITER(c1) (UBool)((c1) == DELIM_CR) +#define IS_COMBINED_STRING_DELIMITER(c1, c2) \ + (UBool)((c1) == DELIM_CR && (c2) == DELIM_LF) + + +#if !UCONFIG_NO_TRANSLITERATION + +U_CAPI UTransliterator* U_EXPORT2 +u_fsettransliterator(UFILE *file, UFileDirection direction, + UTransliterator *adopt, UErrorCode *status) +{ + UTransliterator *old = NULL; + + if(U_FAILURE(*status)) + { + return adopt; + } + + if(!file) + { + *status = U_ILLEGAL_ARGUMENT_ERROR; + return adopt; + } + + if(direction & U_READ) + { + /** TODO: implement */ + *status = U_UNSUPPORTED_ERROR; + return adopt; + } + + if(adopt == NULL) /* they are clearing it */ + { + if(file->fTranslit != NULL) + { + /* TODO: Check side */ + old = file->fTranslit->translit; + uprv_free(file->fTranslit->buffer); + file->fTranslit->buffer=NULL; + uprv_free(file->fTranslit); + file->fTranslit=NULL; + } + } + else + { + if(file->fTranslit == NULL) + { + file->fTranslit = (UFILETranslitBuffer*) uprv_malloc(sizeof(UFILETranslitBuffer)); + if(!file->fTranslit) + { + *status = U_MEMORY_ALLOCATION_ERROR; + return adopt; + } + file->fTranslit->capacity = 0; + file->fTranslit->length = 0; + file->fTranslit->pos = 0; + file->fTranslit->buffer = NULL; + } + else + { + old = file->fTranslit->translit; + ufile_flush_translit(file); + } + + file->fTranslit->translit = adopt; + } + + return old; +} + +static const UChar * u_file_translit(UFILE *f, const UChar *src, int32_t *count, UBool flush) +{ + int32_t newlen; + int32_t junkCount = 0; + int32_t textLength; + int32_t textLimit; + UTransPosition pos; + UErrorCode status = U_ZERO_ERROR; + + if(count == NULL) + { + count = &junkCount; + } + + if ((!f)||(!f->fTranslit)||(!f->fTranslit->translit)) + { + /* fast path */ + return src; + } + + /* First: slide over everything */ + if(f->fTranslit->length > f->fTranslit->pos) + { + memmove(f->fTranslit->buffer, f->fTranslit->buffer + f->fTranslit->pos, + (f->fTranslit->length - f->fTranslit->pos)*sizeof(UChar)); + } + f->fTranslit->length -= f->fTranslit->pos; /* always */ + f->fTranslit->pos = 0; + + /* Calculate new buffer size needed */ + newlen = (*count + f->fTranslit->length) * 4; + + if(newlen > f->fTranslit->capacity) + { + if(f->fTranslit->buffer == NULL) + { + f->fTranslit->buffer = (UChar*)uprv_malloc(newlen * sizeof(UChar)); + } + else + { + f->fTranslit->buffer = (UChar*)uprv_realloc(f->fTranslit->buffer, newlen * sizeof(UChar)); + } + /* Check for malloc/realloc failure. */ + if (f->fTranslit->buffer == NULL) { + return NULL; + } + f->fTranslit->capacity = newlen; + } + + /* Now, copy any data over */ + u_strncpy(f->fTranslit->buffer + f->fTranslit->length, + src, + *count); + f->fTranslit->length += *count; + + /* Now, translit in place as much as we can */ + if(flush == FALSE) + { + textLength = f->fTranslit->length; + pos.contextStart = 0; + pos.contextLimit = textLength; + pos.start = 0; + pos.limit = textLength; + + utrans_transIncrementalUChars(f->fTranslit->translit, + f->fTranslit->buffer, /* because we shifted */ + &textLength, + f->fTranslit->capacity, + &pos, + &status); + + /* now: start/limit point to the transliterated text */ + /* Transliterated is [buffer..pos.start) */ + *count = pos.start; + f->fTranslit->pos = pos.start; + f->fTranslit->length = pos.limit; + + return f->fTranslit->buffer; + } + else + { + textLength = f->fTranslit->length; + textLimit = f->fTranslit->length; + + utrans_transUChars(f->fTranslit->translit, + f->fTranslit->buffer, + &textLength, + f->fTranslit->capacity, + 0, + &textLimit, + &status); + + /* out: converted len */ + *count = textLimit; + + /* Set pointers to 0 */ + f->fTranslit->pos = 0; + f->fTranslit->length = 0; + + return f->fTranslit->buffer; + } +} + +#endif + +void +ufile_flush_translit(UFILE *f) +{ +#if !UCONFIG_NO_TRANSLITERATION + if((!f)||(!f->fTranslit)) + return; +#endif + + u_file_write_flush(NULL, 0, f, FALSE, TRUE); +} + + +void +ufile_flush_io(UFILE *f) +{ + if((!f) || (!f->fFile)) { + return; /* skip if no file */ + } + + u_file_write_flush(NULL, 0, f, TRUE, FALSE); +} + + +void +ufile_close_translit(UFILE *f) +{ +#if !UCONFIG_NO_TRANSLITERATION + if((!f)||(!f->fTranslit)) + return; +#endif + + ufile_flush_translit(f); + +#if !UCONFIG_NO_TRANSLITERATION + if(f->fTranslit->translit) + utrans_close(f->fTranslit->translit); + + if(f->fTranslit->buffer) + { + uprv_free(f->fTranslit->buffer); + } + + uprv_free(f->fTranslit); + f->fTranslit = NULL; +#endif +} + + +/* Input/output */ + +U_CAPI int32_t U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */ +u_fputs(const UChar *s, + UFILE *f) +{ + int32_t count = u_file_write(s, u_strlen(s), f); + count += u_file_write(DELIMITERS, DELIMITERS_LEN, f); + return count; +} + +U_CAPI UChar32 U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */ +u_fputc(UChar32 uc, + UFILE *f) +{ + UChar buf[2]; + int32_t idx = 0; + UBool isError = FALSE; + + U16_APPEND(buf, idx, UPRV_LENGTHOF(buf), uc, isError); + if (isError) { + return U_EOF; + } + return u_file_write(buf, idx, f) == idx ? uc : U_EOF; +} + + +U_CFUNC int32_t U_EXPORT2 +u_file_write_flush(const UChar *chars, + int32_t count, + UFILE *f, + UBool flushIO, + UBool flushTranslit) +{ + /* Set up conversion parameters */ + UErrorCode status = U_ZERO_ERROR; + const UChar *mySource = chars; + const UChar *mySourceBegin; + const UChar *mySourceEnd; + char charBuffer[UFILE_CHARBUFFER_SIZE]; + char *myTarget = charBuffer; + int32_t written = 0; + int32_t numConverted = 0; + + if (count < 0) { + count = u_strlen(chars); + } + +#if !UCONFIG_NO_TRANSLITERATION + if((f->fTranslit) && (f->fTranslit->translit)) + { + /* Do the transliteration */ + mySource = u_file_translit(f, chars, &count, flushTranslit); + } +#endif + + /* Write to a string. */ + if (!f->fFile) { + int32_t charsLeft = (int32_t)(f->str.fLimit - f->str.fPos); + if (flushIO && charsLeft > count) { + count++; + } + written = ufmt_min(count, charsLeft); + u_strncpy(f->str.fPos, mySource, written); + f->str.fPos += written; + return written; + } + + mySourceEnd = mySource + count; + + /* Perform the conversion in a loop */ + do { + mySourceBegin = mySource; /* beginning location for this loop */ + status = U_ZERO_ERROR; + if(f->fConverter != NULL) { /* We have a valid converter */ + ucnv_fromUnicode(f->fConverter, + &myTarget, + charBuffer + UFILE_CHARBUFFER_SIZE, + &mySource, + mySourceEnd, + NULL, + flushIO, + &status); + } else { /*weiv: do the invariant conversion */ + int32_t convertChars = (int32_t) (mySourceEnd - mySource); + if (convertChars > UFILE_CHARBUFFER_SIZE) { + convertChars = UFILE_CHARBUFFER_SIZE; + status = U_BUFFER_OVERFLOW_ERROR; + } + u_UCharsToChars(mySource, myTarget, convertChars); + mySource += convertChars; + myTarget += convertChars; + } + numConverted = (int32_t)(myTarget - charBuffer); + + if (numConverted > 0) { + /* write the converted bytes */ + fwrite(charBuffer, + sizeof(char), + numConverted, + f->fFile); + + written += (int32_t) (mySource - mySourceBegin); + } + myTarget = charBuffer; + } + while(status == U_BUFFER_OVERFLOW_ERROR); + + /* return # of chars written */ + return written; +} + +U_CAPI int32_t U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */ +u_file_write( const UChar *chars, + int32_t count, + UFILE *f) +{ + return u_file_write_flush(chars,count,f,FALSE,FALSE); +} + + +/* private function used for buffering input */ +void +ufile_fill_uchar_buffer(UFILE *f) +{ + UErrorCode status; + const char *mySource; + const char *mySourceEnd; + UChar *myTarget; + int32_t bufferSize; + int32_t maxCPBytes; + int32_t bytesRead; + int32_t availLength; + int32_t dataSize; + char charBuffer[UFILE_CHARBUFFER_SIZE]; + u_localized_string *str; + + if (f->fFile == NULL) { + /* There is nothing to do. It's a string. */ + return; + } + + str = &f->str; + dataSize = (int32_t)(str->fLimit - str->fPos); + if (f->fFileno == 0 && dataSize > 0) { + /* Don't read from stdin too many times. There is still some data. */ + return; + } + + /* shift the buffer if it isn't empty */ + if(dataSize != 0) { + u_memmove(f->fUCBuffer, str->fPos, dataSize); /* not accessing beyond memory */ + } + + + /* record how much buffer space is available */ + availLength = UFILE_UCHARBUFFER_SIZE - dataSize; + + /* Determine the # of codepage bytes needed to fill our UChar buffer */ + /* weiv: if converter is NULL, we use invariant converter with charwidth = 1)*/ + maxCPBytes = availLength / (f->fConverter!=NULL?(2*ucnv_getMinCharSize(f->fConverter)):1); + + /* Read in the data to convert */ + if (f->fFileno == 0) { + /* Special case. Read from stdin one line at a time. */ + char *retStr = fgets(charBuffer, ufmt_min(maxCPBytes, UFILE_CHARBUFFER_SIZE), f->fFile); + bytesRead = (int32_t)(retStr ? uprv_strlen(charBuffer) : 0); + } + else { + /* A normal file */ + bytesRead = (int32_t)fread(charBuffer, + sizeof(char), + ufmt_min(maxCPBytes, UFILE_CHARBUFFER_SIZE), + f->fFile); + } + + /* Set up conversion parameters */ + status = U_ZERO_ERROR; + mySource = charBuffer; + mySourceEnd = charBuffer + bytesRead; + myTarget = f->fUCBuffer + dataSize; + bufferSize = UFILE_UCHARBUFFER_SIZE; + + if(f->fConverter != NULL) { /* We have a valid converter */ + /* Perform the conversion */ + ucnv_toUnicode(f->fConverter, + &myTarget, + f->fUCBuffer + bufferSize, + &mySource, + mySourceEnd, + NULL, + (UBool)(feof(f->fFile) != 0), + &status); + + } else { /*weiv: do the invariant conversion */ + u_charsToUChars(mySource, myTarget, bytesRead); + myTarget += bytesRead; + } + + /* update the pointers into our array */ + str->fPos = str->fBuffer; + str->fLimit = myTarget; +} + +U_CAPI UChar* U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */ +u_fgets(UChar *s, + int32_t n, + UFILE *f) +{ + int32_t dataSize; + int32_t count; + UChar *alias; + const UChar *limit; + UChar *sItr; + UChar currDelim = 0; + u_localized_string *str; + + if (n <= 0) { + /* Caller screwed up. We need to write the null terminatior. */ + return NULL; + } + + /* fill the buffer if needed */ + str = &f->str; + if (str->fPos >= str->fLimit) { + ufile_fill_uchar_buffer(f); + } + + /* subtract 1 from n to compensate for the terminator */ + --n; + + /* determine the amount of data in the buffer */ + dataSize = (int32_t)(str->fLimit - str->fPos); + + /* if 0 characters were left, return 0 */ + if (dataSize == 0) + return NULL; + + /* otherwise, iteratively fill the buffer and copy */ + count = 0; + sItr = s; + currDelim = 0; + while (dataSize > 0 && count < n) { + alias = str->fPos; + + /* Find how much to copy */ + if (dataSize < (n - count)) { + limit = str->fLimit; + } + else { + limit = alias + (n - count); + } + + if (!currDelim) { + /* Copy UChars until we find the first occurrence of a delimiter character */ + while (alias < limit && !IS_FIRST_STRING_DELIMITER(*alias)) { + count++; + *(sItr++) = *(alias++); + } + /* Preserve the newline */ + if (alias < limit && IS_FIRST_STRING_DELIMITER(*alias)) { + if (CAN_HAVE_COMBINED_STRING_DELIMITER(*alias)) { + currDelim = *alias; + } + else { + currDelim = 1; /* This isn't a newline, but it's used to say + that we should break later. We've checked all + possible newline combinations even across buffer + boundaries. */ + } + count++; + *(sItr++) = *(alias++); + } + } + /* If we have a CRLF combination, preserve that too. */ + if (alias < limit) { + if (currDelim && IS_COMBINED_STRING_DELIMITER(currDelim, *alias)) { + count++; + *(sItr++) = *(alias++); + } + currDelim = 1; /* This isn't a newline, but it's used to say + that we should break later. We've checked all + possible newline combinations even across buffer + boundaries. */ + } + + /* update the current buffer position */ + str->fPos = alias; + + /* if we found a delimiter */ + if (currDelim == 1) { + /* break out */ + break; + } + + /* refill the buffer */ + ufile_fill_uchar_buffer(f); + + /* determine the amount of data in the buffer */ + dataSize = (int32_t)(str->fLimit - str->fPos); + } + + /* add the terminator and return s */ + *sItr = 0x0000; + return s; +} + +U_CFUNC UBool U_EXPORT2 +ufile_getch(UFILE *f, UChar *ch) +{ + UBool isValidChar = FALSE; + + *ch = U_EOF; + /* if we have an available character in the buffer, return it */ + if(f->str.fPos < f->str.fLimit){ + *ch = *(f->str.fPos)++; + isValidChar = TRUE; + } + else { + /* otherwise, fill the buffer and return the next character */ + if(f->str.fPos >= f->str.fLimit) { + ufile_fill_uchar_buffer(f); + } + if(f->str.fPos < f->str.fLimit) { + *ch = *(f->str.fPos)++; + isValidChar = TRUE; + } + } + return isValidChar; +} + +U_CAPI UChar U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */ +u_fgetc(UFILE *f) +{ + UChar ch; + ufile_getch(f, &ch); + return ch; +} + +U_CFUNC UBool U_EXPORT2 +ufile_getch32(UFILE *f, UChar32 *c32) +{ + UBool isValidChar = FALSE; + u_localized_string *str; + + *c32 = U_EOF; + + /* Fill the buffer if it is empty */ + str = &f->str; + if (f && str->fPos + 1 >= str->fLimit) { + ufile_fill_uchar_buffer(f); + } + + /* Get the next character in the buffer */ + if (str->fPos < str->fLimit) { + *c32 = *(str->fPos)++; + if (U_IS_LEAD(*c32)) { + if (str->fPos < str->fLimit) { + UChar c16 = *(str->fPos)++; + *c32 = U16_GET_SUPPLEMENTARY(*c32, c16); + isValidChar = TRUE; + } + else { + *c32 = U_EOF; + } + } + else { + isValidChar = TRUE; + } + } + + return isValidChar; +} + +U_CAPI UChar32 U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */ +u_fgetcx(UFILE *f) +{ + UChar32 ch; + ufile_getch32(f, &ch); + return ch; +} + +U_CAPI UChar32 U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */ +u_fungetc(UChar32 ch, + UFILE *f) +{ + u_localized_string *str; + + str = &f->str; + + /* if we're at the beginning of the buffer, sorry! */ + if (str->fPos == str->fBuffer + || (U_IS_LEAD(ch) && (str->fPos - 1) == str->fBuffer)) + { + ch = U_EOF; + } + else { + /* otherwise, put the character back */ + /* Remember, read them back on in the reverse order. */ + if (U_IS_LEAD(ch)) { + if (*--(str->fPos) != U16_TRAIL(ch) + || *--(str->fPos) != U16_LEAD(ch)) + { + ch = U_EOF; + } + } + else if (*--(str->fPos) != ch) { + ch = U_EOF; + } + } + return ch; +} + +U_CAPI int32_t U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */ +u_file_read( UChar *chars, + int32_t count, + UFILE *f) +{ + int32_t dataSize; + int32_t read = 0; + u_localized_string *str = &f->str; + + do { + + /* determine the amount of data in the buffer */ + dataSize = (int32_t)(str->fLimit - str->fPos); + if (dataSize <= 0) { + /* fill the buffer */ + ufile_fill_uchar_buffer(f); + dataSize = (int32_t)(str->fLimit - str->fPos); + } + + /* Make sure that we don't read too much */ + if (dataSize > (count - read)) { + dataSize = count - read; + } + + /* copy the current data in the buffer */ + memcpy(chars + read, str->fPos, dataSize * sizeof(UChar)); + + /* update number of items read */ + read += dataSize; + + /* update the current buffer position */ + str->fPos += dataSize; + } + while (dataSize != 0 && read < count); + + return read; +} +#endif diff --git a/intl/icu/source/io/ustream.cpp b/intl/icu/source/io/ustream.cpp new file mode 100644 index 000000000..8e0087edb --- /dev/null +++ b/intl/icu/source/io/ustream.cpp @@ -0,0 +1,173 @@ +// Copyright (C) 2016 and later: Unicode, Inc. and others. +// License & terms of use: http://www.unicode.org/copyright.html +/* +********************************************************************** +* Copyright (C) 2001-2016, International Business Machines +* Corporation and others. All Rights Reserved. +********************************************************************** +* FILE NAME : ustream.cpp +* +* Modification History: +* +* Date Name Description +* 06/25/2001 grhoten Move iostream from unistr.h to here +****************************************************************************** +*/ + +#include "unicode/utypes.h" + +#if !UCONFIG_NO_CONVERSION + +#include "unicode/uobject.h" +#include "unicode/ustream.h" +#include "unicode/ucnv.h" +#include "unicode/uchar.h" +#include "unicode/utf16.h" +#include "ustr_cnv.h" +#include "cmemory.h" +#include <string.h> + +// console IO + +#if U_IOSTREAM_SOURCE >= 199711 + +#define STD_NAMESPACE std:: + +#define STD_OSTREAM STD_NAMESPACE ostream +#define STD_ISTREAM STD_NAMESPACE istream + +U_NAMESPACE_BEGIN + +U_IO_API STD_OSTREAM & U_EXPORT2 +operator<<(STD_OSTREAM& stream, const UnicodeString& str) +{ + if(str.length() > 0) { + char buffer[200]; + UConverter *converter; + UErrorCode errorCode = U_ZERO_ERROR; + + // use the default converter to convert chunks of text + converter = u_getDefaultConverter(&errorCode); + if(U_SUCCESS(errorCode)) { + const UChar *us = str.getBuffer(); + const UChar *uLimit = us + str.length(); + char *s, *sLimit = buffer + (sizeof(buffer) - 1); + do { + errorCode = U_ZERO_ERROR; + s = buffer; + ucnv_fromUnicode(converter, &s, sLimit, &us, uLimit, 0, FALSE, &errorCode); + *s = 0; + + // write this chunk + if(s > buffer) { + stream << buffer; + } + } while(errorCode == U_BUFFER_OVERFLOW_ERROR); + u_releaseDefaultConverter(converter); + } + } + +/* stream.flush();*/ + return stream; +} + +U_IO_API STD_ISTREAM & U_EXPORT2 +operator>>(STD_ISTREAM& stream, UnicodeString& str) +{ + // This is like ICU status checking. + if (stream.fail()) { + return stream; + } + + /* ipfx should eat whitespace when ios::skipws is set */ + UChar uBuffer[16]; + char buffer[16]; + int32_t idx = 0; + UConverter *converter; + UErrorCode errorCode = U_ZERO_ERROR; + + // use the default converter to convert chunks of text + converter = u_getDefaultConverter(&errorCode); + if(U_SUCCESS(errorCode)) { + UChar *us = uBuffer; + const UChar *uLimit = uBuffer + UPRV_LENGTHOF(uBuffer); + const char *s, *sLimit; + char ch; + UChar ch32; + UBool initialWhitespace = TRUE; + UBool continueReading = TRUE; + + /* We need to consume one byte at a time to see what is considered whitespace. */ + while (continueReading) { + ch = stream.get(); + if (stream.eof()) { + // The EOF is only set after the get() of an unavailable byte. + if (!initialWhitespace) { + stream.clear(stream.eofbit); + } + continueReading = FALSE; + } + sLimit = &ch + (int)continueReading; + us = uBuffer; + s = &ch; + errorCode = U_ZERO_ERROR; + /* + Since we aren't guaranteed to see the state before this call, + this code won't work on stateful encodings like ISO-2022 or an EBCDIC stateful encoding. + We flush on the last byte to ensure that we output truncated multibyte characters. + */ + ucnv_toUnicode(converter, &us, uLimit, &s, sLimit, 0, !continueReading, &errorCode); + if(U_FAILURE(errorCode)) { + /* Something really bad happened. setstate() isn't always an available API */ + stream.clear(stream.failbit); + goto STOP_READING; + } + /* Was the character consumed? */ + if (us != uBuffer) { + /* Reminder: ibm-1390 & JISX0213 can output 2 Unicode code points */ + int32_t uBuffSize = us-uBuffer; + int32_t uBuffIdx = 0; + while (uBuffIdx < uBuffSize) { + U16_NEXT(uBuffer, uBuffIdx, uBuffSize, ch32); + if (u_isWhitespace(ch32)) { + if (!initialWhitespace) { + buffer[idx++] = ch; + while (idx > 0) { + stream.putback(buffer[--idx]); + } + goto STOP_READING; + } + /* else skip intialWhitespace */ + } + else { + if (initialWhitespace) { + /* + When initialWhitespace is TRUE, we haven't appended any + character yet. This is where we truncate the string, + to avoid modifying the string before we know if we can + actually read from the stream. + */ + str.truncate(0); + initialWhitespace = FALSE; + } + str.append(ch32); + } + } + idx = 0; + } + else { + buffer[idx++] = ch; + } + } +STOP_READING: + u_releaseDefaultConverter(converter); + } + +/* stream.flush();*/ + return stream; +} + +U_NAMESPACE_END + +#endif +#endif |