summaryrefslogtreecommitdiffstats
path: root/lib/libsi18n/txtfile.c
blob: a88d167251d801ff7e4c7ff6dcb0a5d96e9227f0 (plain)
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/** BEGIN COPYRIGHT BLOCK
 * Copyright 2001 Sun Microsystems, Inc.
 * Portions copyright 1999, 2001-2003 Netscape Communications Corporation.
 * All rights reserved.
 * END COPYRIGHT BLOCK **/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>


#include "txtfile.h"



#if 0
char fileBuffer[FILE_BUFFER_SIZE + 1];
char *fbCurrent;
int  fbSize;
int fbStatus;
#endif


TEXTFILE * OpenTextFile(char *filename, int access)
{
    TEXTFILE *txtfile;
    FILE *file;
    int status;

    if (access == TEXT_OPEN_FOR_WRITE) {
        status = TEXT_FILE_WRITING;
	    file = fopen(filename, "w+");
    }
    else {
        status = TEXT_FILE_READING;
	    file = fopen(filename, "r");
    }

    if (file == NULL)
        return NULL;

    txtfile = (TEXTFILE *) malloc (sizeof(TEXTFILE));
    memset(txtfile, 0, sizeof(TEXTFILE));

    txtfile->file = file;
    txtfile->fbStatus = status;

	txtfile->fbCurrent = txtfile->fileBuffer;
	*txtfile->fbCurrent = '\0';
	txtfile->fbSize = 0;
    return txtfile;
}


void CloseTextFile(TEXTFILE *txtfile)
{
    if (txtfile) {
	    fclose(txtfile->file);
        free(txtfile);
    }
    
}

int FillTextBuffer(TEXTFILE *txtfile)
{
	int nLeft, size;
	nLeft = strlen(txtfile->fbCurrent); 
	memcpy(txtfile->fileBuffer, txtfile->fbCurrent, nLeft+1);

	size = fread(txtfile->fileBuffer + nLeft, 1, FILE_BUFFER_SIZE - nLeft, txtfile->file);
	if (size == 0)
		return 0;

	txtfile->fbCurrent = txtfile->fileBuffer;
	*(txtfile->fbCurrent + size + nLeft) = '\0';
	txtfile->fbSize = size + nLeft;

	return size;
}

int ReadTextLine(TEXTFILE *txtfile, char *linebuf)
{
	char *p, *q;

    if (txtfile->fbStatus == TEXT_FILE_DONE)
        return -1;

	p = txtfile->fbCurrent;
	q = strchr(p, '\n');
	if (q)
	{
		*q = '\0';
		strcpy(linebuf, p);
		txtfile->fbCurrent = q + 1;
		return strlen(linebuf);
	}
	else
	{
		if (FillTextBuffer(txtfile) == 0)
		{   /* Done with file reading,
               return last line
             */
            txtfile->fbStatus = TEXT_FILE_DONE;
            if (*txtfile->fbCurrent) {
			    strcpy(linebuf, txtfile->fbCurrent);
                CloseTextFile(txtfile);
                return strlen(linebuf);
            }
            else {
                CloseTextFile(txtfile);
                return -1;
            }
		}
        else { 
		    p = txtfile->fbCurrent;
		    q = strchr(p, '\n');
		    if (q)
		    {
			    *q = '\0';
			    strcpy(linebuf, p);
			    txtfile->fbCurrent = q + 1;
		    }
		    else
		    {
			    strcpy(linebuf, txtfile->fbCurrent);
			    txtfile->fbCurrent = txtfile->fbCurrent + strlen(linebuf);
		    }
        }
		return strlen(linebuf);
	}
}