Create integer macros if stdint.h isn't present

On systems without stdint.h, define some of the stdint macros for
minimum and maximum values of fixed-width integers and for integer
constants.


Project: http://git-wip-us.apache.org/repos/asf/lucy/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucy/commit/9340d65f
Tree: http://git-wip-us.apache.org/repos/asf/lucy/tree/9340d65f
Diff: http://git-wip-us.apache.org/repos/asf/lucy/diff/9340d65f

Branch: refs/heads/c99-types
Commit: 9340d65f19cf54442e8ce8429da3441cfc1c95db
Parents: 1ee7b6f
Author: Nick Wellnhofer <[email protected]>
Authored: Sat Nov 17 22:20:59 2012 +0100
Committer: Nick Wellnhofer <[email protected]>
Committed: Sun Nov 18 16:33:20 2012 +0100

----------------------------------------------------------------------
 charmonizer/src/Charmonizer/Core/ConfWriterC.c |   12 ++++-
 charmonizer/src/Charmonizer/Probe/Integers.c   |   47 ++++++++++++++++++-
 charmonizer/src/Charmonizer/Probe/Integers.h   |   20 ++++++++-
 3 files changed, 76 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucy/blob/9340d65f/charmonizer/src/Charmonizer/Core/ConfWriterC.c
