freetype-commit
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[freetype2-demos] master 0fb06cf: Move Mark Leisher's `getopt' implement


From: Werner LEMBERG
Subject: [freetype2-demos] master 0fb06cf: Move Mark Leisher's `getopt' implementation to a separate file.
Date: Sat, 6 Aug 2016 21:51:39 +0000 (UTC)

branch: master
commit 0fb06cf190330dfe452a8fa1843be075a831077b
Author: Werner Lemberg <address@hidden>
Commit: Werner Lemberg <address@hidden>

    Move Mark Leisher's `getopt' implementation to a separate file.
    
    We need this since `ftbench.c' now includes `unistd.h', which in
    turn declares `getopt'.
    
    * src/common.c, src/common.h: Move `getopt' stuff to...
    * src/mlgetopt.c, src/mlgetopt.h: ... these new files.
    Update all callers.
    
    * src/common.c, src/common.h: Use standard C only.
    Replace `CONST' with `const'.
    
    * Makefile (COMMON_OBJ): Add `mlgetopt'.
---
 ChangeLog                    |   16 ++++
 Makefile                     |    4 +-
 src/common.c                 |  212 +++---------------------------------------
 src/common.h                 |   60 +++---------
 src/ftbench.c                |    2 +
 src/ftdiff.c                 |    1 +
 src/ftdump.c                 |    1 +
 src/ftgrid.c                 |    1 +
 src/ftmulti.c                |    1 +
 src/ftstring.c               |    1 +
 src/ftvalid.c                |    1 +
 src/ftview.c                 |    1 +
 src/{common.c => mlgetopt.c} |  129 +------------------------
 src/{common.h => mlgetopt.h} |   36 +------
 src/ttdebug.c                |    1 +
 15 files changed, 59 insertions(+), 408 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 9c03555..0981cf1 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,19 @@
+2016-08-06  Werner Lemberg  <address@hidden>
+
+       Move Mark Leisher's `getopt' implementation to a separate file.
+
+       We need this since `ftbench.c' now includes `unistd.h', which in
+       turn declares `getopt'.
+
+       * src/common.c, src/common.h: Move `getopt' stuff to...
+       * src/mlgetopt.c, src/mlgetopt.h: ... these new files.
+       Update all callers.
+
+       * src/common.c, src/common.h: Use standard C only.
+       Replace `CONST' with `const'.
+
+       * Makefile (COMMON_OBJ): Add `mlgetopt'.
+
 2016-08-02  Alexei Podtelezhnikov  <address@hidden>
 
        * src/ftbench.c (get_time, benchmark): Clean up timers.
diff --git a/Makefile b/Makefile
index fafca4e..428d91e 100644
--- a/Makefile
+++ b/Makefile
@@ -320,8 +320,10 @@ else
   #
   $(OBJ_DIR_2)/common.$(SO): $(SRC_DIR)/common.c
   $(OBJ_DIR_2)/output.$(SO): $(SRC_DIR)/output.c
+  $(OBJ_DIR_2)/mlgetopt.$(SO): $(SRC_DIR)/mlgetopt.c
   COMMON_OBJ := $(OBJ_DIR_2)/common.$(SO) \
-                $(OBJ_DIR_2)/output.$(SO)
+                $(OBJ_DIR_2)/output.$(SO) \
+                $(OBJ_DIR_2)/mlgetopt.$(SO)
 
 
   FTCOMMON_OBJ := $(OBJ_DIR_2)/ftcommon.$(SO)
diff --git a/src/common.c b/src/common.c
index f5ac6fc..1960165 100644
--- a/src/common.c
+++ b/src/common.c
@@ -1,193 +1,17 @@
-/*
- *  This is a cheap replacement for getopt() because that routine is not
- *  available on some platforms and behaves differently on other platforms.
- *  This code was written from scratch without looking at any other
- *  implementation.
- *
- *  This code is hereby expressly placed in the public domain.
- *  address@hidden (Mark Leisher)
- *  10 October 1997
- *
- *  Last update 2009-03-11.
- */
+/* some utility functions */
 
 #include "common.h"
 
 #include <stdio.h>
 #include <stdarg.h>
 #include <stdlib.h>
-#include <string.h>
 
-#ifdef __STDC__
-#define CONST  const
-#else
-#define CONST
-#endif
-
-  /*
-   *  Externals visible to programs.
-   */
-
-  int    opterr = 1;
-  int    optind = 1;
-  char*  optarg;
-
-  /*
-   *  Internal variables that are used to detect when the global values
-   *  need to be reset.
-   */
-
-  static int  cmdac;
-  static CONST char*   cmdname;
-  static char* CONST*  cmdav;
-
-  int
-#ifdef __STDC__
-  getopt( int  ac, char* const*  av, const char*  pat )
-#else
-  getopt( ac, av, pat )
-    int     ac;
-    char**  av;
-    char*   pat;
-#endif
-  {
-    int  opt;
-    CONST char*  p;
-    CONST char*  pp;
-
-    /*
-     *  If there is no pattern, indicate the parsing is done.
-     */
-    if ( pat == 0 || *pat == 0 )
-      return -1;
-
-    /*
-     *  Always reset the option argument to NULL.
-     */
-    optarg = 0;
-
-    /*
-     *  If the number of arguments or argument list do not match the last
-     *  values seen, reset the internal pointers and the globals.
-     */
-    if ( ac != cmdac || av != cmdav )
-    {
-      optind = 1;
-      cmdac = ac;
-      cmdav = av;
-
-      /*
-       *  Determine the command name in case it is needed for warning
-       *  messages.
-       */
-      for ( cmdname = 0, p = av[0]; *p; p++ )
-      {
-        if ( *p == '/' || *p == '\\' )
-          cmdname = p;
-      }
-      /*
-       *  Skip the path separator if the name was assigned.
-       */
-      if ( cmdname )
-        cmdname++;
-      else
-        cmdname = av[0];
-    }
-
-    /*
-     *  If the next index is greater than or equal to the number of
-     *  arguments, then the command line is done.
-     */
-    if ( optind >= ac )
-      return -1;
-
-    /*
-     *  Test the next argument for one of three cases:
-     *    1. The next argument does not have an initial '-'.
-     *    2. The next argument is '-'.
-     *    3. The next argument is '--'.
-     *
-     *  In either of these cases, command line processing is done.
-     */
-    if ( av[optind][0] != '-'            ||
-         strcmp( av[optind], "-" ) == 0  ||
-         strcmp( av[optind], "--" ) == 0 )
-      return -1;
-
-    /*
-     *  Point at the next command line argument and increment the
-     *  command line index.
-     */
-    p = av[optind++];
-
-    /*
-     *  Look for the first character of the command line option.
-     */
-    for ( opt = *(p + 1), pp = pat; *pp && *pp != opt; pp++ )
-      ;
-
-    /*
-     *  If nothing in the pattern was recognized, then issue a warning
-     *  and return a '?'.
-     */
-    if ( *pp == 0 )
-    {
-      if ( opterr )
-        fprintf( stderr, "%s: invalid option -- %c\n", cmdname, opt );
-      return '?';
-    }
-
-    /*
-     *  If the option expects an argument, get it.
-     */
-    if ( *(pp + 1) == ':' && (optarg = av[optind]) == 0 )
-    {
-      /*
-       *  If the option argument is NULL, issue a warning and return a '?'.
-       */
-      if ( opterr )
-        fprintf( stderr, "%s: option requires an argument -- %c\n",
-                         cmdname, opt );
-      opt = '?';
-    }
-    else if ( optarg )
-    /*
-     *  Increment the option index past the argument.
-     */
-      optind++;
-
-    /*
-     *  Return the option character.
-     */
-    return opt;
-  }
-
-
-/****************************************************************************/
-/*                                                                          */
-/*  The FreeType project -- a free and portable quality TrueType renderer.  */
-/*                                                                          */
-/*  Copyright 1996-1998 by                                                  */
-/*  D. Turner, R.Wilhelm, and W. Lemberg                                    */
-/*                                                                          */
-/* ft_basename():                                                           */
-/*                                                                          */
-/* a stupid but useful function...                                          */
-/*                                                                          */
-/* rewritten by DavidT to get rid of GPLed programs in the FreeType demos.  */
-/*                                                                          */
-/****************************************************************************/
 
   char*
-#ifdef __STDC__
-  ft_basename ( const char*  name )
-#else
-  ft_basename ( name )
-    char* name;
-#endif
+  ft_basename( const char*  name )
   {
-    CONST char*  base;
-    CONST char*  current;
+    const char*  base;
+    const char*  current;
     char         c;
 
     base    = name;
@@ -213,12 +37,8 @@
 
 
   void
-#ifdef __STDC__
-  Panic( const char*  fmt, ... )
-#else
-  Panic( fmt )
-    char* fmt;
-#endif
+  Panic( const char*  fmt,
+         ... )
   {
     va_list  ap;
 
@@ -232,20 +52,14 @@
 
 
   extern int
-#ifdef __STDC__
-  utf8_next( const char** pcursor,
-             const char*  end )
-#else
-  utf8_next( pcursor, end )
-    char** pcursor;
-    char*  end;
-#endif
+  utf8_next( const char**  pcursor,
+             const char*   end )
   {
-    CONST unsigned char*  p = (CONST unsigned char*)*pcursor;
+    const unsigned char*  p = (const unsigned char*)*pcursor;
     int                   ch;
 
 
-    if ( (CONST char*)p >= end ) /* end of stream */
+    if ( (const char*)p >= end ) /* end of stream */
       return -1;
 
     ch = *p++;
@@ -274,7 +88,7 @@
 
       while ( len > 0 )
       {
-        if ( (CONST char*)p >= end || ( p[0] & 0xc0 ) != 0x80 )
+        if ( (const char*)p >= end || ( p[0] & 0xc0 ) != 0x80 )
           goto BAD_DATA;
 
         ch   = ( ch << 6 ) | ( p[0] & 0x3f );
@@ -283,11 +97,13 @@
       }
     }
 
-    *pcursor = (CONST char*) p;
+    *pcursor = (const char*)p;
+
     return ch;
 
   BAD_DATA:
     return -1;
   }
 
+
 /* End */
diff --git a/src/common.h b/src/common.h
index 627bbd2..afa5f70 100644
--- a/src/common.h
+++ b/src/common.h
@@ -1,66 +1,30 @@
-/*
- *  This is a cheap replacement for getopt() because that routine is not
- *  available on some platforms and behaves differently on other platforms.
- *
- *  This code is hereby expressly placed in the public domain.
- *  address@hidden (Mark Leisher)
- *  10 October 1997
- */
-
 #ifndef COMMON_H_
 #define COMMON_H_
 
-/* Note that by default, both functions are implemented in common.c */
-
-#ifdef VMS
-#define getopt local_getopt
-#define optind local_optind
-#define opterr local_opterr
-#endif
 
 #ifdef __cplusplus
   extern "C" {
 #endif
 
-  extern int    opterr;
-  extern int    optind;
-  extern char*  optarg;
-
-  extern int  getopt(
-#ifdef __STDC__
-    int           argc,
-    char* const*  argv,
-    const char*   pattern
-#endif
-  );
-
-
-  extern char*  ft_basename(
-#ifdef __STDC__
-    const char*  name
-#endif
-  );
+  extern char*
+  ft_basename( const char*  name );
 
   /* print a message and exit */
-  extern void  Panic(
-#ifdef __STDC__
-    const char*  fmt, ...
-#endif
-  );
+  extern void
+  Panic( const char*  fmt,
+         ... );
 
-  /* read the next UTF-8 code from `*pcursor' and
+  /*
+   * Read the next UTF-8 code from `*pcursor' and
    * returns its value. `end' is the limit of the
    * input string.
    *
-   * return -1 if the end of the input string is
-   * reached, or in case of malformed data
+   * Return -1 if the end of the input string is
+   * reached, or in case of malformed data.
    */
