]> www.wagner.pp.ru Git - oss/fgis.git/blob - lib/epp_cache.c
First checked in version
[oss/fgis.git] / lib / epp_cache.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include "epp.h"
5 #define BAD_ROW -32769
6 int search_cache(EPP *epp,int row);
7 void position_input(EPP *epp,int row);
8 void position_cached(EPP *epp, int row)/* makes given line current */
9
10   if (row==epp->currentline) return;
11   if (!search_cache(epp,row))
12      { position_input(epp,row);
13     }        
14 }
15 typedef struct CACHE_REC { int row_no;
16                            unsigned short *row_ptr;} cache_rec;
17 int search_cache(EPP *epp,int row)
18 { cache_rec* c;unsigned short int *rp; 
19   int i;
20   for(c=epp->cache,i=0;i<epp->cache_size && c->row_no!=BAD_ROW&&c->row_no!=row;
21           i++) c++;
22   if (i==epp->cache_size) {
23    /* Cache is full and row not found. Discard last row */
24    c--;
25   }
26   rp=c->row_ptr;
27   i=epp->currentline;
28   epp->currentline=c->row_no;
29   /* move all rows one forward */
30   for(;c!=epp->cache;c--) *c=*(c-1);
31   c=epp->cache;/* may be this is not nessecary*/
32   c->row_no=i;
33   c->row_ptr=epp->row;
34   epp->row=rp;
35   return epp->currentline==row;
36 }
37 void free_cache(EPP *epp)
38 { int i;cache_rec *c;
39   for (i=0,c=epp->cache;i<epp->cache_size;i++,c++)
40    free(c->row_ptr);
41   if (epp->cache) free(epp->cache);
42   epp->cache=NULL;
43   epp->cache_size=0;
44 }     
45 int set_epp_cache(EPP *epp,int lines)
46 { int i; cache_rec* c;
47   if (!(epp->mode&MAP_INPUT)||(epp->mode&MAP_LOADED))
48   { map_error = ME_INVALID_MODE;
49     return 0;
50   }
51  free_cache(epp);
52   /* this is really assignment inside condition*/
53  if (lines)  
54  {if (!(epp->cache=calloc(lines, sizeof(cache_rec))))
55    { return 0; }
56   for (i=0,c=epp->cache;i<lines;i++,c++)
57     {  c->row_ptr=calloc(epp->width+1,sizeof(short));
58        c->row_no=-32768;
59     }
60   epp->cache_size=lines;
61   epp->position=position_cached;
62   }
63   else epp->position=position_input;
64  return 1;
65
66