comparison helpgen/topic.c @ 0:13d2b8934445

Import AnaGram (near-)release tree into Mercurial.
author David A. Holland
date Sat, 22 Dec 2007 17:52:45 -0500
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:13d2b8934445
1 #include <assert.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5
6 #include "utils.h"
7 #include "topic.h"
8
9 #define MAXTITLES 16
10 #define MAXREFS 64
11
12 struct topic {
13 char *titles[MAXTITLES];
14 int numtitles;
15 char *refs[MAXREFS];
16 int numrefs;
17 char *body;
18 };
19
20 struct topic *topic_create(void) {
21 struct topic *t = domalloc(sizeof(struct topic));
22 t->numtitles = 0;
23 t->numrefs = 0;
24 t->body = NULL;
25 return t;
26 }
27
28 void topic_destroy(struct topic *t) {
29 int i;
30 for (i=0; i<t->numtitles; i++) {
31 free(t->titles[i]);
32 }
33 for (i=0; i<t->numrefs; i++) {
34 free(t->refs[i]);
35 }
36 if (t->body) {
37 free(t->body);
38 }
39 free(t);
40 }
41
42 void topic_addtitle(struct topic *t, const char *s) {
43 if (t->numtitles >= MAXTITLES) {
44 fprintf(stderr, "Too many titles (increase MAXTITLES) at %s\n", s);
45 exit(1);
46 }
47 t->titles[t->numtitles++] = dostrdup(s);
48 }
49
50 void topic_addref(struct topic *t, const char *s) {
51 if (t->numrefs >= MAXREFS) {
52 fprintf(stderr, "Too many refs (increase MAXREFS) at %s\n", s);
53 exit(1);
54 }
55 t->refs[t->numrefs++] = dostrdup(s);
56 }
57
58 void topic_setbody(struct topic *t, const char *buf, size_t len) {
59 size_t i, j, reallen = 0;
60
61 for (i=0; i<len; i++) {
62 if (buf[i] != '\r') {
63 reallen++;
64 }
65 }
66
67 char *text = domalloc(reallen+1);
68 for (i=j=0; i<len; i++) {
69 if (buf[i] != '\r') {
70 text[j++] = buf[i];
71 }
72 }
73 assert(j==reallen);
74 text[reallen] = 0;
75
76 t->body = text;
77 }
78
79 const char *topic_getbody(struct topic *t) { return t->body; }
80 int topic_getnumtitles(struct topic *t) { return t->numtitles; }
81 const char *topic_gettitle(struct topic *t, int ix) { return t->titles[ix]; }
82 int topic_getnumrefs(struct topic *t) { return t->numrefs; }
83 const char *topic_getref(struct topic *t, int ix) { return t->refs[ix]; }