-  extern int  utf8_next(
-#ifdef __STDC__
-    const char** pcursor,
-    const char*  end
-#endif
-  );
+  extern int
+  utf8_next( const char**  pcursor,
+             const char*   end );
 
 #ifdef __cplusplus
   }
diff --git a/src/ftbench.c b/src/ftbench.c
index 0e89d3f..c3e7cce 100644
--- a/src/ftbench.c
+++ b/src/ftbench.c
@@ -32,6 +32,8 @@
 
 #ifdef UNIX
 #include <unistd.h>
+#else
+#include "mlgetopt.h"
 #endif
 
 #include "common.h"
diff --git a/src/ftdiff.c b/src/ftdiff.c
index f69a4ea..d65e7f6 100644
--- a/src/ftdiff.c
+++ b/src/ftdiff.c
@@ -15,6 +15,7 @@
 
 #include "ftcommon.h"
 #include "common.h"
+#include "mlgetopt.h"
 
 #include FT_OUTLINE_H
 #include FT_LCD_FILTER_H
diff --git a/src/ftdump.c b/src/ftdump.c
index 3676bab..b8a7619 100644
--- a/src/ftdump.c
+++ b/src/ftdump.c
@@ -25,6 +25,7 @@
 
 #include "common.h"
 #include "output.h"
