1 /*
2 
3 Copyright (C) 2000  Paul Wilkins
4 
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
9 
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14 
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18 
19 */
20 /* read_db.c by Paul Wilkins 1/2/2000 */
21 
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 
26 #include "read_line.h"
27 #include "read_db.h"
28 
malloc_db(int strlen,int n)29 struct PIC_DB *malloc_db(int strlen, int n){
30    int i;
31    struct PIC_DB *db;
32    if(NULL == (db=malloc(sizeof(struct PIC_DB)))){
33       perror("malloc");
34       exit(1);
35    }
36    if(NULL == (db->fname=malloc((strlen+1)*sizeof(char)))){
37       perror("malloc");
38       exit(1);
39    }
40    if(NULL == (db->data=malloc(n*sizeof(int *)))){
41       perror("malloc");
42       exit(1);
43    }
44    for(i=0; i<n; i++){
45       if(NULL == (db->data[i]=malloc(3*(i+1)*(i+1)*sizeof(int)))){
46 	 perror("malloc");
47 	 exit(1);
48       }
49    }
50    return db;
51 }
52 
53 
reset_db_data(struct PIC_DB * head)54 void reset_db_data(struct PIC_DB *head){
55    struct PIC_DB *db;
56 
57    for(db=head; db!=NULL; db=db->next){
58       db->refcnt = 0;
59       db->done = 0;
60    }
61 }
62 
63 
read_database(int * max_order)64 struct PIC_DB *read_database(int *max_order){
65    int i, j, done;
66    int len;
67    int ndb;
68    int size;
69    int ret;
70    FILE *dbfp;
71    int *p1;
72    char line[1024];
73    struct PIC_DB *head, *db;
74 
75    if(NULL == (dbfp=fopen("pic_db.dat", "r"))){
76       fprintf(stderr, "Error opening pic_db.dat for read\n");
77       exit(1);
78    }
79    ret = read_line(dbfp, line);
80    if(ret == EOF) {
81       fprintf(stderr, "Error: can't read first line from pic_db.dat\n");
82       exit(1);
83    }
84    if(1 != sscanf(line, "%d", &size)){
85       fprintf(stderr, "Error: can't read size from pic_db.dat\n");
86       exit(1);
87    }
88    *max_order = size;
89 
90    ndb = 0;
91    head = NULL;
92 
93    /* read in the db */
94    done = 0;
95    do {
96       ret = read_line(dbfp, line);
97       if(*line == '\0') continue;
98       if(*line == '#') continue;
99       if(*line == '5') continue;
100 
101       len = strlen(line);
102 
103       db = malloc_db(len, size);
104 
105       strcpy(db->fname, line);
106       /* printf("filename: %s\n", db->fname);  */
107 
108       for(j=0; j<size; j++){
109 
110 	 p1 = db->data[j];
111 
112 	 for(i=0; i<3*(j+1)*(j+1); i++){
113 	    if(1 != fscanf(dbfp, "%d", p1)){
114 	       fprintf(stderr, "Error: File %d: %s: reading value %d from pic_db.dat\n",
115 		  ndb, db->fname, i);
116 	       exit(1);
117 	    }
118 	    /* printf("Read value no: %d: %d\n", i, *p1); */
119 	    p1++;
120 	 }
121       }
122 
123       /* add db to the list */
124       db->next = head;
125       head = db;
126       ndb++;
127 
128    } while (ret != EOF && !done);
129 
130    fclose(dbfp);
131 
132    return head;
133 }
134