File: ex_writecb.c

package info (click to toggle)
e00compr 1.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 272 kB
  • ctags: 238
  • sloc: ansic: 1,316; makefile: 69
file content (103 lines) | stat: -rw-r--r-- 3,150 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/**********************************************************************
 *                          ex_writecb.c
 *
 * This example program illustrates the use of the E00WriteCallbackOpen()
 * and associated compressed E00 write functions.
 **********************************************************************/
#include <stdio.h>

#include "e00compr.h"

static int myWriteNextLine(void *pRefData, const char *pszLine);

int main(int argc, char *argv[])
{
    E00ReadPtr  hReadPtr;
    E00WritePtr hWritePtr = NULL;
    const char  *pszLine;
    FILE        *fp;
    int         nStatus = 0;

    /* Open input file */
    hReadPtr = E00ReadOpen("test1.e00");

    if (hReadPtr)
    {
        /* Open output file, and if succesful then initialize the writer
         * using the myWriteNextLine callback method.
         * For the purposes of this example, fp will be used directly
         * as reference data for the callback.
         */
        if ((fp = fopen("test2.e00", "wt")) != NULL)
        {
            hWritePtr = E00WriteCallbackOpen((void *)fp,
                                             myWriteNextLine,
                                             E00_COMPR_FULL);
        }

        if (hWritePtr)
        {
            /* Read lines from input until we reach EOF */
            while((pszLine = E00ReadNextLine(hReadPtr)) != NULL)
            {
                if ((nStatus = CPLGetLastErrorNo()) == 0)
                    nStatus = E00WriteNextLine(hWritePtr, pszLine);

                if (nStatus != 0)
                {
                    /* An error happened while converting the last 
                     * line... abort*/
                    break;
                }
            }
            /* Close output file. */
            E00WriteClose(hWritePtr);
        }
        else
        {
            /* ERROR ... failed to open output file */
            nStatus = CPLGetLastErrorNo();
        }

        /* Close the physical output file */
        if (fp)
            fclose(fp);

        /* Close input file. */
        E00ReadClose(hReadPtr);
    }
    else
    {
        /* ERROR ... failed to open input file */
        nStatus = CPLGetLastErrorNo();
    }

    return nStatus;
}


/**********************************************************************
 *                          myWriteNextLine()
 *
 * My own implementation of the ReadWriteNextLine() function to test the 
 * E00WriteCallbackOpen() functions.
 * 
 * This function writes a line to the E00 file.  The line that is passed
 * as argument won't be terminated by a '\n', it is the responsibility
 * of this function to add one if needed.
 *
 * myWriteNextLine() should return a positive value on success (the number
 * of chars written, as printf() does) or -1 if an error happened...
 **********************************************************************/
static int myWriteNextLine(void *pRefData, const char *pszLine)
{
    int nStatus;

    /* For the purpose of this example, pRefData is the actual FILE *
     * but it can be whatever you want...
     */

    nStatus =  fprintf((FILE *)pRefData, "%s\n", pszLine);

    return nStatus;
}