+#include "mlgetopt.h"
 
 #include <stdio.h>
 #include <stdlib.h>
diff --git a/src/ftgrid.c b/src/ftgrid.c
index 5933d2e..e9ec6d6 100644
--- a/src/ftgrid.c
+++ b/src/ftgrid.c
@@ -16,6 +16,7 @@
 #include "ftcommon.h"
 #include "common.h"
 #include "output.h"
+#include "mlgetopt.h"
 #include <stdlib.h>
 
   /* the following header shouldn't be used in normal programs */
diff --git a/src/ftmulti.c b/src/ftmulti.c
index 7c0153a..f97a48c 100644
--- a/src/ftmulti.c
+++ b/src/ftmulti.c
@@ -17,6 +17,7 @@
 #include FT_MULTIPLE_MASTERS_H
 
 #include "common.h"
+#include "mlgetopt.h"
 
 #include <stdio.h>
 #include <stdlib.h>
diff --git a/src/ftstring.c b/src/ftstring.c
index b95cd47..0cdd2bb 100644
--- a/src/ftstring.c
+++ b/src/ftstring.c
@@ -13,6 +13,7 @@
 
 #include "ftcommon.h"
 #include "common.h"
+#include "mlgetopt.h"
 
 #include <stdio.h>
 #include <stdlib.h>
