浏览代码

* Add getopt and getopt_long support to genethumb.

git-svn-id: file:///srv/caca.zoy.org/var/lib/svn/libpipi/trunk@2258 92316355-f0b4-4df1-b90c-862c8a59935f
remotes/tiles
sam 16 年前
父节点
当前提交
d7c0fa358c
共有 5 个文件被更改,包括 242 次插入10 次删除
  1. +13
    -1
      configure.ac
  2. +6
    -2
      genethumb/Makefile.am
  3. +63
    -7
      genethumb/genethumb.c
  4. +129
    -0
      genethumb/mygetopt.c
  5. +31
    -0
      genethumb/mygetopt.h

+ 13
- 1
configure.ac 查看文件

@@ -25,7 +25,19 @@ AC_LIBTOOL_CXX
AC_C_CONST
AC_C_INLINE

AC_CHECK_HEADERS(stdio.h stdarg.h inttypes.h endian.h stdint.h)
AC_CHECK_HEADERS(stdio.h stdarg.h inttypes.h endian.h stdint.h getopt.h)

ac_cv_have_getopt_long="no"
AC_CHECK_FUNCS(getopt_long,
[ac_cv_have_getopt_long="yes"],
[AC_CHECK_LIB(gnugetopt, getopt_long,
[ac_cv_have_getopt_long="yes"
GETOPT_LIBS="${GETOPT_LIBS} -lgnugetopt"])])
if test "$ac_cv_have_getopt_long" != "no"; then
AC_DEFINE(HAVE_GETOPT_LONG, 1, Define to 1 if you have the `getopt_long' function.)
fi
AM_CONDITIONAL(NEED_GETOPT_LONG, test "$ac_cv_have_getopt_long" = "no")
AC_SUBST(GETOPT_LIBS)

# Optimizations
CFLAGS="${CFLAGS} -g -O2 -fno-strength-reduce -fomit-frame-pointer"


+ 6
- 2
genethumb/Makefile.am 查看文件

@@ -2,8 +2,12 @@

AM_CPPFLAGS = -I$(top_srcdir) -I$(top_srcdir)/pipi

if NEED_GETOPT_LONG
GETOPT = mygetopt.c mygetopt.h
endif

bin_PROGRAMS = genethumb

genethumb_SOURCES = genethumb.c
genethumb_LDADD = ../pipi/libpipi.la
genethumb_SOURCES = genethumb.c $(GETOPT)
genethumb_LDADD = ../pipi/libpipi.la $(GETOPT_LIBS)


+ 63
- 7
genethumb/genethumb.c 查看文件

@@ -1,17 +1,73 @@
#include "config.h"
#include "common.h"

#if !defined HAVE_GETOPT_LONG
# include "mygetopt.h"
#elif defined HAVE_GETOPT_H
# include <getopt.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <pipi.h>

int main(void)
#if defined HAVE_GETOPT_LONG
# define mygetopt getopt_long
# define myoptind optind
# define myoptarg optarg
# define myoption option
#endif

#define MOREINFO "Try `%s --help' for more information.\n"

int main(int argc, char *argv[])
{
pipi_image_t *i, *j;
char *srcname = NULL, *dstname = NULL;
pipi_image_t *src, *dst;

int i, w = 0, h = 0;

for(;;)
{
int option_index = 0;
static struct myoption long_options[] =
{
{ "geometry", 1, NULL, 'g' },
};
int c = mygetopt(argc, argv, "g:", long_options, &option_index);

if(c == -1)
break;

switch(c)
{
case 'g':
w = atoi(myoptarg);
if(strchr(myoptarg, 'x'))
h = atoi(strchr(myoptarg, 'x') + 1);
break;
default:
fprintf(stderr, "%s: invalid option -- %c\n", argv[0], c);
printf(MOREINFO, argv[0]);
return EXIT_FAILURE;
}
}

for(i = myoptind; i < argc; i++)
{
if(!srcname)
srcname = argv[i];
else
dstname = argv[i];
}

i = pipi_load("irc.png");
j = pipi_resize(i, 180, 180);
pipi_save(j, "irc.bmp");
pipi_free(i);
pipi_free(j);
src = pipi_load(srcname);
dst = pipi_resize(src, w, h);
pipi_save(dst, dstname);
pipi_free(src);
pipi_free(dst);

return 0;
}


+ 129
- 0
genethumb/mygetopt.c 查看文件

@@ -0,0 +1,129 @@
/*
* zzuf - general purpose fuzzer
* Copyright (c) 2002, 2007 Sam Hocevar <sam@zoy.org>
* All Rights Reserved
*
* $Id: mygetopt.c 271 2007-02-02 11:58:06Z sam $
*
* This program is free software. It comes without any warranty, to
* the extent permitted by applicable law. You can redistribute it
* and/or modify it under the terms of the Do What The Fuck You Want
* To Public License, Version 2, as published by Sam Hocevar. See
* http://sam.zoy.org/wtfpl/COPYING for more details.
*/

/*
* mygetopt.c: getopt_long reimplementation
*/

#include "config.h"

#if defined HAVE_STDINT_H
# include <stdint.h>
#elif defined HAVE_INTTYPES_H
# include <inttypes.h>
#endif

/* DOS / Kernel driver */
#ifndef __intptr_t_defined
#ifndef _UINTPTR_T
typedef unsigned int uintptr_t;
#endif
#endif

#include <stdio.h>
#include <string.h>

#include "mygetopt.h"

int myoptind = 1;
char *myoptarg = NULL;

/* XXX: this getopt_long implementation should not be trusted for other
* applications without any serious peer reviewing. It “just works” with
* zzuf but may fail miserably in other programs. */
int mygetopt(int argc, char * const _argv[], const char *optstring,
const struct myoption *longopts, int *longindex)
{
char **argv = (char **)(uintptr_t)_argv;
char *flag;
int i;

if(myoptind >= argc)
return -1;

flag = argv[myoptind];

if(flag[0] == '-' && flag[1] != '-')
{
char *tmp;
int ret = flag[1];

if(ret == '\0')
return -1;

tmp = strchr(optstring, ret);
if(!tmp || ret == ':')
return '?';

myoptind++;
if(tmp[1] == ':')
{
if(flag[2] != '\0')
myoptarg = tmp + 2;
else
myoptarg = argv[myoptind++];
return ret;
}

if(flag[2] != '\0')
{
flag[1] = '-';
myoptind--;
argv[myoptind]++;
}

return ret;
}

if(flag[0] == '-' && flag[1] == '-')
{
if(flag[2] == '\0')
return -1;

for(i = 0; longopts[i].name; i++)
{
size_t l = strlen(longopts[i].name);

if(strncmp(flag + 2, longopts[i].name, l))
continue;

switch(flag[2 + l])
{
case '=':
if(!longopts[i].has_arg)
goto bad_opt;
if(longindex)
*longindex = i;
myoptind++;
myoptarg = flag + 2 + l + 1;
return longopts[i].val;
case '\0':
if(longindex)
*longindex = i;
myoptind++;
if(longopts[i].has_arg)
myoptarg = argv[myoptind++];
return longopts[i].val;
default:
break;
}
}
bad_opt:
fprintf(stderr, "%s: unrecognized option `%s'\n", argv[0], flag);
return '?';
}

return -1;
}


+ 31
- 0
genethumb/mygetopt.h 查看文件

@@ -0,0 +1,31 @@
/*
* zzuf - general purpose fuzzer
* Copyright (c) 2002, 2007 Sam Hocevar <sam@zoy.org>
* All Rights Reserved
*
* $Id: mygetopt.h 268 2007-02-01 22:33:07Z sam $
*
* This program is free software. It comes without any warranty, to
* the extent permitted by applicable law. You can redistribute it
* and/or modify it under the terms of the Do What The Fuck You Want
* To Public License, Version 2, as published by Sam Hocevar. See
* http://sam.zoy.org/wtfpl/COPYING for more details.
*/

/*
* mygetopt.h: getopt_long reimplementation
*/

struct myoption
{
const char *name;
int has_arg;
int *flag;
int val;
};

extern int myoptind;
extern char *myoptarg;

int mygetopt(int, char * const[], const char *, const struct myoption *, int *);


正在加载...
取消
保存