File: read_db.c

package info (click to toggle)
pixelize 0.9.2-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 304 kB
  • ctags: 199
  • sloc: ansic: 2,349; makefile: 77
file content (132 lines) | stat: -rw-r--r-- 2,987 bytes parent folder | download | duplicates (4)
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
/*

Copyright (C) 2000  Paul Wilkins

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.

*/
/* read_db.c by Paul Wilkins 1/2/2000 */

#include <stdio.h>
#include <stdlib.h>

#include "read_line.h"
#include "read_db.h"

struct PIC_DB *malloc_db(int strlen, int n){
   int i;
   struct PIC_DB *db;
   if(NULL == (db=malloc(sizeof(struct PIC_DB)))){
      perror("malloc");
      exit(1);
   }
   if(NULL == (db->fname=malloc((strlen+1)*sizeof(char)))){
      perror("malloc");
      exit(1);
   }
   if(NULL == (db->data=malloc(n*sizeof(int *)))){
      perror("malloc");
      exit(1);
   }
   for(i=0; i<n; i++){
      if(NULL == (db->data[i]=malloc(3*(i+1)*(i+1)*sizeof(int)))){
	 perror("malloc");
	 exit(1);
      }
   }
   return db;
}


void reset_db_data(struct PIC_DB *head){
   struct PIC_DB *db;

   for(db=head; db!=NULL; db=db->next){
      db->refcnt = 0;
      db->done = 0;
   }
}


struct PIC_DB *read_database(int *max_order){
   int i, j, done;
   int len;
   int ndb;
   int size;
   int ret;
   FILE *dbfp;
   int *p1;
   char line[1024];
   struct PIC_DB *head, *db;

   if(NULL == (dbfp=fopen("pic_db.dat", "r"))){
      fprintf(stderr, "Error opening pic_db.dat for read\n");
      exit(1);
   }
   ret = read_line(dbfp, line);
   if(ret == EOF) {
      fprintf(stderr, "Error: can't read first line from pic_db.dat\n");
      exit(1);
   }
   if(1 != sscanf(line, "%d", &size)){
      fprintf(stderr, "Error: can't read size from pic_db.dat\n");
      exit(1);
   }
   *max_order = size;

   ndb = 0;
   head = NULL;

   /* read in the db */
   done = 0;
   do {
      ret = read_line(dbfp, line);
      if(*line == '\0') continue;
      if(*line == '#') continue;
      if(*line == '5') continue;

      len = strlen(line);

      db = malloc_db(len, size);

      strcpy(db->fname, line);
      /* printf("filename: %s\n", db->fname);  */

      for(j=0; j<size; j++){

	 p1 = db->data[j];

	 for(i=0; i<3*(j+1)*(j+1); i++){
	    if(1 != fscanf(dbfp, "%d", p1)){
	       fprintf(stderr, "Error: File %d: %s: reading value %d from pic_db.dat\n", 
		  ndb, db->fname, i);
	       exit(1);
	    }
	    /* printf("Read value no: %d: %d\n", i, *p1); */
	    p1++;
	 }
      }

      /* add db to the list */
      db->next = head;
      head = db;
      ndb++;
      
   } while (ret != EOF && !done);

   fclose(dbfp);

   return head;
}