diff --git a/src/ftvalid.c b/src/ftvalid.c
index 7ea7373..b5a91ee 100644
--- a/src/ftvalid.c
+++ b/src/ftvalid.c
@@ -32,6 +32,7 @@
 #include FT_INTERNAL_OBJECTS_H
 
 #include "common.h"
+#include "mlgetopt.h"
 
 
 #include <stdio.h>
diff --git a/src/ftview.c b/src/ftview.c
index 2a0c9dd..c649eee 100644
--- a/src/ftview.c
+++ b/src/ftview.c
@@ -18,6 +18,7 @@
 
 #include "ftcommon.h"
 #include "common.h"
+#include "mlgetopt.h"
 #include <stdio.h>
 
   /* the following header shouldn't be used in normal programs */
diff --git a/src/common.c b/src/mlgetopt.c
similarity index 56%
copy from src/common.c
copy to src/mlgetopt.c
index f5ac6fc..607130a 100644
--- a/src/common.c
+++ b/src/mlgetopt.c
@@ -11,7 +11,7 @@
  *  Last update 2009-03-11.
  */
 
-#include "common.h"
+#include "mlgetopt.h"
 
 #include <stdio.h>
 #include <stdarg.h>
@@ -163,131 +163,4 @@
   }
 
 
-/****************************************************************************/
-/*                                                                          */
-/*  The FreeType project -- a free and portable quality TrueType renderer.  */
-/*                                                                          */
-/*  Copyright 1996-1998 by                                                  */
-/*  D. Turner, R.Wilhelm, and W. Lemberg                                    */
-/*                                                                          */
-/* ft_basename():                                                           */
-/*                                                                          */
-/* a stupid but useful function...                                          */
-/*                                                                          */
-/* rewritten by DavidT to get rid of GPLed programs in the FreeType demos.  */
-/*                                                                          */
-/****************************************************************************/
-
-  char*
-#ifdef __STDC__
-  ft_basename ( const char*  name )
-#else
-  ft_basename ( name )
-    char* name;
-#endif
-  {
-    CONST char*  base;
-    CONST char*  current;
-    char         c;
-
-    base    = name;
-    current = name;
-
-    c = *current;
-
-    while ( c )
-    {
-#ifndef macintosh
-      if ( c == '/' || c == '\\' )
-#else
-      if ( c == ':' )
-#endif
-        base = current + 1;
-
-      current++;
-      c = *current;
-    }
-
-    return (char*)base;
-  }
-
-
-  void
-#ifdef __STDC__
-  Panic( const char*  fmt, ... )
-#else
-  Panic( fmt )
-    char* fmt;
-#endif
-  {
-    va_list  ap;
-
-
-    va_start( ap, fmt );
-    vprintf( fmt, ap );
-    va_end( ap );
-
-    exit( 1 );
-  }
-
-
-  extern int
-#ifdef __STDC__
-  utf8_next( const char** pcursor,
-             const char*  end )
-#else
-  utf8_next( pcursor, end )
-    char** pcursor;
-    char*  end;
-#endif
-  {
-    CONST unsigned char*  p = (CONST unsigned char*)*pcursor;
-    int                   ch;
-
-
-    if ( (CONST char*)p >= end ) /* end of stream */
-      return -1;
-
-    ch = *p++;
-    if ( ch >= 0x80 )
-    {
-      int  len;
-
-
-      if ( ch < 0xc0 )  /* malformed data */
-        goto BAD_DATA;
-      else if ( ch < 0xe0 )
-      {
-        len = 1;
-        ch &= 0x1f;
-      }
-      else if ( ch < 0xf0 )
-      {
-        len = 2;
-        ch &= 0x0f;
-      }
-      else
-      {
-        len = 3;
-        ch &= 0x07;
-      }
-
-      while ( len > 0 )
-      {
-        if ( (CONST char*)p >= end || ( p[0] & 0xc0 ) != 0x80 )
-          goto BAD_DATA;
-
-        ch   = ( ch << 6 ) | ( p[0] & 0x3f );
-        p   += 1;
-        len -= 1;
-      }
-    }
-
-    *pcursor = (CONST char*) p;
-    return ch;
-
-  BAD_DATA:
-    return -1;
-  }
-
 /* End */
