Annotation of mandoc/tbl_data.c, Revision 1.6
1.6 ! kristaps 1: /* $Id: tbl_data.c,v 1.5 2011/01/01 17:10:20 kristaps Exp $ */
1.1 kristaps 2: /*
3: * Copyright (c) 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
4: *
5: * Permission to use, copy, modify, and distribute this software for any
6: * purpose with or without fee is hereby granted, provided that the above
7: * copyright notice and this permission notice appear in all copies.
8: *
9: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16: */
17: #include <assert.h>
18: #include <ctype.h>
19: #include <stdlib.h>
20: #include <string.h>
1.6 ! kristaps 21: #include <time.h>
1.1 kristaps 22:
23: #include "mandoc.h"
24: #include "libmandoc.h"
25: #include "libroff.h"
26:
27: static void data(struct tbl *, struct tbl_span *,
28: int, const char *, int *);
29:
30: void
31: data(struct tbl *tbl, struct tbl_span *dp,
32: int ln, const char *p, int *pos)
33: {
34: struct tbl_dat *dat;
1.3 kristaps 35: struct tbl_cell *cp;
1.1 kristaps 36: int sv;
37:
1.3 kristaps 38: cp = NULL;
39: if (dp->last && dp->last->layout)
40: cp = dp->last->layout->next;
41: else if (NULL == dp->last)
42: cp = dp->layout->first;
43:
44: /* Skip over spanners to data formats. */
45:
46: while (cp && (TBL_CELL_VERT == cp->pos ||
47: TBL_CELL_DVERT == cp->pos))
48: cp = cp->next;
49:
1.1 kristaps 50: /* FIXME: warn about losing data contents if cell is HORIZ. */
51:
52: dat = mandoc_calloc(1, sizeof(struct tbl_dat));
1.3 kristaps 53: dat->layout = cp;
1.1 kristaps 54:
55: if (dp->last) {
56: dp->last->next = dat;
57: dp->last = dat;
58: } else
59: dp->last = dp->first = dat;
60:
61: sv = *pos;
62: while (p[*pos] && p[*pos] != tbl->tab)
63: (*pos)++;
64:
65: dat->string = mandoc_malloc(*pos - sv + 1);
66: memcpy(dat->string, &p[sv], *pos - sv);
67: dat->string[*pos - sv] = '\0';
68:
69: if (p[*pos])
70: (*pos)++;
71:
72: if ( ! strcmp(dat->string, "_"))
1.5 kristaps 73: dat->pos = TBL_DATA_HORIZ;
1.1 kristaps 74: else if ( ! strcmp(dat->string, "="))
1.5 kristaps 75: dat->pos = TBL_DATA_DHORIZ;
1.1 kristaps 76: else if ( ! strcmp(dat->string, "\\_"))
1.5 kristaps 77: dat->pos = TBL_DATA_NHORIZ;
1.1 kristaps 78: else if ( ! strcmp(dat->string, "\\="))
1.5 kristaps 79: dat->pos = TBL_DATA_NDHORIZ;
80: else
81: dat->pos = TBL_DATA_DATA;
1.1 kristaps 82: }
83:
1.2 kristaps 84: int
1.1 kristaps 85: tbl_data(struct tbl *tbl, int ln, const char *p)
86: {
87: struct tbl_span *dp;
1.3 kristaps 88: struct tbl_row *rp;
1.1 kristaps 89: int pos;
90:
91: pos = 0;
92:
93: if ('\0' == p[pos]) {
94: TBL_MSG(tbl, MANDOCERR_TBL, ln, pos);
1.4 kristaps 95: return(0);
1.1 kristaps 96: }
97:
1.3 kristaps 98: /*
99: * Choose a layout row: take the one following the last parsed
100: * span's. If that doesn't exist, use the last parsed span's.
101: * If there's no last parsed span, use the first row. This can
102: * be NULL!
103: */
104:
105: if (tbl->last_span) {
106: assert(tbl->last_span->layout);
107: rp = tbl->last_span->layout->next;
108: if (NULL == rp)
109: rp = tbl->last_span->layout;
110: } else
111: rp = tbl->first_row;
112:
1.1 kristaps 113: dp = mandoc_calloc(1, sizeof(struct tbl_span));
1.3 kristaps 114: dp->layout = rp;
1.1 kristaps 115:
1.2 kristaps 116: if (tbl->last_span) {
117: tbl->last_span->next = dp;
118: tbl->last_span = dp;
119: } else
120: tbl->last_span = tbl->first_span = dp;
121:
1.1 kristaps 122: if ( ! strcmp(p, "_")) {
1.5 kristaps 123: dp->pos = TBL_SPAN_HORIZ;
1.2 kristaps 124: return(1);
1.1 kristaps 125: } else if ( ! strcmp(p, "=")) {
1.5 kristaps 126: dp->pos = TBL_SPAN_DHORIZ;
1.2 kristaps 127: return(1);
1.1 kristaps 128: }
1.5 kristaps 129:
130: dp->pos = TBL_SPAN_DATA;
1.1 kristaps 131:
132: while ('\0' != p[pos])
133: data(tbl, dp, ln, p, &pos);
134:
1.2 kristaps 135: return(1);
1.1 kristaps 136: }
CVSweb