Simple database: Difference between revisions

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