~ubuntu-branches/ubuntu/oneiric/e00compr/oneiric

« back to all changes in this revision

Viewing changes to ex_readcb.c

  • Committer: Bazaar Package Importer
  • Author(s): Paul Wise
  • Date: 2005-11-15 15:43:39 UTC
  • Revision ID: james.westby@ubuntu.com-20051115154339-tmt82iiu3dwtk94a
Tags: upstream-1.0.0
ImportĀ upstreamĀ versionĀ 1.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**********************************************************************
 
2
 *                          ex_readcb.c
 
3
 *
 
4
 * This example program illustrates the use of the E00ReadCallbackOpen()
 
5
 * and associated compressed E00 read functions.
 
6
 **********************************************************************/
 
7
 
 
8
#include <stdio.h>
 
9
 
 
10
#include "e00compr.h"
 
11
 
 
12
static const char *myReadNextLine(void *pRefData);
 
13
static void       myReadRewind(void *pRefData);
 
14
 
 
15
int main(int argc, char *argv[])
 
16
{
 
17
    E00ReadPtr  hReadPtr;
 
18
    const char  *pszLine;
 
19
    FILE        *fp;
 
20
 
 
21
    /* Open input file */
 
22
    if ((fp = fopen("test.e00", "rt")) == NULL)
 
23
    {
 
24
        /* Error */
 
25
        printf("Cannot open input file test.e00\n");
 
26
        return 1;
 
27
    }
 
28
 
 
29
    /* Initialize reader */
 
30
    hReadPtr = E00ReadCallbackOpen((void*)fp, 
 
31
                                   myReadNextLine, myReadRewind);
 
32
 
 
33
    if (hReadPtr)
 
34
    {
 
35
        /* Read lines from input until we reach EOF */
 
36
        while((pszLine = E00ReadNextLine(hReadPtr)) != NULL)
 
37
        {
 
38
            if (E00GetLastErrorNo() == 0)
 
39
                printf("%s\n", pszLine);
 
40
            else
 
41
            {
 
42
                /* An error happened while reading the last line... */
 
43
                break;
 
44
            }
 
45
        }
 
46
 
 
47
        /* Close input file */
 
48
        E00ReadClose(hReadPtr);
 
49
 
 
50
        fclose(fp);
 
51
    }
 
52
    else
 
53
    {
 
54
        /* ERROR ... file is not a valid E00 */
 
55
    }
 
56
 
 
57
    return 0;
 
58
}
 
59
 
 
60
 
 
61
 
 
62
/**********************************************************************
 
63
 *                          myReadNextLine()
 
64
 *
 
65
 * My own implementation of the ReadNextLine() function to test the 
 
66
 * E00ReadCallbackOpen() functions.
 
67
 * 
 
68
 * This function must return a reference to static buffer with the next
 
69
 * line of input, or NULL when it reaches EOF.
 
70
 **********************************************************************/
 
71
static const char *myReadNextLine(void *pRefData)
 
72
{
 
73
    FILE *fp;
 
74
    static char szBuf[256];
 
75
 
 
76
    fp = (FILE *)pRefData;
 
77
 
 
78
    if (fgets(szBuf, 255, fp) == NULL)
 
79
        return NULL;
 
80
 
 
81
    szBuf[255] = '\0';
 
82
 
 
83
    return szBuf;
 
84
}
 
85
 
 
86
/**********************************************************************
 
87
 *                          myReadRewind()
 
88
 *
 
89
 * Callback function to rewind the file being read by myReadNextLine()
 
90
 **********************************************************************/
 
91
static void myReadRewind(void *pRefData)
 
92
{
 
93
    rewind((FILE *)pRefData);
 
94
}
 
95
 
 
96