Use gperf to tokenize some strings.
This makes the code way more readable, and also avoids a lot of strcmps. Use it for draw_align_get_from_str as a proof of concept. Signed-off-by: Pierre Habouzit <madcoder@debian.org>
This commit is contained in:
parent
ec71a73072
commit
c2df807ede
|
@ -27,3 +27,4 @@ widgetgen.h
|
|||
layoutgen.h
|
||||
awesome-version-internal.h
|
||||
apidocgen.txt
|
||||
common/tokenize.[hc]
|
||||
|
|
|
@ -26,6 +26,24 @@ SET(AWE_DOC_FILES
|
|||
${CMAKE_CURRENT_SOURCE_DIR}/README
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/LICENSE)
|
||||
|
||||
ADD_CUSTOM_COMMAND(
|
||||
COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/build-utils/gperf.sh
|
||||
ARGS ${CMAKE_CURRENT_SOURCE_DIR}/common/tokenize.gperf
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/common/tokenize.h
|
||||
OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/common/tokenize.h
|
||||
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/common/tokenize.gperf
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/build-utils/gperf.sh
|
||||
COMMENT "Generating common/tokenize.h"
|
||||
)
|
||||
ADD_CUSTOM_COMMAND(
|
||||
COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/build-utils/gperf.sh
|
||||
ARGS ${CMAKE_CURRENT_SOURCE_DIR}/common/tokenize.gperf
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/common/tokenize.c
|
||||
OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/common/tokenize.c
|
||||
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/common/tokenize.gperf
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/build-utils/gperf.sh
|
||||
COMMENT "Generating common/tokenize.c"
|
||||
)
|
||||
SET(AWE_SRCS
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/awesome.c
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/client.c
|
||||
|
@ -54,6 +72,8 @@ SET(AWE_SRCS
|
|||
${CMAKE_CURRENT_SOURCE_DIR}/common/markup.c
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/common/socket.c
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/common/swindow.c
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/common/tokenize.c
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/common/tokenize.h
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/common/util.c
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/common/version.c
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/common/xembed.c
|
||||
|
|
1
README
1
README
|
@ -13,6 +13,7 @@ In order to build awesome itself, you need header files and libs of:
|
|||
- glib
|
||||
- GdkPixBuf or Imlib2 (use --with-imlib2 with ./configure)
|
||||
- dbus (optional, use --with-dbus=no with ./configure to disable)
|
||||
- gperf
|
||||
|
||||
In order to build the awesome man pages, you need these tools:
|
||||
- asciidoc (recent version)
|
||||
|
|
|
@ -29,6 +29,7 @@ FIND_PROGRAM(CAT_EXECUTABLE cat)
|
|||
FIND_PROGRAM(GREP_EXECUTABLE grep)
|
||||
FIND_PROGRAM(GIT_EXECUTABLE git)
|
||||
FIND_PROGRAM(LUA_EXECUTABLE lua)
|
||||
FIND_PROGRAM(GPERF_EXECUTABLE gperf)
|
||||
# programs needed for man pages
|
||||
FIND_PROGRAM(ASCIIDOC_EXECUTABLE asciidoc)
|
||||
FIND_PROGRAM(XMLTO_EXECUTABLE xmlto)
|
||||
|
|
|
@ -0,0 +1,129 @@
|
|||
#! /bin/sh -e
|
||||
#
|
||||
# Copyright © 2008 Pierre Habouzit <madcoder@debian.org>
|
||||
#
|
||||
# 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.
|
||||
#
|
||||
# This program 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 this program; if not, write to the Free Software Foundation, Inc.,
|
||||
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
#
|
||||
|
||||
|
||||
die() {
|
||||
echo "$@" 1>&2
|
||||
exit 2
|
||||
}
|
||||
|
||||
do_hdr() {
|
||||
cat <<EOF
|
||||
/* This file is autogenerated by $(basename $0) */
|
||||
|
||||
EOF
|
||||
}
|
||||
|
||||
out=
|
||||
type_t=
|
||||
|
||||
while true; do
|
||||
test $# -gt 2 || break
|
||||
case "$1" in
|
||||
-o) shift; out="$1"; shift;;
|
||||
-t) shift; type_t="$1"; shift;;
|
||||
*) break;;
|
||||
esac
|
||||
done
|
||||
|
||||
do_h() {
|
||||
cat <<EOF
|
||||
`do_hdr`
|
||||
#ifndef AWESOME_TOKENIZE_H
|
||||
#define AWESOME_TOKENIZE_H
|
||||
|
||||
typedef enum awesome_token_t {
|
||||
A_TK_UNKNOWN,
|
||||
`tr 'a-z-./ ' 'A-Z____' | sed -e "s/^[^/].*/ A_TK_&,/"`
|
||||
} awesome_token_t;
|
||||
|
||||
__attribute__((pure)) enum awesome_token_t a_tokenize(const char *s, int len);
|
||||
#endif
|
||||
EOF
|
||||
}
|
||||
|
||||
do_tokens() {
|
||||
while read tok; do
|
||||
case "$tok" in
|
||||
"") continue;;
|
||||
*)
|
||||
echo "$tok, A_TK_`echo $tok | tr 'a-z-./ ' 'A-Z____'`"
|
||||
;;
|
||||
esac
|
||||
done
|
||||
}
|
||||
|
||||
do_c() {
|
||||
if ! which gperf > /dev/null; then
|
||||
echo "gperf not found. You need to install gperf." > /dev/stderr;
|
||||
exit 1;
|
||||
fi;
|
||||
|
||||
cat <<EOF | gperf --ignore-case -m16 -l -t -C -F",0" \
|
||||
--language=ANSI-C -Na_tokenize_aux \
|
||||
| sed -e '/__gnu_inline__/d;s/\<\(__\|\)inline\>//g'
|
||||
%{
|
||||
`do_hdr`
|
||||
|
||||
#include <string.h>
|
||||
#include "common/tokenize.h"
|
||||
|
||||
static const struct tok *a_tokenize_aux(const char *str, unsigned int len);
|
||||
|
||||
%}
|
||||
struct tok { const char *name; int val; };
|
||||
%%
|
||||
`do_tokens`
|
||||
%%
|
||||
|
||||
awesome_token_t a_tokenize(const char *s, int len)
|
||||
{
|
||||
if (len < 0)
|
||||
len = (int)strlen(s);
|
||||
|
||||
if (len) {
|
||||
const struct tok *res = a_tokenize_aux(s, len);
|
||||
return res ? res->val : A_TK_UNKNOWN;
|
||||
} else {
|
||||
return A_TK_UNKNOWN;
|
||||
}
|
||||
}
|
||||
EOF
|
||||
}
|
||||
|
||||
extract_tokens() {
|
||||
grep '^### ' "$1" | cut -d ' ' -f 2
|
||||
}
|
||||
|
||||
|
||||
TOKENS_FILE="$1"
|
||||
TARGET="$2"
|
||||
|
||||
trap "rm -f ${TARGET}" 0
|
||||
|
||||
rm -f "${TARGET}"
|
||||
case "${TARGET}" in
|
||||
*.h) cat "${TOKENS_FILE}" | do_h > "${TARGET}";;
|
||||
*.c) cat "${TOKENS_FILE}" | do_c > "${TARGET}";;
|
||||
*) die "you must ask for the 'h' or 'c' generation";;
|
||||
esac
|
||||
chmod -w "${TARGET}"
|
||||
|
||||
trap - 0
|
||||
exit 0
|
|
@ -42,6 +42,7 @@
|
|||
#include <ctype.h>
|
||||
#include <math.h>
|
||||
|
||||
#include "common/tokenize.h"
|
||||
#include "common/draw.h"
|
||||
#include "common/markup.h"
|
||||
#include "common/xutil.h"
|
||||
|
@ -1042,16 +1043,13 @@ draw_text_extents(xcb_connection_t *conn, int phys_screen, font_t *font, const c
|
|||
alignment_t
|
||||
draw_align_get_from_str(const char *align)
|
||||
{
|
||||
if(!a_strcmp(align, "left"))
|
||||
return AlignLeft;
|
||||
else if(!a_strcmp(align, "center"))
|
||||
return AlignCenter;
|
||||
else if(!a_strcmp(align, "right"))
|
||||
return AlignRight;
|
||||
else if(!a_strcmp(align, "flex"))
|
||||
return AlignFlex;
|
||||
|
||||
return AlignAuto;
|
||||
switch (a_tokenize(align, -1)) {
|
||||
case A_TK_LEFT: return AlignLeft;
|
||||
case A_TK_CENTER: return AlignCenter;
|
||||
case A_TK_RIGHT: return AlignRight;
|
||||
case A_TK_FLEX: return AlignFlex;
|
||||
default: return AlignAuto;
|
||||
}
|
||||
}
|
||||
|
||||
#define RGB_COLOR_8_TO_16(i) (65535 * ((i) & 0xff) / 255)
|
||||
|
|
|
@ -0,0 +1,4 @@
|
|||
center
|
||||
flex
|
||||
left
|
||||
right
|
Loading…
Reference in New Issue