-
Notifications
You must be signed in to change notification settings - Fork 2
/
entry.c
103 lines (83 loc) · 2.01 KB
/
entry.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "entry.h"
#include "string_helpers.h"
entry * entry_init( void )
{
entry * e = malloc( sizeof( entry ) );
e->entry_number = 0;
e->tag_count = 0;
e->link_count = 0;
e->title = malloc( 1 );
e->title[0] = '\0';
e->content = malloc( 1 );
e->content[0] = '\0';
return e;
}
void entry_add_link( entry * e, uint64_t n, char * l ) {
char * tmp = calloc(100, sizeof( char ) );
sprintf( tmp, "{%lu} : %s", n, l );
e->links[e->link_count++] = strdup( tmp );
free( tmp );
}
void entry_add_tags( entry * e, char * tags ) {
int index, ch_p = 0;
char * tag = calloc( 50, sizeof( char ) );
for (index = 0; index < strlen( tags ); index++ ) {
if ( tags[index] == ' ' || tags[index] == '\0' ) {
//add tag here
if (ch_p == 0) continue;
tag[ch_p] = '\0';
entry_add_tag( e, tag );
ch_p = 0;
} else {
tag[ch_p++] = tags[index];
}
}
}
void entry_set_title( entry * e, char * t )
{
remove_trailing_white( t );
free(e->title);
e->title = strdup( t );
}
void entry_set_content( entry * e, char * c )
{
remove_trailing_white( c );
free(e->content);
e->content = strdup( c );
}
void entry_add_tag( entry * e, char * c )
{
remove_trailing_white( c );
e->tags[e->tag_count] = strdup(c);
e->tag_count++;
}
void entry_free( entry * e )
{
free(e->title);
free(e->content);
int i;
if ( e->tag_count > 0 ) {
for ( i = 0; i < e->tag_count; i++ )
{
free(e->tags[i]);
}
}
if ( e->link_count > 0 ) {
for ( i = 0; i < e->link_count; i++ )
{
free( e->links[i] );
}
}
free(e);
}
uint8_t entry_has_tag( entry * e, const char * tag ) {
for ( int i = 0; i < e->tag_count; i++ ) {
if ( strcmp( e->tags[i], tag ) == 0 ) {
return 1;
}
}
return 0;
}