Browse Source

* use single imdb_t->flags instead separate write and caps members

* make caps as bit mask
* prepare for imdb_open returns handle
master
Alex 'AdUser' Z 8 years ago
parent
commit
dc64ed210c
  1. 29
      src/database.c
  2. 17
      src/database.h

29
src/database.c

@ -66,26 +66,25 @@ int imdb_open(imdb_db_t *db, const char *path, int write)
ssize_t bytes = 0;
struct stat st;
char buf[IMDB_REC_LEN] = "\0";
int flags = 0, fd = -1;
char *p;
assert(db != NULL);
assert(path != NULL);
memset(db, 0x0, sizeof(imdb_db_t));
db->fd = -1;
errno = 0;
if (stat(path, &st) == -1) {
if (stat(path, &st) < 0) {
strncpy(db->error, strerror(errno), sizeof(db->error));
return -1;
}
errno = 0;
if ((db->fd = open(path, write ? O_RDWR : O_RDONLY)) == -1) {
if ((fd = open(path, write ? O_RDWR : O_RDONLY)) < 0) {
strncpy(db->error, strerror(errno), sizeof(db->error));
return -1;
}
db->write = write;
strncpy(db->path, path, sizeof(db->path));
DB_READ(db, buf, IMDB_REC_LEN, 0);
@ -105,7 +104,25 @@ int imdb_open(imdb_db_t *db, const char *path, int write)
strncpy(db->error, "Can't read database capabilities", sizeof(db->error));
return -1;
}
memcpy(db->caps, buf + 16, sizeof(char) * 8);
if (write)
flags |= IMDB_FLAG_WRITE;
p = buf + 16;
for (size_t i = 0; i < 8 && *p != '\0'; p++) {
switch (*p) {
case 'M' : flags |= IMDB_CAP_BITMAP; break;
case 'C' : flags |= IMDB_CAP_COLORS; break;
case 'R' : flags |= IMDB_CAP_RATIO; break;
case ';' : i = 9; /* end of flags */ break;
default: /* ignore */ break;
}
}
db->fd = fd;
db->flags = flags;
strncpy(db->path, path, sizeof(db->path));
return 0;
}

17
src/database.h

@ -4,12 +4,20 @@
#define IMDB_REC_LEN 48
#define IMDB_VERSION 2
/* runtime flags */
#define IMDB_FLAG_WRITE 1 << (0 + 0)
/* database capabilities */
#define IMDB_CAP_BITMAP 1 << (8 + 0)
#define IMDB_CAP_COLORS 1 << (8 + 1)
#define IMDB_CAP_RATIO 1 << (8 + 2)
/* 3 used, 5 reserved */
typedef struct {
int fd;
int write;
int flags;
char path[PATH_MAX];
char error[256];
char caps[8];
} imdb_db_t;
/**
@ -20,11 +28,6 @@ typedef struct {
24-48 : padding with null's
*/
#define CAP_OFF_BITMAP 0
#define CAP_OFF_COLORS 1
#define CAP_OFF_RATIO 2
/* 3 used, 5 reserved */
/**
Database record format - fixed length, 48 bytes

Loading…
Cancel
Save