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.

66 lines
1.3 KiB
C

#include "emoji.h"
// Submodules
#include "json/layout.h"
#include "util/format.h"
// Model
#include "model/server.h"
// Standard Library
#include <dirent.h>
#include <string.h>
#include <stdlib.h>
#include <libgen.h>
#define OBJ_TYPE struct emoji
struct json_object_field emoji_layout[] = {
JSON_FIELD_STRING( url, true ),
JSON_FIELD_STRING( shortcode, true ),
JSON_FIELD_END,
};
#undef OBJ_TYPE
JSON_FIELD_TYPE_OBJECT_LAYOUT_WITH_DEFAULTS( emoji );
char* filename_for_shortcode( const char* shortcode )
{
int len = strlen(shortcode);
DIR* d = opendir( "data/emoji" );
struct dirent* ent;
while(( ent = readdir(d) )) {
if( 0 == strncmp( shortcode, ent->d_name, len ) && ent->d_name[len] == '.' ) {
char* res = strdup(ent->d_name);
closedir(d);
return res;
}
}
closedir(d);
return NULL;
}
struct emoji* emoji_from_shortcode( const char* shortcode )
{
// check for existence of emoji
char* filename = filename_for_shortcode( shortcode );
if( !filename ) { return NULL; }
struct emoji* e;
e = malloc(sizeof(*e));
memset(e,0,sizeof(*e));
e->shortcode = strdup(shortcode);
e->url = aformat( "https://%s/emoji/%s/%s", g_server->domain, shortcode, basename(filename) );
free(filename);
return e;
}
void emoji_free( struct emoji* e )
{
if( !e ) { return; }
free(e->shortcode);
free(e->url);
free(e);
}