jcs
/subtext
/amendments
/52
db: Adapt to new bile API
jcs made amendment 52 over 2 years ago
--- db.c Wed Jan 5 20:46:27 2022
+++ db.c Sat Jan 15 20:27:15 2022
@@ -24,13 +24,14 @@
#include "user.h"
#include "util.h"
-struct db * db_init(char *path, struct bile *bile);
+struct db * db_init(Str255 file, short vrefnum, struct bile *bile);
short db_migrate(struct db *tdb, short is_new);
void db_config_load(struct db *tdb);
struct db *
-db_open(char *file)
+db_open(Str255 file, short vrefnum)
{
+ Str255 filepath;
struct stat sb;
Point pt = { 75, 100 };
SFReply reply = { 0 };
@@ -38,35 +39,30 @@ db_open(char *file)
StringHandle lastfileh;
struct db *ret;
- if (file) {
- if (stat((const char *)file, &sb) == 0)
- return db_init(file, NULL);
+ if (file[0]) {
+ getpath(vrefnum, file, &filepath, true);
+ if (FStat(filepath, &sb) == 0)
+ return db_init(file, vrefnum, NULL);
- warn("Failed to open %s", file);
+ warn("Failed to open %s", PtoCstr(file));
}
if ((lastfileh = GetString(STR_LAST_DB))) {
HLock(lastfileh);
- file = xmalloc(GetHandleSize(lastfileh));
- memcpy(file, *lastfileh, *lastfileh[0] + 1);
+ memcpy(filepath, *lastfileh, sizeof(filepath));
HUnlock(lastfileh);
ReleaseResource(lastfileh);
- PtoCstr(file);
- if (stat(file, &sb) == 0) {
- ret = db_init(file, NULL);
- free(file);
+ if (FStat(filepath, &sb) == 0) {
+ ret = db_init(filepath, 0, NULL);
return ret;
}
- free(file);
}
/* no file passed, no last file, prompt */
types[0] = DB_TYPE;
SFGetFile(pt, NULL, NULL, 1, &types, NULL, &reply);
if (reply.good) {
- getpath(reply.vRefNum, reply.fName, &file, 1);
- ret = db_init(file, NULL);
- free(file);
+ ret = db_init(reply.fName, reply.vRefNum, NULL);
return ret;
}
@@ -78,65 +74,56 @@ db_create(void)
{
Point pt = { 75, 100 };
SFReply reply;
- struct db *ret;
struct bile *bile;
- char *path;
- Str255 ppath;
- short error, fh, tfh, i;
+ short error;
SFPutFile(pt, "\pCreate new Subtext DB:", "\p", NULL, &reply);
if (!reply.good)
return NULL;
-
- getpath(reply.vRefNum, reply.fName, &path, true);
- memcpy(ppath, path, sizeof(ppath));
- CtoPstr(ppath);
- bile = bile_create(ppath, SUBTEXT_CREATOR, DB_TYPE);
- if (bile == NULL && bile_error() == dupFNErr) {
+ bile = bile_create(reply.fName, reply.vRefNum, SUBTEXT_CREATOR,
+ DB_TYPE);
+ if (bile == NULL && bile_error(NULL) == dupFNErr) {
error = FSDelete(reply.fName, reply.vRefNum);
if (error)
panic("Failed to re-create file %s: %d", PtoCstr(reply.fName),
error);
- bile = bile_create(ppath, SUBTEXT_CREATOR, DB_TYPE);
+ bile = bile_create(reply.fName, reply.vRefNum, SUBTEXT_CREATOR,
+ DB_TYPE);
}
if (bile == NULL)
panic("Failed to create file %s: %d", PtoCstr(reply.fName), error);
- ret = db_init(path, bile);
- free(path);
-
- return ret;
+ return db_init(reply.fName, reply.vRefNum, bile);
}
struct db *
-db_init(char *path, struct bile *bile)
+db_init(Str255 path, short vrefnum, struct bile *bile)
{
+ Str255 fullpath;
struct db *tdb;
Handle resh, lastfileh;
short was_new, error, i;
- Str255 ppath;
- memcpy(ppath, path, sizeof(ppath));
- CtoPstr(ppath);
-
was_new = (bile != NULL);
if (bile == NULL) {
- bile = bile_open(ppath);
+ bile = bile_open(path, vrefnum);
if (bile == NULL)
- panic("Failed to open DB file %s: %d", path, bile_error());
+ panic("Failed to open DB file %s: %d", PtoCstr(path),
+ bile_error(NULL));
}
/* we got this far, store it as the last-accessed file */
+ getpath(vrefnum, path, &fullpath, true);
lastfileh = Get1Resource('STR ', STR_LAST_DB);
if (lastfileh)
- xSetHandleSize(lastfileh, ppath[0] + 1);
+ xSetHandleSize(lastfileh, fullpath[0] + 1);
else
- lastfileh = xNewHandle(ppath[0] + 1);
+ lastfileh = xNewHandle(fullpath[0] + 1);
HLock(lastfileh);
- memcpy(*lastfileh, ppath, ppath[0] + 1);
+ memcpy(*lastfileh, fullpath, fullpath[0] + 1);
HUnlock(lastfileh);
if (HomeResFile(lastfileh) == -1)
AddResource(lastfileh, 'STR ', STR_LAST_DB, "\pSTR_LAST_DB");
@@ -213,7 +200,7 @@ db_migrate(struct db *tdb, short is_new)
/* store new version */
ver = DB_CUR_VERS;
if (bile_write(tdb->bile, DB_VERS_RTYPE, 1, &ver, 1) != 1)
- panic("Failed writing new version: %d", bile_error());
+ panic("Failed writing new version: %d", bile_error(tdb->bile));
return 0;
}
@@ -223,7 +210,8 @@ db_config_save(struct db *tdb)
{
if (bile_write(tdb->bile, DB_CONFIG_RTYPE, 1, &tdb->config,
sizeof(tdb->config)) != sizeof(tdb->config))
- panic("db_config_save: failed to write: %d", bile_error());
+ panic("db_config_save: failed to write: %d",
+ bile_error(tdb->bile));
}
void
@@ -233,8 +221,9 @@ db_config_load(struct db *tdb)
char *newconfig;
rlen = bile_read_alloc(tdb->bile, DB_CONFIG_RTYPE, 1, &newconfig);
- if (rlen == 0 || bile_error())
- panic("db_config_load: error reading config: %d", bile_error());
+ if (rlen == 0 || bile_error(tdb->bile))
+ panic("db_config_load: error reading config: %d",
+ bile_error(tdb->bile));
if (rlen != sizeof(tdb->config))
panic("db_config_load: read config of size %lu, but expected %lu",
rlen, sizeof(tdb->config));
--- db.h Wed Jan 5 17:17:56 2022
+++ db.h Fri Jan 14 17:42:11 2022
@@ -78,7 +78,7 @@ struct db {
short nusers;
};
-struct db * db_open(char *file);
+struct db * db_open(Str255 file, short vrefnum);
struct db * db_create(void);
void db_close(struct db *tdb);
void db_config_save(struct db *tdb);
--- main.c Wed Jan 5 13:07:33 2022
+++ main.c Sat Jan 15 20:18:37 2022
@@ -44,7 +44,6 @@ main(void)
GrafPtr old_port;
AppFile finder_file;
short event_in, n, finder_action, finder_count;
- char *dbpath;
char key, iters;
uthread_init();
@@ -58,14 +57,15 @@ main(void)
InitDialogs(0);
InitCursor();
MaxApplZone();
-
- err_init();
- mbar = GetNewMBar(MBAR_ID);
+ if (!(mbar = GetNewMBar(MBAR_ID)))
+ panic("no mbar");
SetMenuBar(mbar);
- apple_menu = GetMHandle(APPLE_MENU_ID);
+ if (!(apple_menu = GetMHandle(APPLE_MENU_ID)))
+ panic("no apple menu");
AddResMenu(apple_menu, 'DRVR');
- file_menu = GetMHandle(FILE_MENU_ID);
+ if (!(file_menu = GetMHandle(FILE_MENU_ID)))
+ panic("no file menu");
update_menu();
DrawMenuBar();
@@ -75,12 +75,12 @@ main(void)
GetAppFiles(1, &finder_file);
ClrAppFiles(1);
finder_count = 0;
- getpath(finder_file.vRefNum, finder_file.fName, &dbpath, 1);
- db = db_open(dbpath);
- free(dbpath);
- } else
- db = db_open(NULL);
-
+ db = db_open(finder_file.fName, finder_file.vRefNum);
+ } else {
+ char none = 0;
+ db = db_open(*((Str255 *)&none), 0);
+ }
+
if (!db)
panic("Failed to open database");
--- user.c Wed Jan 5 20:54:21 2022
+++ user.c Fri Jan 14 20:43:31 2022
@@ -38,11 +38,7 @@ user_update_cache_map(struct db *tdb)
tdb->user_map = xmalloczero(sizeof(struct user_map) * tdb->nusers);
nuser = 0;
- for (n = 0; n < tdb->bile->nobjects; n++) {
- o = &tdb->bile->map[n];
- if (o->type != DB_USER_RTYPE)
- continue;
-
+ while ((o = bile_get_nth_of_type(tdb->bile, nuser, DB_USER_RTYPE))) {
muser = &tdb->user_map[nuser];
muser->id = o->id;
@@ -51,8 +47,9 @@ user_update_cache_map(struct db *tdb)
muser->username_key, sizeof(muser->username_key));
if (len != sizeof(muser->username_key))
panic("user_update_cache_map: can't read user %lu: %d", o->id,
- bile_error());
-
+ bile_error(tdb->bile));
+
+ free(o);
nuser++;
}
}
@@ -110,7 +107,7 @@ user_save(struct db *tdb, struct user *user)
len = bile_write(tdb->bile, DB_USER_RTYPE, user->id,
user, sizeof(struct user));
if (len != sizeof(struct user))
- panic("user_save: failed to write: %d", bile_error());
+ panic("user_save: failed to write: %d", bile_error(tdb->bile));
user_update_cache_map(tdb);
}