Simple database: Difference between revisions

Content added Content deleted
m (→‎{{header|C}}: Fixed incomplete edit 155789 from 20 April 2013)
(→‎{{header|C}}: As I wrote, previous version does not compile. Now all works fine.)
Line 64: Line 64:
sort_by(last_name);
sort_by(last_name);
sort_by(title);
sort_by(title);
static sort by_date;
static int by_date(pdb_t *p1, pdb_t *p2);
/* main */
/* main */
int main (int argc, char **argv) {
int main (int argc, char **argv) {
Line 76: Line 76:
TRY (f=fopen(DB,"a+"));
TRY (f=fopen(DB,"a+"));
if (argc<2) {
if (argc<2) {
usage: printf ("Usage: %s [commands]\n"
usage: printf ("Usage: %s [commands]\n"
"-c Create new entry.\n"
"-c Create new entry.\n"
"-p Print the latest entry.\n"
"-p Print the latest entry.\n"
"-t Print all entries sorted by title.\n"
"-t Print all entries sorted by title.\n"
"-d Print all entries sorted by date.\n"
"-d Print all entries sorted by date.\n"
"-a Print all entries sorted by author.\n",argv[0]);
"-a Print all entries sorted by author.\n",argv[0]);
fclose (f);
fclose (f);
Line 112: Line 112:
printf ("-d Print all entries sorted by date.\n");
printf ("-d Print all entries sorted by date.\n");
dblist = dao (READ,f,&db,NULL);
dblist = dao (READ,f,&db,NULL);
dblist = dao (SORT,f,dblist,by_date);
dblist = dao (SORT,f,dblist,(int (*)(const void *,const void *)) by_date);
dao (PRINT,f,dblist,NULL);
dao (PRINT,f,dblist,NULL);
dao (DESTROY,f,dblist,NULL);
dao (DESTROY,f,dblist,NULL);
Line 191: Line 191:
qsort (pdb,i,sizeof in_db,sortby);
qsort (pdb,i,sizeof in_db,sortby);
pdb[i-1]->next=NULL;
pdb[i-1]->next=NULL;
for (;i;i--) {
for (i=i-1;i;i--) {
pdb[i-1]->next=pdb[i];
pdb[i-1]->next=pdb[i];
}
}
Line 221: Line 221:
}
}
/* sort by date callback for qsort */
/* sort by date callback for qsort */
static int by_date (const void *a, const void *b) {
static int by_date (pdb_t *p1, pdb_t *p2) {
const pdb_t *p1 = a;
if ((*p1)->date < (*p2)->date) {
const pdb_t *p2 = b;
if ((*p1)->date < (*p2)->date)
return -1;
return -1;
else
}
return (*p1)->date > (*p2)->date;
else return ((*p1)->date > (*p2)->date);
}</lang>
}</lang>