/[cvs]/dary/db_view.cpp
ViewVC logotype

Annotation of /dary/db_view.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.1 - (hide annotations)
Tue Oct 5 19:56:29 2004 UTC (14 years, 8 months ago) by riso
Branch: MAIN
Branch point for: riso
Initial revision

1 riso 1.1 #include "db_view.h"
2     #include "util.h"
3     #include <stdlib.h>
4    
5     #define MAX_COMPL 500
6    
7     static int inited=0;
8     static int mode=MODE_ENTRIES, sort=SORT_DATE;
9     static int pn=-1, en=-1;
10     static tPersonId *pa=NULL;
11     static tEntryId *ea=NULL;
12    
13     static int cmp_entries_date(const void *a, const void *b) {
14     int i,j;
15     i = (*((tEntryId *)a))->date;
16     j = (*((tEntryId *)b))->date;
17     if (i<j) return -1;
18     if (i>j) return 1;
19     return 0;
20     }
21    
22     static int cmp_entries_sum(const void *a, const void *b) {
23     int i,j;
24     i = (*((tEntryId *)a))->sum;
25     j = (*((tEntryId *)b))->sum;
26     if (i<j) return -1;
27     if (i>j) return 1;
28     return 0;
29     }
30    
31     static int cmp_entries_name(const void *a, const void *b) {
32     int i;
33     i = mystrcmp( (*((tEntryId *)a))->person->surname, (*((tEntryId *)b))->person->surname );
34     if (i!=0) return i;
35     return mystrcmp( (*((tEntryId *)a))->person->name, (*((tEntryId *)b))->person->name );
36     }
37    
38     static int cmp_persons_name(const void *a, const void *b) {
39     int i;
40     i = mystrcmp( (*((tPersonId *)a))->surname, (*((tPersonId *)b))->surname );
41     if (i!=0) return i;
42     return mystrcmp( (*((tPersonId *)a))->name, (*((tPersonId *)b))->name );
43     }
44    
45     static int cmp_persons_sum(const void *a, const void *b) {
46     int i,j;
47     i = (*((tPersonId *)a))->sum;
48     j = (*((tPersonId *)b))->sum;
49     if (i<j) return -1;
50     if (i>j) return 1;
51     return 0;
52     }
53    
54     void UpdateViewPerson(tPersonId id, int action) {}
55     void UpdateViewEntry(tEntryId id, int action) {}
56    
57     void ResetView() {
58     int i;
59    
60     if (!inited) return;
61    
62     if (mode==MODE_PERSONS) {
63     if (pn != persons->n) {
64     if (pa!=NULL) free(pa);
65     pn = persons->n;
66     pa = ALLOC(tPersonId, pn);
67     }
68     for (i=0; i<persons->n; i++)
69     pa[i]=(tPersonId)(persons->data[i]);
70     if (sort==SORT_NAME) {
71     qsort(pa, pn, sizeof(tPersonId), cmp_persons_name);
72     } else if (sort==SORT_DATE) {
73     // invalid option
74     } else if (sort==SORT_SUM) {
75     qsort(pa, pn, sizeof(tPersonId), cmp_persons_sum);
76     }
77     } else if (mode==MODE_ENTRIES) {
78     if (en != entries->n) {
79     if (ea!=NULL) free(ea);
80     en = entries->n;
81     ea = ALLOC(tEntryId, en);
82     }
83     for (i=0; i<entries->n; i++)
84     ea[i]=(tEntryId)(entries->data[i]);
85     if (sort==SORT_NAME) {
86     qsort(ea, en, sizeof(tEntryId), cmp_entries_name);
87     } else if (sort==SORT_DATE) {
88     qsort(ea, en, sizeof(tEntryId), cmp_entries_date);
89     } else if (sort==SORT_SUM) {
90     qsort(ea, en, sizeof(tEntryId), cmp_entries_sum);
91     }
92     }
93     }
94    
95     void InitView() {
96     DeleteView();
97     inited=1;
98     }
99    
100     static void DeleteView2() {
101     if (pa!=NULL) free(pa);
102     if (ea!=NULL) free(ea);
103     pa=NULL; ea=NULL;
104     pn=en=-1;
105     }
106     void DeleteView() {
107     DeleteView2();
108     inited=0;
109     }
110    
111     void SetMode(int _mode) { DeleteView2(); mode=_mode; ResetView(); }
112     void SetSort(int _sort) { DeleteView2(); sort=_sort; ResetView(); }
113    
114     int nRows() {
115     if (mode==MODE_PERSONS) {
116     if (pn<0) return 0; else return pn;
117     } else if (mode==MODE_ENTRIES) {
118     if (en<0) return 0; else return en;
119     }
120     return 0;
121     }
122    
123    
124     void *getRow(int r) {
125     if (mode==MODE_PERSONS) {
126     return (void *)(pa[r]);
127     } else if (mode==MODE_ENTRIES) {
128     return (void *)(ea[r]);
129     }
130     return NULL;
131     }
132    
133     static int cmp_comp(const void *a, const void *b) {
134     char **A = (char **)a,**B = (char **)b;
135     int i;
136    
137     i = mystrcmp(A[1], B[1]);
138     if (i==0) i = mystrcmp(A[0], B[0]);
139     return i;
140     }
141    
142     char **Completion(char *name, char *surname) {
143     int i,n, nlen=strlen(name), snlen=strlen(surname);
144     char **o;
145    
146     for (n=i=0; i<persons->n; i++) {
147     if (strncmp(name, ((tPersonId)(persons->data[i]))->name, nlen)==0)
148     if (strncmp(surname, ((tPersonId)(persons->data[i]))->surname, snlen)==0) {
149     n++;
150     if (n>MAX_COMPL) return NULL;
151     }
152     }
153    
154     o = ALLOC(char *, 2*n+1);
155    
156     for (n=i=0; i<persons->n; i++) {
157     if (strncmp(name, ((tPersonId)(persons->data[i]))->name, nlen)==0)
158     if (strncmp(surname, ((tPersonId)(persons->data[i]))->surname, snlen)==0) {
159     o[n] = ALLOC(char, strlen(((tPersonId)(persons->data[i]))->name)+1);
160     strcpy(o[n], ((tPersonId)(persons->data[i]))->name);
161     n++;
162     o[n] = ALLOC(char, strlen(((tPersonId)(persons->data[i]))->surname)+1);
163     strcpy(o[n], ((tPersonId)(persons->data[i]))->surname);
164     n++;
165     }
166     }
167    
168     qsort(o, n/2, sizeof(char *)*2, cmp_comp);
169     o[n] = NULL;
170     return o;
171     }
172    

CVS Admin">CVS Admin
ViewVC Help
Powered by ViewVC 1.1.26