1ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/* 2ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru ******************************************************************************* 3ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * 464339d36f8bd4db5025fe2988eda22b491a9219cFredrik Roubert * Copyright (C) 2016 and later: Unicode, Inc. and others. 564339d36f8bd4db5025fe2988eda22b491a9219cFredrik Roubert * License & terms of use: http://www.unicode.org/copyright.html#License 664339d36f8bd4db5025fe2988eda22b491a9219cFredrik Roubert * 764339d36f8bd4db5025fe2988eda22b491a9219cFredrik Roubert ******************************************************************************* 864339d36f8bd4db5025fe2988eda22b491a9219cFredrik Roubert ******************************************************************************* 964339d36f8bd4db5025fe2988eda22b491a9219cFredrik Roubert * 1085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * Copyright (C) 1999-2009, International Business Machines 11ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Corporation and others. All Rights Reserved. 12ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * 13ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru ******************************************************************************* 14ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * file name: reader.c 15ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * encoding: US-ASCII 16ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * tab size: 8 (not used) 17ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * indentation:4 18ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * 19ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * created on: 2000sep5 20ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * created by: Vladimir Weinstein 21ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */ 22ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 23ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/******************************************************************************* 24ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Derived from Madhu Katragadda gentest 25ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *******************************************************************************/ 26ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 27ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 28ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include <string.h> 29ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include <stdio.h> 30ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include <stdlib.h> 31ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#ifdef WIN32 32ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include <direct.h> 33ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#else 34ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include <unistd.h> 35ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#endif 36ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "unicode/utypes.h" 37ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "unicode/putil.h" 38ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "unicode/udata.h" 39ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 4085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#define DATA_NAME "mypkg_example" 41ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define DATA_TYPE "dat" 42ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 43ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/* UDataInfo cf. udata.h */ 44ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querustatic const UDataInfo dataInfo={ 45ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru sizeof(UDataInfo), 46ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 0, 47ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 48ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru U_IS_BIG_ENDIAN, 49ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru U_CHARSET_FAMILY, 50ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru sizeof(UChar), 51ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 0, 52ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 53ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 0x4D, 0x79, 0x44, 0x74, /* dataFormat="MyDt" */ 54ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1, 0, 0, 0, /* formatVersion */ 55ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1, 0, 0, 0 /* dataVersion */ 56ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}; 57ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 58ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querustatic UBool 59ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruisAcceptable(void *context, 60ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru const char *type, const char *name, 61ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru const UDataInfo *pInfo){ 62ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 63ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if( pInfo->size>=20 && 64ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru pInfo->isBigEndian==U_IS_BIG_ENDIAN && 65ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru pInfo->charsetFamily==U_CHARSET_FAMILY && 66ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru pInfo->dataFormat[0]==0x4D && /* dataFormat="MyDt" */ 67ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru pInfo->dataFormat[1]==0x79 && 68ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru pInfo->dataFormat[2]==0x44 && 69ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru pInfo->dataFormat[3]==0x74 && 70ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru pInfo->formatVersion[0]==1 && 71ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru pInfo->dataVersion[0]==1 ) { 72ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return TRUE; 73ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } else { 74ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return FALSE; 75ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 76ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 77ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 78ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 79ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 80ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruextern int 81ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querumain(int argc, const char *argv[]) { 82ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UDataMemory *result = NULL; 83ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UErrorCode status=U_ZERO_ERROR; 84ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 85ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru uint16_t intValue = 0; 86ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 87ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru char *string = NULL; 88ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru uint16_t *intPointer = NULL; 89ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 90ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru const void *dataMemory = NULL; 91ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru char curPathBuffer[1024]; 92ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 93ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#ifdef WIN32 94ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru char *currdir = _getcwd(NULL, 0); 95ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#else 96ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru char *currdir = getcwd(NULL, 0); 97ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#endif 98ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 9985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho /* need to put "current/dir" as path */ 100ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru strcpy(curPathBuffer, currdir); 101ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 102ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru result=udata_openChoice(curPathBuffer, DATA_TYPE, DATA_NAME, isAcceptable, NULL, &status); 103ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 104ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(currdir != NULL) { 105ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru free(currdir); 106ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 107ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 108ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(U_FAILURE(status)){ 109ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru printf("Failed to open data file example.dat in %s with error number %d\n", curPathBuffer, status); 110ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return -1; 111ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 112ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 113ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru dataMemory = udata_getMemory(result); 114ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 115ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru intPointer = (uint16_t *)dataMemory; 116ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 117ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru printf("Read value %d from data file\n", *intPointer); 118ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 119ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru string = (char *) (intPointer+1); 120ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 121ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru printf("Read string %s from data file\n", string); 122ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 123ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(U_SUCCESS(status)){ 124ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru udata_close(result); 125ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 126ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 127ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return 0; 128ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 129ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 130ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 131ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 132ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 133ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 134ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 135ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 136