You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
2.1 KiB
C

#include "status.h"
#include "model/account.h"
#include "json/json.h"
#include "json/layout.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stddef.h>
void write_json_escaped( FILE* f, const char* str );
static const char* b( bool value )
{
return value ? "true" : "false";
}
static const char* host()
{
return "apogee.polaris-1.work";
}
static struct json_object_field status_layout[] = {
{ "account_id", offsetof( struct status, account_id ), true, &json_field_integer },
{ "sensitive", offsetof( struct status, sensitive ), true, &json_field_bool },
{ "content", offsetof( struct status, content ), true, &json_field_string },
{ NULL }
};
struct status* status_from_id( unsigned int id )
{
struct status* s = NULL;
char filename[512];
snprintf( filename, 512, "data/statuses/%d.json", id );
FILE* f = fopen( filename, "r" );
if( !f ) { return NULL; }
struct json_pull_parser* jpp = json_pull_parser_new( f );
if( !jpp ) { return NULL; }
s = malloc(sizeof(struct status));
s->id = id;
s->account_id = -1;
if( !json_read_object_layout( jpp, status_layout, s ) ) { goto failed; }
cleanup:
json_pull_parser_release(jpp);
fclose(f);
return s;
failed:
//status_free(s);
s = NULL;
goto cleanup;
}
bool status_save_new( struct status* s )
{
int head = -1;
FILE* f = fopen("data/statuses/HEAD","r");
fscanf(f,"%d",&head);
if( head == -1 ) { return false; }
fclose(f);
s->id = head + 1;
f = fopen("data/statuses/HEAD.tmp","w");
fprintf( f, "%d", s->id );
fclose(f);
rename( "data/statuses/HEAD.tmp", "data/statuses/HEAD" );
status_save( s );
return true;
}
void status_save( struct status* s )
{
char filename[512];
char tmp_filename[512+32];
snprintf( filename, 512, "data/statuses/%d.json", s->id );
snprintf( tmp_filename, 512+32, "%s.tmp", filename );
struct json_writer jw = {
.f = fopen( tmp_filename, "w" ),
.indent = 0,
.indentation = "\t",
};
printf( "content:\n%s\n", s->content );
json_write_pretty_object_layout( &jw, status_layout, s );
fclose(jw.f);
rename( tmp_filename, filename );
}
void status_free( struct status* s )
{
if( !s ) { return; }
free(s->content);
free(s);
}