diff --git a/src/common.h b/src/mlgetopt.h
similarity index 50%
copy from src/common.h
copy to src/mlgetopt.h
index 627bbd2..1c07629 100644
--- a/src/common.h
+++ b/src/mlgetopt.h
@@ -7,10 +7,8 @@
  *  10 October 1997
  */
 
-#ifndef COMMON_H_
-#define COMMON_H_
-
-/* Note that by default, both functions are implemented in common.c */
+#ifndef MLGETOPT_H_
+#define MLGETOPT_H_
 
 #ifdef VMS
 #define getopt local_getopt
@@ -34,39 +32,11 @@
 #endif
   );
 
-
-  extern char*  ft_basename(
-#ifdef __STDC__
-    const char*  name
-#endif
-  );
-
-  /* print a message and exit */
-  extern void  Panic(
-#ifdef __STDC__
-    const char*  fmt, ...
-#endif
-  );
-
-  /* read the next UTF-8 code from `*pcursor' and
-   * returns its value. `end' is the limit of the
-   * input string.
-   *
-   * return -1 if the end of the input string is
-   * reached, or in case of malformed data
-   */
-  extern int  utf8_next(
-#ifdef __STDC__
-    const char** pcursor,
-    const char*  end
-#endif
-  );
-
 #ifdef __cplusplus
   }
 #endif
 
-#endif /* COMMON_H_ */
+#endif /* MLGETOPT_H_ */
 
 
 /* End */
diff --git a/src/ttdebug.c b/src/ttdebug.c
index 5771bb7..7f37fc6 100644
--- a/src/ttdebug.c
+++ b/src/ttdebug.c
@@ -51,6 +51,7 @@
 #include <ft2build.h>
 #include FT_FREETYPE_H
 #include "common.h"
+#include "mlgetopt.h"
 
 #include FT_TRUETYPE_DRIVER_H
 



reply via email to

[Prev in Thread] Current Thread [Next in Thread]