1ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/*
2ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*******************************************************************************
3ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*
4ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*   Copyright (C) 1999-2006, International Business Machines
5ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*   Corporation and others.  All Rights Reserved.
6ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*
7ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*******************************************************************************
8ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*   file name:  writer.c
9ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*   encoding:   US-ASCII
10ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*   tab size:   8 (not used)
11ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*   indentation:4
12ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*
13ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*   created on: 2000sep5
14ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*   created by: Vladimir Weinstein
15ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*/
16ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
17ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/******************************************************************************
18ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * A program to write simple binary data readable by udata - example for
19ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * ICU workshop
20ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru ******************************************************************************/
21ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
22ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
23ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include <stdio.h>
24ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include <stdlib.h>
25ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#ifdef WIN32
26ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include <direct.h>
27ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#else
28ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include <unistd.h>
29ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#endif
30ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "unicode/utypes.h"
31ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "unicode/udata.h"
32ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
33ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/* this is private - available only through toolutil */
34ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "unewdata.h"
35ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
36ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define DATA_NAME "mypkg_example"
37ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define DATA_TYPE "dat"
38ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
39ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/* UDataInfo cf. udata.h */
40ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querustatic const UDataInfo dataInfo={
41ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    sizeof(UDataInfo),
42ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    0,
43ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
44ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    U_IS_BIG_ENDIAN,
45ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    U_CHARSET_FAMILY,
46ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    sizeof(UChar),
47ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    0,
48ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
49ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    0x4D, 0x79, 0x44, 0x74,     /* dataFormat="MyDt" */
50ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    1, 0, 0, 0,                 /* formatVersion */
51ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    1, 0, 0, 0                  /* dataVersion */
52ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru};
53ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
54ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
55ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/* Excersise: add writing out other data types */
56ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/* see icu/source/tools/toolutil/unewdata.h    */
57ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/* for other possibilities                     */
58ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
59ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruextern int
60ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querumain(int argc, const char *argv[]) {
61ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    UNewDataMemory *pData;
62ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    UErrorCode errorCode=U_ZERO_ERROR;
63ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    char stringValue[]={'E', 'X', 'A', 'M', 'P', 'L', 'E', '\0'};
64ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    uint16_t intValue=2000;
65ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
66ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    long dataLength;
67ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    uint32_t size;
68ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#ifdef WIN32
69ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    char *currdir = _getcwd(NULL, 0);
70ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#else
71ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    char *currdir = getcwd(NULL, 0);
72ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#endif
73ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
74ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    pData=udata_create(currdir, DATA_TYPE, DATA_NAME, &dataInfo,
75ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                       U_COPYRIGHT_STRING, &errorCode);
76ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
77ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if(currdir != NULL) {
78ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        free(currdir);
79ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
80ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
81ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
82ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if(U_FAILURE(errorCode)) {
83ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        fprintf(stderr, "Error: unable to create data memory, error %d\n", errorCode);
84ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        exit(errorCode);
85ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
86ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
87ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    /* write the data to the file */
88ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    /* a 16 bit value  and a String*/
89ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    printf("Writing uint16_t value of %d\n", intValue);
90ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    udata_write16(pData, intValue);
91ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    printf("Writing string value of %s\n", stringValue);
92ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    udata_writeString(pData, stringValue, sizeof(stringValue));
93ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
94ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    /* finish up */
95ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    dataLength=udata_finish(pData, &errorCode);
96ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if(U_FAILURE(errorCode)) {
97ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        fprintf(stderr, "Error: error %d writing the output file\n", errorCode);
98ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        exit(errorCode);
99ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
100ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    size=sizeof(stringValue) + sizeof(intValue);
101ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
102ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
103ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if(dataLength!=(long)size) {
104ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        fprintf(stderr, "Error: data length %ld != calculated size %lu\n", dataLength, size);
105ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        exit(U_INTERNAL_PROGRAM_ERROR);
106ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
107ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return 0;
108ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
109ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
110ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
111ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
112ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
113ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
114ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
115ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
116ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
117ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
118ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
119ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
120