----------------------------------------------------------------------
diff --git a/charmonizer/src/Charmonizer/Core/ConfWriterC.c 
b/charmonizer/src/Charmonizer/Core/ConfWriterC.c
index 3be86d1..132a4e4 100644
--- a/charmonizer/src/Charmonizer/Core/ConfWriterC.c
+++ b/charmonizer/src/Charmonizer/Core/ConfWriterC.c
@@ -188,7 +188,17 @@ chaz_ConfWriterC_add_global_def(const char *sym, const 
char *value) {
 static void
 chaz_ConfWriterC_append_global_def_to_conf(const char *sym,
         const char *value) {
-    fprintf(chaz_ConfWriterC.fh, "#ifndef %s\n", sym);
+    char *name_end = strchr(sym, '(');
+    if (name_end == NULL) {
+        fprintf(chaz_ConfWriterC.fh, "#ifndef %s\n", sym);
+    }
+    else {
+        size_t  name_len = (size_t)(name_end - sym);
+        char   *name     = chaz_Util_strdup(sym);
+        name[name_len] = '\0';
+        fprintf(chaz_ConfWriterC.fh, "#ifndef %s\n", name);
+        free(name);
+    }
     if (value) {
         fprintf(chaz_ConfWriterC.fh, "  #define %s %s\n", sym, value);
     }

http://git-wip-us.apache.org/repos/asf/lucy/blob/9340d65f/charmonizer/src/Charmonizer/Probe/Integers.c
----------------------------------------------------------------------
diff --git a/charmonizer/src/Charmonizer/Probe/Integers.c 
b/charmonizer/src/Charmonizer/Probe/Integers.c
index 15f4326..d307ce5 100644
--- a/charmonizer/src/Charmonizer/Probe/Integers.c
+++ b/charmonizer/src/Charmonizer/Probe/Integers.c
@@ -260,7 +260,7 @@ chaz_Integers_run(void) {
         chaz_ConfWriter_add_sys_include("stdint.h");
     }
     else {
-        /* we support only the following subset of stdint.h
+        /* We support only the following subset of stdint.h
          *   int8_t
          *   int16_t
          *   int32_t
@@ -269,25 +269,70 @@ chaz_Integers_run(void) {
          *   uint16_t
          *   uint32_t
          *   uint64_t
+         *   INT8_MAX
+         *   INT16_MAX
+         *   INT32_MAX
+         *   INT64_MAX
+         *   INT8_MIN
+         *   INT16_MIN
+         *   INT32_MIN
+         *   INT64_MIN
+         *   UINT8_MAX
+         *   UINT16_MAX
+         *   UINT32_MAX
+         *   UINT64_MAX
+         *   SIZE_MAX
+         *   INT32_C
+         *   INT64_C
+         *   UINT32_C
+         *   UINT64_C
          */
         if (has_8) {
             chaz_ConfWriter_add_global_typedef("signed char", "int8_t");
             chaz_ConfWriter_add_global_typedef("unsigned char", "uint8_t");
+            chaz_ConfWriter_add_global_def("INT8_MAX", "127");
+            chaz_ConfWriter_add_global_def("INT8_MIN", "-128");
+            chaz_ConfWriter_add_global_def("UINT8_MAX", "255");
         }
         if (has_16) {
             chaz_ConfWriter_add_global_typedef("signed short", "int16_t");
             chaz_ConfWriter_add_global_typedef("unsigned short", "uint16_t");
+            chaz_ConfWriter_add_global_def("INT16_MAX", "32767");
+            chaz_ConfWriter_add_global_def("INT16_MIN", "-32768");
+            chaz_ConfWriter_add_global_def("UINT16_MAX", "65535");
         }
         if (has_32) {
             chaz_ConfWriter_add_global_typedef(i32_t_type, "int32_t");
             sprintf(scratch, "unsigned %s", i32_t_type);
             chaz_ConfWriter_add_global_typedef(scratch, "uint32_t");
+            chaz_ConfWriter_add_global_def("INT32_MAX", "2147483647");
+            chaz_ConfWriter_add_global_def("INT32_MIN", "(-INT32_MAX-1)");
+            chaz_ConfWriter_add_global_def("UINT32_MAX", "4294967295U");
+            if (strcmp(i32_t_postfix, "") == 0) {
+                chaz_ConfWriter_add_global_def("INT32_C(n)", "n");
+            }
+            else {
+                sprintf(scratch, "n##%s", i32_t_postfix);
+                chaz_ConfWriter_add_global_def("INT32_C(n)", scratch);
+            }
+            sprintf(scratch, "n##%s", u32_t_postfix);
+            chaz_ConfWriter_add_global_def("UINT32_C(n)", scratch);
         }
         if (has_64) {
             chaz_ConfWriter_add_global_typedef(i64_t_type, "int64_t");
             sprintf(scratch, "unsigned %s", i64_t_type);
             chaz_ConfWriter_add_global_typedef(scratch, "uint64_t");
+            sprintf(scratch, "9223372036854775807%s", i64_t_postfix);
+            chaz_ConfWriter_add_global_def("INT64_MAX", scratch);
+            chaz_ConfWriter_add_global_def("INT64_MIN", "(-INT64_MAX-1)");
+            sprintf(scratch, "18446744073709551615%s", u64_t_postfix);
+            chaz_ConfWriter_add_global_def("UINT64_MAX", scratch);
+            sprintf(scratch, "n##%s", i64_t_postfix);
+            chaz_ConfWriter_add_global_def("INT64_C(n)", scratch);
+            sprintf(scratch, "n##%s", u64_t_postfix);
+            chaz_ConfWriter_add_global_def("UINT64_C(n)", scratch);
         }
+        chaz_ConfWriter_add_global_def("SIZE_MAX", "((size_t)-1)");
     }
     if (has_8) {
         chaz_ConfWriter_add_def("HAS_I8_T", NULL);

http://git-wip-us.apache.org/repos/asf/lucy/blob/9340d65f/charmonizer/src/Charmonizer/Probe/Integers.h
----------------------------------------------------------------------
diff --git a/charmonizer/src/Charmonizer/Probe/Integers.h 
b/charmonizer/src/Charmonizer/Probe/Integers.h
index 50e807b..22766fe 100644
--- a/charmonizer/src/Charmonizer/Probe/Integers.h
+++ b/charmonizer/src/Charmonizer/Probe/Integers.h
@@ -47,7 +47,8 @@
  * HAS_STDINT_H
  *
  * If stdint.h is is available, it will be pound-included in the configuration
- * header.  If it is not, the following typedefs will be defined if possible:
+ * header.  If it is not, the following typedefs and macros will be defined if
+ * possible:
  *
  * int8_t
  * int16_t
@@ -57,6 +58,23 @@
  * uint16_t
  * uint32_t
  * uint64_t
+ * INT8_MAX
+ * INT16_MAX
+ * INT32_MAX
+ * INT64_MAX
+ * INT8_MIN
+ * INT16_MIN
+ * INT32_MIN
+ * INT64_MIN
+ * UINT8_MAX
+ * UINT16_MAX
+ * UINT32_MAX
+ * UINT64_MAX
+ * SIZE_MAX
+ * INT32_C
+ * INT64_C
+ * UINT32_C
+ * UINT64_C
  *
  * The following typedefs will be created if a suitable integer type exists,
  * as will most often be the case.  However, if for example a char is 64 bits

Reply via email to