[argyllcms] [Fwd: Patch for libcgats - improve compatibility with C++]

  • From: "Alastair M. Robinson" <profiling@xxxxxxxxxxxxxxxxxxxxxxx>
  • To: argyllcms@xxxxxxxxxxxxx
  • Date: Sun, 09 Nov 2008 11:41:55 +0000

(Oops - I sent this from the wrong address!  Trying again...)

Hi,

I've attached a simple patch for libcgats which makes it easier to use
from within C++ code.  It marks a number of "char *" arguments as const
and adds an
#ifdef __cplusplus
        extern "C" {
#endif
wrapper around the cgats.h header file.

All the best,
--
Alastair M. Robinson

diff -urN Argyll_V1.0.3_orig/cgats/cgats.c Argyll_V1.0.3_new/cgats/cgats.c
--- Argyll_V1.0.3_orig/cgats/cgats.c    2008-09-03 01:10:24.000000000 +0100
+++ Argyll_V1.0.3_new/cgats/cgats.c     2008-11-08 11:21:50.000000000 +0000
@@ -35,7 +35,7 @@
 #endif
 
 #ifdef STANDALONE_TEST
-extern void error(char *fmt, ...), warning(char *fmt, ...);
+extern void error(const char *fmt, ...), warning(const char *fmt, ...);
 #endif
 
 #include <sys/types.h>
@@ -48,14 +48,14 @@
 #define REAL_SIGDIG 5          /* Number of significant digits in real 
representation */
 
 static int cgats_read(cgats *p, cgatsFile *fp);
-static int find_kword(cgats *p, int table, char *ksym);
-static int find_field(cgats *p, int table, char *fsym);
+static int find_kword(cgats *p, int table, const char *ksym);
+static int find_field(cgats *p, int table, const char *fsym);
 static int add_table(cgats *p, table_type tt, int oi);
 static int set_table_flags(cgats *p, int table, int sup_id, int sup_kwords, 
int sup_fields);
-static int set_cgats_type(cgats *p, char *osym);
-static int add_other(cgats *p, char *osym);
-static int add_kword(cgats *p, int table, char *ksym, char *kdata, char *kcom);
-static int add_field(cgats *p, int table, char *fsym, data_type ftype);
+static int set_cgats_type(cgats *p, const char *osym);
+static int add_other(cgats *p, const char *osym);
+static int add_kword(cgats *p, int table, const char *ksym, const char *kdata, 
const char *kcom);
+static int add_field(cgats *p, int table, const char *fsym, data_type ftype);
 static int add_set(cgats *p, int table, ...);
 static int add_setarr(cgats *p, int table, cgats_set_elem *args);
 static int get_setarr(cgats *p, int table, int set_index, cgats_set_elem 
*args);
@@ -65,24 +65,24 @@
 
 static void cgats_table_free(cgats_table *t);
 static void *alloc_copy_data_type(cgatsAlloc *al, data_type ktype, void 
*dpoint);
-static int reserved_kword(char *ksym);
-static int standard_kword(char *ksym);
-static data_type standard_field(char *fsym);
-static int cs_has_ws(char *cs);
-static char *quote_cs(cgatsAlloc *al, char *cs);
+static int reserved_kword(const char *ksym);
+static int standard_kword(const char *ksym);
+static data_type standard_field(const char *fsym);
+static int cs_has_ws(const char *cs);
+static char *quote_cs(cgatsAlloc *al, const char *cs);
 static int clear_fields(cgats *p, int table);
-static int add_kword_at(cgats *p, int table, int  pos, char *ksym, char 
*kdatak, char *kcom);
+static int add_kword_at(cgats *p, int table, int  pos, const char *ksym, const 
char *kdatak, const char *kcom);
 static int add_data_item(cgats *p, int table, void *data);
 static void unquote_cs(char *cs);
-static data_type guess_type(char *cs);
+static data_type guess_type(const char *cs);
 static void real_format(double value, int nsd, char *fmt);
 
 #ifdef COMBINED_STD
-static int cgats_read_name(cgats *p, char *filename);
-static int cgats_write_name(cgats *p, char *filename);
+static int cgats_read_name(cgats *p, const char *filename);
+static int cgats_write_name(cgats *p, const char *filename);
 #endif
 
-static char *data_type_desc[] =
+static const char *data_type_desc[] =
        { "real", "integer", "char string", "non-quoted char string", "no type" 
};
 
 /* Create an empty cgats object */
@@ -125,7 +125,7 @@
        return p;
 }
 
-static int err(cgats *p, int errc, char *fmt, ...);
+static int err(cgats *p, int errc, const char *fmt, ...);
 
 /* new_cgats() with default malloc allocator */
 
@@ -141,7 +141,7 @@
 /* Implimentation function - register an error */
 /* Return the error number */
 static int
