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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
|
/** @file
* @brief HID report descriptor - source code sink
*
* Copyright (C) 2010 Nikolai Kondrashov
*
* This file is part of hidrd.
*
* Hidrd is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Hidrd is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with hidrd; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* @author Nikolai Kondrashov <spbnick@gmail.com>
*
* @(#) $Id: snk.c 439 2010-05-24 13:59:30Z spb_nick $
*/
#include "hidrd/cfg.h"
#include "hidrd/util/buf.h"
#include "hidrd/fmt/code/snk.h"
static bool
hidrd_code_snk_init(hidrd_snk *snk, char **perr,
size_t tabstop, bool comments, bool comments_comments)
{
hidrd_code_snk_inst *code_snk = (hidrd_code_snk_inst *)snk;
if (!hidrd_spec_snk_init(snk, perr, tabstop, false, comments_comments))
return false;
code_snk->comments = comments;
return true;
}
static bool
hidrd_code_snk_initv(hidrd_snk *snk, char **perr, va_list ap)
{
size_t tabstop = va_arg(ap, size_t);
bool comments = (va_arg(ap, int) != 0);
bool comments_comments = (va_arg(ap, int) != 0);
return hidrd_code_snk_init(snk, perr,
tabstop, comments, comments_comments);
}
#ifdef HIDRD_WITH_OPT
static const hidrd_opt_spec hidrd_code_snk_opts_spec[] = {
{.name = "tabstop",
.type = HIDRD_OPT_TYPE_U32,
.req = false,
.dflt = {.u32 = 4},
.desc = "number of spaces per tab"},
{.name = "comments",
.type = HIDRD_OPT_TYPE_BOOLEAN,
.req = false,
.dflt = {.boolean = true},
.desc = "enable comments in specification example format"},
{.name = "comments_comments",
.type = HIDRD_OPT_TYPE_BOOLEAN,
.req = false,
.dflt = {.boolean = false},
.desc = "enable comments in specification example format comments"},
{.name = NULL}
};
static bool
hidrd_code_snk_init_opts(hidrd_snk *snk, char **perr, const hidrd_opt *list)
{
return hidrd_code_snk_init(
snk, perr,
hidrd_opt_list_get_u32(list, "tabstop"),
hidrd_opt_list_get_boolean(list, "comments"),
hidrd_opt_list_get_boolean(list, "comments_comments"));
}
#endif /* HIDRD_WITH_OPT */
static bool
hidrd_code_snk_valid(const hidrd_snk *snk)
{
return (snk->type->size >= sizeof(hidrd_code_snk_inst)) &&
hidrd_spec_snk_valid(snk);
}
static bool
hidrd_code_snk_flush(hidrd_snk *snk)
{
bool result = false;
hidrd_code_snk_inst *code_snk = (hidrd_code_snk_inst *)
snk;
hidrd_spec_snk_inst *spec_snk = &code_snk->spec_snk;
const hidrd_spec_snk_ent_list *list = &spec_snk->list;
hidrd_spec_snk_ent *p;
size_t last_l = 0;
size_t l;
hidrd_ttbl *tbl = NULL;
hidrd_buf buf = HIDRD_BUF_EMPTY;
uint8_t *item_p;
size_t item_size;
if (snk->pbuf != NULL)
{
free(*snk->pbuf);
*snk->pbuf = NULL;
}
if (snk->psize != NULL)
*snk->psize = 0;
/* Find the last item */
for (p = list->ptr, l = 0; l < list->len; p++, l++)
if (p->name != NULL)
last_l = l;
/* If the comments are not requested */
if (!code_snk->comments)
{
tbl = hidrd_ttbl_new();
if (tbl == NULL)
goto cleanup;
}
else
{
if (!hidrd_spec_snk_ent_list_to_tbl(&tbl, list,
spec_snk->tabstop,
spec_snk->dumps,
spec_snk->comments))
goto cleanup;
/* Add comment end string column right after the spec comments */
for (l = 0; l < list->len; l++)
hidrd_ttbl_set(tbl, HIDRD_SPEC_SNK_ENT_LIST_CMNT_COL + 1, l,
"*/");
/* Insert comment start column before the leftmost column */
hidrd_ttbl_ins_cols(tbl, 0, 1);
for (l = 0; l < list->len; l++)
hidrd_ttbl_set(tbl, 0, l, "/*");
/* Make room for our output */
hidrd_ttbl_ins_cols(tbl, 0, 1);
}
/* Output the code */
for (p = list->ptr, l = 0; l < list->len; p++, l++)
{
if (p->item == NULL)
continue;
for (item_size = hidrd_item_get_size(p->item), item_p = p->item;
item_size > 0; item_size--, item_p++)
if (!hidrd_buf_add_printf(&buf,
(item_size == 1)
? ((l == last_l)
? "0x%.2hhX"
: "0x%.2hhX,")
: "0x%.2hhX, ",
*item_p))
goto cleanup;
if (!hidrd_buf_add_span(&buf, '\0', 1))
goto cleanup;
hidrd_ttbl_set(tbl, 0, l, (const char *)buf.ptr);
hidrd_buf_reset(&buf);
}
/* Render the table */
result = hidrd_ttbl_render((char **)snk->pbuf, snk->psize, tbl,
spec_snk->tabstop);
cleanup:
hidrd_buf_clnp(&buf);
hidrd_ttbl_delete(tbl);
spec_snk->err = result ? HIDRD_SPEC_SNK_ERR_NONE
: HIDRD_SPEC_SNK_ERR_ALLOC;
return result;
}
const hidrd_snk_type hidrd_code_snk = {
.size = sizeof(hidrd_code_snk_inst),
.initv = hidrd_code_snk_initv,
#ifdef HIDRD_WITH_OPT
.init_opts = hidrd_code_snk_init_opts,
.opts_spec = hidrd_code_snk_opts_spec,
#endif
.valid = hidrd_code_snk_valid,
.errmsg = hidrd_spec_snk_errmsg,
.put = hidrd_spec_snk_put,
.flush = hidrd_code_snk_flush,
.clnp = hidrd_spec_snk_clnp,
};
|