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