-err(cgats *p, int errc, char *fmt, ...) {
+err(cgats *p, int errc, const char *fmt, ...) {
        va_list args;
 
        p->errc = errc;
@@ -262,7 +262,7 @@
 /* Return index of the keyword, -1 on fail */
 /* -2 on illegal table index, message in err & errc */
 static int
-find_kword(cgats *p, int table, char *ksym) {
+find_kword(cgats *p, int table, const char *ksym) {
        int i;
        cgats_table *t;
 
@@ -288,7 +288,7 @@
 /* Return index of the field, -1 on fail */
 /* -2 on illegal table index, message in err & errc */
 static int
-find_field(cgats *p, int table, char *fsym) {
+find_field(cgats *p, int table, const char *fsym) {
        int i;
        cgats_table *t;
 
@@ -701,7 +701,7 @@
 /* Define the (one) variable CGATS type */
 /* Return -2 & set errc and err on system error */
 static int
-set_cgats_type(cgats *p, char *osym) {
+set_cgats_type(cgats *p, const char *osym) {
        cgatsAlloc *al = p->al;
 
        p->errc = 0;
@@ -718,7 +718,7 @@
 /* Use a zero length string to indicate a wildcard. */
 /* Return -2 & set errc and err on system error */
 static int
-add_other(cgats *p, char *osym) {
+add_other(cgats *p, const char *osym) {
        cgatsAlloc *al = p->al;
 
        p->errc = 0;
@@ -788,7 +788,7 @@
 /* NULL values for ksym and kdata, and the comment in kcom */
 /* Return the index of the new keyword, or -1, err & errc on error */
 static int
-add_kword(cgats *p, int table, char *ksym, char *kdata, char *kcom) {
+add_kword(cgats *p, int table, const char *ksym, const char *kdata, const char 
*kcom) {
        cgats_table *t;
 
        p->errc = 0;
@@ -808,7 +808,7 @@
 /* NULL values for ksym and kdata, and the comment in kcom */
 /* Return the index of the keyword, or -1, err & errc on error */
 static int
-add_kword_at(cgats *p, int table, int pos, char *ksym, char *kdata, char 
*kcom) {
+add_kword_at(cgats *p, int table, int pos, const char *ksym, const char 
*kdata, const char *kcom) {
        cgatsAlloc *al = p->al;
        cgats_table *t;
 
@@ -877,7 +877,7 @@
 /* Return the index of the field */
 /* return -1 or -2, errc & err on error */
 static int
-add_field(cgats *p, int table, char *fsym, data_type ftype) {
+add_field(cgats *p, int table, const char *fsym, data_type ftype) {
        cgatsAlloc *al = p->al;
        cgats_table *t;
        data_type st;
@@ -1521,7 +1521,7 @@
 /* See if the keyword name is a standard one */
 /* Return non-zero if it is standard */
 static int
-standard_kword(char *ksym) {
+standard_kword(const char *ksym) {
        if (ksym == NULL)
                return 0;
        if (strcmp(ksym,"ORIGINATOR") == 0)
@@ -1551,7 +1551,7 @@
 /* (code generates it automatically) */
 /* Return non-zero if it is reserved */
 static int
-reserved_kword(char *ksym) {
+reserved_kword(const char *ksym) {
        if (ksym == NULL)
                return 0;
        if (strcmp(ksym,"NUMBER_OF_FIELDS") == 0)
@@ -1574,7 +1574,7 @@
 /* See if the field name is a standard one */
 /* with an expected data type */
 static data_type
-standard_field(char *fsym) {
+standard_field(const char *fsym) {
        if (strcmp(fsym,"SAMPLE_ID") == 0)
                return nqcs_t;
        if (strcmp(fsym,"STRING") == 0)
@@ -1683,7 +1683,7 @@
 /* Return non-zero if char string has an embedded */
 /* white space, quote or comment character. */
 static int
-cs_has_ws(char *cs)
+cs_has_ws(const char *cs)
        {
        int i;
        for (i = 0; cs[i] != '\000'; i++)
@@ -1706,7 +1706,7 @@
 /* Return NULL if malloc failed */
 /* (Returned string should be free()'d after use) */
 static char *
-quote_cs(cgatsAlloc *al, char *cs) {
+quote_cs(cgatsAlloc *al, const char *cs) {
        int i,j;
        char *rs;
        /* see how much space we need for returned string */
@@ -1754,7 +1754,7 @@
 
 /* Guess the data type from the string */
 static data_type
-guess_type(char *cs)
+guess_type(const char *cs)
        {
        int i;
        int rf = 0;
@@ -2080,7 +2080,7 @@
 /* Basic printf type error() and warning() routines */
 
 void
-error(char *fmt, ...) {
+error(const char *fmt, ...) {
        va_list args;
 
        fprintf(stderr,"cgats: Error - ");
@@ -2092,7 +2092,7 @@
 }
 
 void
-warning(char *fmt, ...) {
+warning(const char *fmt, ...) {
        va_list args;
 
        fprintf(stderr,"cgats: Warning - ");
diff -urN Argyll_V1.0.3_orig/cgats/cgats.h Argyll_V1.0.3_new/cgats/cgats.h
--- Argyll_V1.0.3_orig/cgats/cgats.h    2008-09-03 01:10:24.000000000 +0100
+++ Argyll_V1.0.3_new/cgats/cgats.h     2008-11-08 11:21:53.000000000 +0000
@@ -20,6 +20,10 @@
 #define CGATSLIB_VERSION 0x020005
 #define CGATSLIB_VERSION_STR "2.05"
 
+#ifdef __cplusplus
+       extern "C" {
+#endif
+
 #include "pars.h"              /* We use the ASCII parsing class */
 
 /* Possible table types */
@@ -80,11 +84,11 @@
        char **others;          /* Other file type identifiers */
 
        /* Public Methods */
-       int (*set_cgats_type)(struct _cgats *p, char *osym);
+       int (*set_cgats_type)(struct _cgats *p, const char *osym);
                                                                                
        /* Define the (one) variable CGATS type */
                                                                                
        /* Return -2, set errc & err on system error */
 
-       int (*add_other)(struct _cgats *p, char *osym);
+       int (*add_other)(struct _cgats *p, const char *osym);
                                                                                
        /* Add a user defined file identifier string. */
                                                                                
        /* Use a zero length string for wildcard. */
                                                                                
        /* Return -2, set errc & err on system error */
@@ -93,13 +97,13 @@
                                                                                
                /* return -ve and errc and err set on error */
 
        /* NULL if SEPARATE_STD is defined: */ 
-       int (*read_name)(struct _cgats *p, char *filename);     /* Standard 
file I/O */
+       int (*read_name)(struct _cgats *p, const char *filename);       /* 
Standard file I/O */
                                                                                
                /* return -ve and errc and err set on error */
 
-       int (*find_kword)(struct _cgats *p, int table, char *ksym);
+       int (*find_kword)(struct _cgats *p, int table, const char *ksym);
                                                                                
                /* Return index of the keyword, -1 on fail */
                                                                                
                /* -2 on illegal table index, errc & err */
-       int (*find_field)(struct _cgats *p, int table, char *fsym);
+       int (*find_field)(struct _cgats *p, int table, const char *fsym);
                                                                                
                /* Return index of the field, -1 on fail */
                                                                                
                /* -2 on illegal table index, errc & err */
 
@@ -110,10 +114,10 @@
        int (*set_table_flags)(struct _cgats *p, int table, int sup_id,int 
sup_kwords,int sup_fields);
                                                /* Set or reset table output 
suppresion flags */
                                                /* Return -ve, set errc & err 
on error */
-       int (*add_kword)(struct _cgats *p, int table, char *ksym, char *kdata, 
char *kcom);
+       int (*add_kword)(struct _cgats *p, int table, const char *ksym, const 
char *kdata, const char *kcom);
                                                /* Add a new keyword/value pair 
+ optional comment to the table */
                                                /* Return index of new keyword, 
or -1, errc & err on error */
-       int (*add_field)(struct _cgats *p, int table, char *fsym, data_type 
ftype);
+       int (*add_field)(struct _cgats *p, int table, const char *fsym, 
data_type ftype);
                                                /* Add a new field to the table 
*/
                                                /* Return index of new field, 
or -1, -2, errc and err on error */
        int (*add_set)(struct _cgats *p, int table, ...);       /* Add a set of 
data */
@@ -127,7 +131,7 @@
                                                /* Fill a suitable set_element 
with a line of data */
                                                /* Return 0 normally, -1, -2, 
errc & err if error */
        /* NULL if SEPARATE_STD is defined: */ 
-       int (*write_name)(struct _cgats *p, char *filename);    /* Standard 
file I/O */
+       int (*write_name)(struct _cgats *p, const char *filename);      /* 
Standard file I/O */
                                                                                
/* return -ve and errc and err set on error */
 
 
@@ -155,8 +159,12 @@
 extern cgats *new_cgats(void);                                                 
/* Standard allocator */
 
 /* Available from cgatsstd.obj SEPARATE_STD is defined: */ 
-CGATS_STATIC int cgats_read_name(cgats *p, char *filename);
-CGATS_STATIC int cgats_write_name(cgats *p, char *filename);
+CGATS_STATIC int cgats_read_name(cgats *p, const char *filename);
+CGATS_STATIC int cgats_write_name(cgats *p, const char *filename);
+
+#ifdef __cplusplus
+       }
+#endif
 
 #define CGATS_H
 #endif /* CGATS_H */
diff -urN Argyll_V1.0.3_orig/cgats/cgatsstd.c Argyll_V1.0.3_new/cgats/cgatsstd.c
--- Argyll_V1.0.3_orig/cgats/cgatsstd.c 2008-09-03 01:10:24.000000000 +0100
+++ Argyll_V1.0.3_new/cgats/cgatsstd.c  2008-11-08 11:21:50.000000000 +0000
@@ -32,7 +32,7 @@
 /* Return the error number */
 /* [Note that this is a duplicate of the one in cgats.c] */
 static int
-err(cgats *p, int errc, char *fmt, ...) {
+err(cgats *p, int errc, const char *fmt, ...) {
        va_list args;
 
        p->errc = errc;
@@ -77,7 +77,7 @@
 /* and set p->errc * p->err */
 CGATS_STATIC
 int
-cgats_read_name(cgats *p, char *filename) {
+cgats_read_name(cgats *p, const char *filename) {
        int rv;
        cgatsFile *fp;
 
@@ -96,7 +96,7 @@
 /* Return -ve, errc & err if there was an error */
 CGATS_STATIC
 int
-cgats_write_name(cgats *p, char *filename) {
+cgats_write_name(cgats *p, const char *filename) {
        int rv;
        cgatsFile *fp;
 
diff -urN Argyll_V1.0.3_orig/cgats/pars.c Argyll_V1.0.3_new/cgats/pars.c
--- Argyll_V1.0.3_orig/cgats/pars.c     2008-09-03 01:10:24.000000000 +0100
+++ Argyll_V1.0.3_new/cgats/pars.c      2008-11-08 11:21:50.000000000 +0000
@@ -22,7 +22,7 @@
 #include <string.h>
 
 #ifdef STANDALONE_TEST
-extern void error(char *fmt, ...), warning(char *fmt, ...);
+extern void error(const char *fmt, ...), warning(const char *fmt, ...);
 #endif
 
 #include "pars.h"
@@ -314,7 +314,7 @@
 
 /* Basic printf type error() and warning() routines for standalone test */
 void
-error(char *fmt, ...) {
+error(const char *fmt, ...) {
        va_list args;
 
        fprintf(stderr,"chart: Error - ");
@@ -326,7 +326,7 @@
 }
 
 void
-warning(char *fmt, ...) {
+warning(const char *fmt, ...) {
        va_list args;
 
        fprintf(stderr,"chart: Warning - ");
diff -urN Argyll_V1.0.3_orig/cgats/pars.h Argyll_V1.0.3_new/cgats/pars.h
--- Argyll_V1.0.3_orig/cgats/pars.h     2008-09-03 01:10:24.000000000 +0100
+++ Argyll_V1.0.3_new/cgats/pars.h      2008-11-08 11:21:53.000000000 +0000
@@ -132,13 +132,13 @@
 }; typedef struct _cgatsFileStd cgatsFileStd;
 
 /* Create given a file name */
-cgatsFile *new_cgatsFileStd_name(char *name, char *mode);
+cgatsFile *new_cgatsFileStd_name(const char *name, const char *mode);
 
 /* Create given a (binary) FILE* */
 cgatsFile *new_cgatsFileStd_fp(FILE *fp);
 
 /* Create given a file name and allocator */
-cgatsFile *new_cgatsFileStd_name_a(char *name, char *mode, cgatsAlloc *al);
+cgatsFile *new_cgatsFileStd_name_a(const char *name, const char *mode, 
cgatsAlloc *al);
 
 /* Create given a (binary) FILE* and allocator */
 cgatsFile *new_cgatsFileStd_fp_a(FILE *fp, cgatsAlloc *al);
diff -urN Argyll_V1.0.3_orig/cgats/parsstd.c Argyll_V1.0.3_new/cgats/parsstd.c
--- Argyll_V1.0.3_orig/cgats/parsstd.c  2008-09-03 01:10:24.000000000 +0100
+++ Argyll_V1.0.3_new/cgats/parsstd.c   2008-11-08 11:21:50.000000000 +0000
@@ -337,16 +337,16 @@
 
 /* Create cgatsFile given a file name */
 cgatsFile *new_cgatsFileStd_name(
-char *name,
-char *mode
+const char *name,
+const char *mode
 ) {
        return new_cgatsFileStd_name_a(name, mode, NULL);
 }
 
 /* Create given a file name and allocator */
 cgatsFile *new_cgatsFileStd_name_a(
-char *name,
-char *mode,
+const char *name,
+const char *mode,
 cgatsAlloc *al                 /* heap allocator, NULL for default */
 ) {
        FILE *fp;

Other related posts: