/[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.2 - (hide annotations)
Fri Nov 5 20:48:15 2004 UTC (14 years, 7 months ago) by riso
Branch: MAIN
CVS Tags: Obalky
Changes since 1.1: +7 -0 lines
Port do mingw; pocty ludi/darov; tlac aj bez oznacenia; slovenske triedenie

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 riso 1.2 int nPersons() {
124     return persons->n;
125     }
126    
127     int nEntries() {
128     return entries->n;
129     }
130 riso 1.1
131     void *getRow(int r) {
132     if (mode==MODE_PERSONS) {
133     return (void *)(pa[r]);
134     } else if (mode==MODE_ENTRIES) {
135     return (void *)(ea[r]);
136     }
137     return NULL;
138     }
139    
140     static int cmp_comp(const void *a, const void *b) {
141     char **A = (char **)a,**B = (char **)b;
142     int i;
143    
144     i = mystrcmp(A[1], B[1]);
145     if (i==0) i = mystrcmp(A[0], B[0]);
146     return i;
147     }
148    
149     char **Completion(char *name, char *surname) {
150     int i,n, nlen=strlen(name), snlen=strlen(surname);
151     char **o;
152    
153     for (n=i=0; i<persons->n; i++) {
154     if (strncmp(name, ((tPersonId)(persons->data[i]))->name, nlen)==0)
155     if (strncmp(surname, ((tPersonId)(persons->data[i]))->surname, snlen)==0) {
156     n++;
157     if (n>MAX_COMPL) return NULL;
158     }
159     }
160    
161     o = ALLOC(char *, 2*n+1);
162    
163     for (n=i=0; i<persons->n; i++) {
164     if (strncmp(name, ((tPersonId)(persons->data[i]))->name, nlen)==0)
165     if (strncmp(surname, ((tPersonId)(persons->data[i]))->surname, snlen)==0) {
166     o[n] = ALLOC(char, strlen(((tPersonId)(persons->data[i]))->name)+1);
167     strcpy(o[n], ((tPersonId)(persons->data[i]))->name);
168     n++;
169     o[n] = ALLOC(char, strlen(((tPersonId)(persons->data[i]))->surname)+1);
170     strcpy(o[n], ((tPersonId)(persons->data[i]))->surname);
171     n++;
172     }
173     }
174    
175     qsort(o, n/2, sizeof(char *)*2, cmp_comp);
176     o[n] = NULL;
177     return o;
178     }
179    

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