File: source.c

package info (click to toggle)
python-bibtex 1.2.2-3.1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 376 kB
  • ctags: 616
  • sloc: ansic: 2,832; yacc: 372; python: 225; lex: 175; makefile: 53
file content (231 lines) | stat: -rw-r--r-- 4,987 bytes parent folder | download | duplicates (5)
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
 This file is part of pybliographer
 
 Copyright (C) 1998-1999 Frederic GOBRY
 Email : gobry@idiap.ch
 	   
 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2 
 of the License, or (at your option) any later version.
   
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details. 
 
 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
 $Id: source.c,v 1.1.2.1 2003/09/01 19:54:46 fredgo Exp $
*/


#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <errno.h>
#include "bibtex.h"

BibtexSource * 
bibtex_source_new (void) {
    BibtexSource * new;

    new = g_new (BibtexSource, 1);

    new->name  = NULL;
    new->type  = BIBTEX_SOURCE_NONE;
    new->table = g_hash_table_new (g_str_hash, g_str_equal);
    new->debug = FALSE;
    new->buffer = NULL;
    new->strict = TRUE;

    return new;
}

static void
reset_source (BibtexSource * source) {

    bibtex_analyzer_finish (source);

    if (source->name) {
	g_free (source->name);
    }
    
    switch (source->type) {
    case BIBTEX_SOURCE_NONE:
	break;

    case BIBTEX_SOURCE_FILE:
	fclose (source->source.file);
	break;

    case BIBTEX_SOURCE_STRING:
	g_free (source->source.string);
	break;

    default:
	g_assert_not_reached ();
    }

    source->offset = 0;
    source->line   = 1;
    source->eof    = FALSE;
    source->error  = FALSE;
}

static void
freedata (gpointer key, 
	   gpointer value, 
	   gpointer user) {

    g_free (key);

    if ((gboolean) GPOINTER_TO_INT(user)) {
	bibtex_struct_destroy ((BibtexStruct *) value, TRUE);
    }
}

BibtexStruct * 
bibtex_source_get_string (BibtexSource * source,
			  gchar * key) {
    g_return_val_if_fail (source != NULL, NULL);
    g_return_val_if_fail (key != NULL, NULL);

    return g_hash_table_lookup (source->table, key);
}

void
bibtex_source_set_string (BibtexSource * source,
			  gchar * key,
			  BibtexStruct * value) {
    gchar * inskey = key;
    BibtexStruct * oldstruct = NULL;

    g_return_if_fail (source != NULL);
    g_return_if_fail (key != NULL);

    if ((oldstruct = g_hash_table_lookup (source->table, inskey)) == NULL) {
	inskey = g_strdup (key);
	g_strdown (inskey);
    }
    else {
	bibtex_struct_destroy (oldstruct, TRUE);
    }

    g_hash_table_insert (source->table, inskey, value);
}

void           
bibtex_source_destroy (BibtexSource * source,
		       gboolean free_data) {
    g_return_if_fail (source != NULL);

    g_hash_table_foreach (source->table, freedata, GINT_TO_POINTER(free_data));
    g_hash_table_destroy (source->table);

    reset_source (source);

    g_free (source);
}


gboolean
bibtex_source_file (BibtexSource * source, 
		    gchar * filename) {
    FILE * fh = NULL;

    g_return_val_if_fail (source != NULL, FALSE);
    g_return_val_if_fail (filename != NULL, FALSE);
    
    fh = fopen (filename, "r");
    if (fh == NULL) {
	bibtex_error ("can't open file `%s': %s",
		      filename,
		      g_strerror (errno));
	return FALSE;
    }

    reset_source (source);

    source->type = BIBTEX_SOURCE_FILE;
    source->name = g_strdup (filename);
    source->source.file = fh;
    
    bibtex_analyzer_initialize (source);

    return TRUE;
}


gboolean
bibtex_source_string (BibtexSource * source, 
		      gchar * name,
		      gchar * string) {
    g_return_val_if_fail (source != NULL, FALSE);
    g_return_val_if_fail (string != NULL, FALSE);

    reset_source (source);

    source->type = BIBTEX_SOURCE_STRING;

    if (name) {
	source->name = g_strdup (name);
    }
    else {
	source->name = g_strdup ("<string>");
    }

    source->source.string = g_strdup (string);
    
    bibtex_analyzer_initialize (source);

    return TRUE;
}

void 
bibtex_source_rewind (BibtexSource * file) {

    bibtex_source_set_offset (file, 0);
}

gint 
bibtex_source_get_offset (BibtexSource * file) {
    g_return_val_if_fail (file != NULL, -1);

    return file->offset;
}
    
void
bibtex_source_set_offset (BibtexSource * file, 
			  gint offset) {
    g_return_if_fail (file != NULL);

    bibtex_analyzer_finish (file);

    switch (file->type) {
    case BIBTEX_SOURCE_FILE:
	if (fseek (file->source.file, offset, SEEK_SET) == -1) {
	    bibtex_error ("%s: can't jump to offset %d: %s", 
			  file->name,
			  offset, g_strerror (errno));
	    file->error = TRUE;
	    return;
	}
	break;

    case BIBTEX_SOURCE_STRING:
	break;

    case BIBTEX_SOURCE_NONE:
	g_warning ("no source to set offset");
	break;
    }

    file->offset = offset;
    file->eof    = file->error = FALSE;

    bibtex_analyzer_initialize (file);
}