mirror of
git://projects.qi-hardware.com/eda-tools.git
synced 2025-04-21 12:27:27 +03:00
eeshow/fmt-pango.c, fmt-pango.h: also move to gui/
This commit is contained in:
151
eeshow/gui/fmt-pango.c
Normal file
151
eeshow/gui/fmt-pango.c
Normal file
@@ -0,0 +1,151 @@
|
||||
/*
|
||||
* gui/fmt-pango.c - Format strings for Pango markup
|
||||
*
|
||||
* Written 2016 by Werner Almesberger
|
||||
* Copyright 2016 by Werner Almesberger
|
||||
*
|
||||
* This program 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.
|
||||
*/
|
||||
|
||||
|
||||
#define _GNU_SOURCE /* for asprintf */
|
||||
#include <stdarg.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <ctype.h>
|
||||
#include <alloca.h>
|
||||
|
||||
#include "util.h"
|
||||
#include "diag.h"
|
||||
#include "gui/fmt-pango.h"
|
||||
|
||||
|
||||
unsigned vsfmt_pango(char *buf, const char *fmt, va_list ap)
|
||||
{
|
||||
char *res;
|
||||
const char *p, *q, *s, *t;
|
||||
char *u;
|
||||
char *tmp_fmt;
|
||||
char *tmp, *tmp2;
|
||||
int len;
|
||||
unsigned extra;
|
||||
|
||||
res = buf;
|
||||
for (p = fmt; *p; p++) {
|
||||
if (*p != '%') {
|
||||
if (buf)
|
||||
*res = *p;
|
||||
res++;
|
||||
continue;
|
||||
}
|
||||
for (q = p + 1; isdigit(*q) || *q == '.' || *q == '-'; q++);
|
||||
tmp_fmt = alloca(q - p + 1 + 1);
|
||||
memcpy(tmp_fmt, p, q - p + 1);
|
||||
tmp_fmt[q - p + 1] = 0;
|
||||
switch (*q) {
|
||||
case 's':
|
||||
s = va_arg(ap, const char *);
|
||||
len = asprintf(&tmp, tmp_fmt, s);
|
||||
|
||||
extra = 0;
|
||||
for (t = tmp; *t; t++)
|
||||
switch (*t) {
|
||||
case '<':
|
||||
case '>':
|
||||
extra += 3;
|
||||
break;
|
||||
case '&':
|
||||
extra += 4;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
if (extra) {
|
||||
tmp2 = u = alloca(len + extra + 1);
|
||||
for (t = tmp; *t; t++) {
|
||||
switch (*t) {
|
||||
case '<':
|
||||
strcpy(u, "<");
|
||||
u += 4;
|
||||
break;
|
||||
case '>':
|
||||
strcpy(u, ">");
|
||||
u += 4;
|
||||
break;
|
||||
case '&':
|
||||
strcpy(u, "&");
|
||||
u += 5;
|
||||
break;
|
||||
default:
|
||||
*u++ = *t;
|
||||
break;
|
||||
}
|
||||
}
|
||||
*u = 0;
|
||||
tmp = tmp2;
|
||||
}
|
||||
|
||||
if (buf)
|
||||
memcpy(res, tmp, len + extra);
|
||||
res += len + extra;
|
||||
break;
|
||||
case 'c':
|
||||
/* @@@ we don't filter markup meta-characters */
|
||||
case 'd':
|
||||
case 'x':
|
||||
len = asprintf(&tmp, tmp_fmt, va_arg(ap, int));
|
||||
if (buf)
|
||||
memcpy(res, tmp, len);
|
||||
res += len;
|
||||
break;
|
||||
case 'u':
|
||||
len = asprintf(&tmp, tmp_fmt, va_arg(ap, unsigned));
|
||||
if (buf)
|
||||
memcpy(res, tmp, len);
|
||||
res += len;
|
||||
break;
|
||||
case '%':
|
||||
if (buf)
|
||||
*res = '%';
|
||||
res++;
|
||||
break;
|
||||
default:
|
||||
fatal("unrecognized format '%%%c'\n", *q);
|
||||
}
|
||||
p = q;
|
||||
}
|
||||
if (buf)
|
||||
*res = 0;
|
||||
return res - buf;
|
||||
}
|
||||
|
||||
|
||||
char *vfmt_pango(const char *fmt, va_list ap)
|
||||
{
|
||||
va_list aq;
|
||||
unsigned len;
|
||||
char *buf;
|
||||
|
||||
va_copy(aq, ap);
|
||||
len = vsfmt_pango(NULL, fmt, ap);
|
||||
buf = alloc_size(len + 1);
|
||||
vsfmt_pango(buf, fmt, aq);
|
||||
va_end(aq);
|
||||
return buf;
|
||||
}
|
||||
|
||||
|
||||
char *fmt_pango(const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
char *buf;
|
||||
|
||||
va_start(ap, fmt);
|
||||
buf = vfmt_pango(fmt, ap);
|
||||
va_end(ap);
|
||||
return buf;
|
||||
}
|
||||
25
eeshow/gui/fmt-pango.h
Normal file
25
eeshow/gui/fmt-pango.h
Normal file
@@ -0,0 +1,25 @@
|
||||
/*
|
||||
* gui/fmt-pango.h - Format strings for Pango markup
|
||||
*
|
||||
* Written 2016 by Werner Almesberger
|
||||
* Copyright 2016 by Werner Almesberger
|
||||
*
|
||||
* This program 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.
|
||||
*/
|
||||
|
||||
|
||||
#ifndef GUI_FMT_PANGO_H
|
||||
#define GUI_FMT_PANGO_H
|
||||
|
||||
#include <stdarg.h>
|
||||
|
||||
|
||||
unsigned vsfmt_pango(char *buf, const char *fmt, va_list ap);
|
||||
char *vfmt_pango(const char *fmt, va_list ap);
|
||||
char *fmt_pango(const char *fmt, ...)
|
||||
__attribute__((format(printf, 1, 2)));
|
||||
|
||||
#endif /* !GUI_FMT_PANGO_H */
|
||||
@@ -38,6 +38,7 @@
|
||||
#include "delta.h"
|
||||
#include "diff.h"
|
||||
#include "dwg.h"
|
||||
#include "gui/fmt-pango.h"
|
||||
#include "gui/aoi.h"
|
||||
#include "gui/style.h"
|
||||
#include "gui/over.h"
|
||||
@@ -489,7 +490,7 @@ static bool hover_history(void *user, bool on)
|
||||
char *s;
|
||||
|
||||
if (on) {
|
||||
s = vcs_git_long_for_pango(h->vcs_hist);
|
||||
s = vcs_git_long_for_pango(h->vcs_hist, fmt_pango);
|
||||
overlay_text_raw(h->over, s);
|
||||
free(s);
|
||||
} else {
|
||||
@@ -680,7 +681,7 @@ static bool show_history_details(void *user, bool on)
|
||||
char *s;
|
||||
|
||||
if (on) {
|
||||
s = vcs_git_long_for_pango(h->vcs_hist);
|
||||
s = vcs_git_long_for_pango(h->vcs_hist, fmt_pango);
|
||||
overlay_text_raw(h->over, s);
|
||||
free(s);
|
||||
} else {
|
||||
|
||||
@@ -31,7 +31,7 @@
|
||||
#include <pango/pangocairo.h>
|
||||
|
||||
#include "util.h"
|
||||
#include "fmt-pango.h"
|
||||
#include "gui/fmt-pango.h"
|
||||
#include "gui/aoi.h"
|
||||
#include "gui/style.h"
|
||||
#include "gui/over.h"
|
||||
|
||||
Reference in New Issue
Block a user