Moved 'current' to table header; fixed table load; added project show and delete

This commit is contained in:
Mike Dvorkin
2010-07-19 23:39:35 -07:00
parent e3a017b249
commit 73427f743c
5 changed files with 92 additions and 44 deletions

View File

@@ -9,7 +9,6 @@ typedef unsigned char uchar;
typedef struct {
ulong id;
int current; /* 1 if the project is current, 0 otherwise. */
char name[128]; /* Project name. */
char status[16]; /* Project status. */
ulong number_of_open_tasks; /* Number of open tasks. */
@@ -25,7 +24,6 @@ typedef struct {
typedef struct {
ulong id;
ulong project_id; /* Which project the task belongs to? */
int current; /* 1 if the task is current, 0 otherwise. */
int priority; /* Task priority. */
char name[128]; /* Task name. */
char status[16]; /* Task status. */

View File

@@ -27,7 +27,7 @@ static void list_projects()
pit_db_load();
for(i = 0, pp = (PProject)projects->slots; i < projects->number_of_records; i++, pp++) {
printf("%lu: [%s] %s\n", pp->id, pp->status, pp->name);
printf("%c %lu: [%s] %s\n", (pp->id == projects->current ? '*' : ' '), pp->id, pp->status, pp->name);
}
}
@@ -35,8 +35,10 @@ static void create_project(char *name, char *status)
{
pit_db_load();
if (!already_exist(name)) {
Project p;
if (already_exist(name)) {
die("project with the same name already exists");
} else {
Project p, *pp;
memset(&p, 0, sizeof(p));
@@ -47,33 +49,54 @@ static void create_project(char *name, char *status)
strncpy(p.name, name, sizeof(p.name) - 1);
strncpy(p.status, status, sizeof(p.status) - 1);
p.current = 1;
p.number_of_open_tasks = 0;
p.number_of_closed_tasks = 0;
p.closed_by = 0;
p.created_by = p.updated_by = 1; // TODO
p.closed_at = 0;
p.created_at = p.updated_at = time(NULL);
pit_table_insert(projects, (uchar *)&p);
pp = (PProject)pit_table_insert(projects, (uchar *)&p);
pit_table_mark(projects, pp->id);
pit_db_save();
}
}
static int show_project(unsigned long number)
static int show_project(ulong number)
{
printf("showing project %lu\n", number);
PProject pp;
pit_db_load();
pp = (PProject)pit_table_find(projects, number);
if (pp) {
printf("%lu: [%s] %s\n", pp->id, pp->status, pp->name);
pit_table_mark(projects, pp->id);
pit_db_save();
} else {
die("could not find the project");
}
return 1;
}
static int delete_project(unsigned long number)
{
PProject pp;
printf("deleting project %lu\n", number);
pit_db_load();
pp = (PProject)pit_table_delete(projects, number);
if (pp) {
pit_table_mark(projects, 0);
pit_db_save();
} else {
die("could not delete the project");
}
return 1;
}
int pit_project(char *argv[])
{
char **arg = &argv[1];
unsigned long number;
if (!*arg) {
list_projects();
@@ -83,21 +106,25 @@ int pit_project(char *argv[])
} else {
create_project(*arg, *(arg + 1));
}
} else if (!strcmp(*arg, "-d") || !strcmp(*arg, "-s")) {
} else if (!strcmp(*arg, "-d")) {
if (!*++arg) {
die("missing project number");
} else {
unsigned long number = atoi(*arg);
number = atoi(*arg);
if (!number) {
die("invalid project number");
} else {
if (!strcmp(*--arg, "-d")) {
delete_project(number);
} else {
show_project(number);
}
delete_project(number);
}
}
/* } else if (!strcmp(*arg, "-e")) { TODO: Edit */
} else {
number = atoi(*arg);
if (!number) {
die("invalid project parameters");
} else {
show_project(number);
}
}
return 1;

View File

@@ -4,7 +4,7 @@
#include <time.h>
#include "pit.h"
#define TABLE_INCREMENT 5
#define TABLE_INCREMENT 10
/*
** Initialize the table by alloocating necessary memory chunks.
*/
@@ -17,6 +17,7 @@ PTable pit_table_initialize(ulong record_size, ulong flags) {
pt->number_of_slots = TABLE_INCREMENT;
pt->number_of_records = 0;
pt->auto_increment = 0;
pt->current = 0;
pt->slots = calloc(TABLE_INCREMENT, pt->record_size);
pt->index = calloc(TABLE_INCREMENT, sizeof(uchar *));
@@ -104,8 +105,8 @@ uchar *pit_table_find(PTable pt, ulong id) {
}
/*
** Delete a record by its ID. Return the address of next record or NULL
** if deleted last record.
** Delete a record by its ID. Return the address of deleted record or NULL
** if the record was not found.
*/
uchar *pit_table_delete(PTable pt, ulong id) {
// TODO: retrn NULL (or raise?) if table doesn't have id.
@@ -138,11 +139,7 @@ uchar *pit_table_delete(PTable pt, ulong id) {
pt->number_of_records--;
}
if (pr && pt->number_of_records > 0) {
return pr;
} else {
return NULL;
}
return pr;
}
/*
@@ -184,6 +181,20 @@ uchar *pit_table_insert(PTable pt, uchar *record) {
return *pi;
}
/*
** Find current record.
*/
uchar *pit_table_current(PTable pt) {
return pit_table_find(pt, pt->current);
}
/*
** Set current record as indicated by the id, then find and return it.
*/
uchar *pit_table_mark(PTable pt, ulong id) {
return pit_table_find(pt, pt->current = id);
}
/*
** Release pt->slots and pt->index memory chunks, then free the table itself.
*/
@@ -209,9 +220,9 @@ int pit_table_save(FILE *file, PTable pt) {
register int written = 0;
/*
** Save table header data: flags, record_size, number_of_slots,
** number_of_records, and auto_increment.
** number_of_records, and auto_increment, current.
*/
written += fwrite(pt, sizeof(ulong), 5, file);
written += fwrite(pt, sizeof(ulong), 6, file);
/*
** Save the records. Note that we save the actual (not allocated) data.
*/
@@ -233,9 +244,9 @@ PTable pit_table_load(FILE *file) {
/*
** First read the header.
*/
read += fread(pt, sizeof(ulong), 5, file);
read += fread(pt, sizeof(ulong), 6, file);
/*
** Now allocate slots and index based n original number of slots.
** Now allocate slots and index based on the original number of slots.
*/
/*** printf("Allocating %lu slots\n", pt->number_of_slots); ***/
pt->slots = pr = calloc(pt->number_of_slots, pt->record_size);
@@ -244,9 +255,9 @@ PTable pit_table_load(FILE *file) {
** Now read the records into the slots and rebuild the index.
*/
read += fread(pt->slots, pt->record_size, pt->number_of_records, file);
for(i = 0; i < pt->number_of_records; i++, pi++) {
for(i = 1; i <= pt->number_of_slots; i++, pi++) {
// TODO: if table doesn't have id || has id && id matches...
if ((ulong)*pr == i + 1) {
if ((ulong)*pr == i) {
*pi = pr;
pr += pt->record_size;
}
@@ -254,7 +265,6 @@ PTable pit_table_load(FILE *file) {
return pt;
}
/* #define TEST */
#if defined(TEST)
@@ -268,7 +278,7 @@ typedef struct {
void dump(Record *prec) {
if (prec) {
printf("(%08lX) id: %08lu, value: %lu, created_at: %lu, updated_at: %lu)\n",
printf("(%08lX) id: %08lu, value: %lu, created_at: %lu, updated_at: %lu\n",
(ulong)prec, prec->id, prec->value, prec->created_at, prec->updated_at);
} else {
printf("(NULL)\n");
@@ -293,7 +303,7 @@ int main() {
PTable pt;
Record rec, *prec;
ulong i, total = 30;
ulong i, total = 3;
pt = pit_table_initialize(sizeof(Record), TABLE_HAS_ID | TABLE_HAS_TIMESTAMPS);
for(i = 0; i < total; i++) {
@@ -305,10 +315,17 @@ int main() {
prec = (Record *)pit_table_insert(pt, (uchar *)&rec);
dump(prec);
}
for (i = 20; i < total; i++) {
printf("Deleting %lu\n", i + 1);
prec = (Record *)pit_table_delete(pt, i + 1);
}
prec = (Record *)pit_table_find(pt, total - 1);
pit_table_mark(pt, prec->id);
printf("current: %lu\n", pt->current);
// for (i = 20; i < total; i++) {
// printf("Deleting %lu\n", i + 1);
// prec = (Record *)pit_table_delete(pt, i + 1);
// }
printf("Deleting %lu\n", 1L);
prec = (Record *)pit_table_delete(pt, 1);
printf("current: %lu\n", pt->current);
dump_all(pt);
FILE *file = fopen("/tmp/.pit", "w");
@@ -318,6 +335,7 @@ int main() {
file = fopen("/tmp/.pit", "r");
pt = pit_table_load(file);
dump_all(pt);
printf("current: %lu\n", pt->current);
fclose(file);
pit_table_free(pt);

BIN
src/table.c.out Executable file

Binary file not shown.

View File

@@ -7,13 +7,14 @@
#define TABLE_HAS_TIMESTAMPS (TABLE_HAS_CREATED_AT | TABLE_HAS_UPDATED_AT)
typedef struct {
ulong flags; /* Bit mask with table flags */
ulong record_size; /* Record size in bytes; all records are of fixed size */
ulong number_of_slots; /* Number of slots allocated, each slot is 'record_size' long */
ulong number_of_records; /* Number of records currently stored in slots */
ulong auto_increment; /* Current value of record id */
uchar *slots; /* Memory chunk to store records; compacted when a record gets deleted (no holes) */
uchar **index; /* Memory chunk to store pointers to individual records, holes for deleted record ids */
ulong flags; /* Bit mask with table flags. */
ulong record_size; /* Record size in bytes; all records are of fixed size. */
ulong number_of_slots; /* Number of slots allocated, each slot is 'record_size' long. */
ulong number_of_records; /* Number of records currently stored in slots. */
ulong auto_increment; /* Current value of record id. */
ulong current; /* The id of currently selected record, one per table. */
uchar *slots; /* Memory chunk to store records; compacted when a record gets deleted (no holes). */
uchar **index; /* Memory chunk to store pointers to individual records, holes for deleted record IDs. */
} Table, *PTable;
PTable pit_table_initialize(ulong record_size, ulong flags);
@@ -21,6 +22,10 @@ PTable pit_table_free(PTable pt);
uchar *pit_table_find(PTable pt, ulong id);
uchar *pit_table_delete(PTable pt, ulong id);
uchar *pit_table_insert(PTable pt, uchar *record);
uchar *pit_table_current(PTable pt);
uchar *pit_table_mark(PTable pt, ulong id);
int pit_table_save(FILE *file, PTable pt);
PTable pit_table_load(FILE *file);