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.

60 lines
1.2 KiB
C

#include "emoji.h"
#include "json/layout.h"
#include "format.h"
#include "model/server.h"
#include <dirent.h>
#include <string.h>
#include <stdlib.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;
char* filename = NULL;
while( ent = readdir(d) ) {
if( 0 == strncmp( shortcode, ent->d_name, len ) && ent->d_name[len] == '.' ) {
closedir(d);
return strdup(ent->d_name);
}
}
closedir(d);
return NULL;
}
struct emoji* emoji_from_shortcode( const char* shortcode )
{
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", g_server_name, shortcode );
return e;
}
void emoji_free( struct emoji* e )
{
if( !e ) { return; }
free(e->shortcode);
free(e->url);
free(e);
}