#include "outbox_envelope.h" #include "json/layout.h" #include "ffdb/fs_list.h" #include #include #include #include static struct json_object_field layout[] = { { "account", offsetof( struct outbox_envelope, account_id ), true, &json_field_integer }, { "activity", offsetof( struct outbox_envelope, activity_id ), true, &json_field_integer }, { NULL }, }; struct outbox_envelope* outbox_envelope_new() { struct outbox_envelope* env; env = malloc(sizeof(*env)); if( !env ) { return NULL; } memset(env,0,sizeof(*env)); } void outbox_envelope_free( struct outbox_envelope* env ) { free(env); } void outbox_envelope_save( struct outbox_envelope* env ) { int id = fs_list_get( "data/outbox/HEAD" ) + 1; fs_list_set( "data/outbox/HEAD", id ); char filename[512]; snprintf( filename, sizeof(filename), "data/outbox/%d.json", id ); json_write_object_layout_to_file( filename, "\t", layout, env ); } struct outbox_envelope* outbox_envelope_load_next() { struct outbox_envelope* env; if( !( env = outbox_envelope_new()) ) { return NULL; } int id = fs_list_get( "data/outbox/TAIL" ) + 1; env->id = id; char filename[512]; snprintf( filename, sizeof(filename), "data/outbox/%d.json", id ); if( !json_read_object_layout_from_file( filename, layout, env ) ) { outbox_envelope_free( env ); return NULL; } return env; } void outbox_envelope_delete( struct outbox_envelope* env ) { char filename[512]; snprintf( filename, sizeof(filename), "data/outbox/%d.json", env->id ); remove(filename); outbox_envelope_free(env); }