summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlbrecht Schlosser <albrechts.fltk@online.de>2020-02-10 11:55:34 +0100
committerAlbrecht Schlosser <albrechts.fltk@online.de>2020-02-10 12:10:40 +0100
commit4c1b92eb52db567b4c1618222bb3007a2a2e7d9b (patch)
tree58081f8e02a8d43dc273de033d454c16e9946f28
parentdcb848ca3ed0ebddd4f28888d2a8ab2f59c9fd1c (diff)
Implement fl_putenv() as cross-platform putenv()
-rw-r--r--CHANGES.txt3
-rw-r--r--FL/fl_utf8.h9
-rw-r--r--src/Fl_System_Driver.H8
-rw-r--r--src/drivers/Android/Fl_Android_System_Driver.H8
-rw-r--r--src/drivers/Posix/Fl_Posix_System_Driver.H12
-rw-r--r--src/drivers/WinAPI/Fl_WinAPI_System_Driver.H10
-rw-r--r--src/drivers/WinAPI/Fl_WinAPI_System_Driver.cxx19
-rw-r--r--src/fl_utf8.cxx47
-rw-r--r--test/demo.cxx21
9 files changed, 79 insertions, 58 deletions
diff --git a/CHANGES.txt b/CHANGES.txt
index 83d6e33b1..3a1af7e84 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -1,4 +1,4 @@
-Changes in FLTK 1.4.0 Released: ??? ?? 2019
+Changes in FLTK 1.4.0 Released: ??? ?? 2020
General Information about this Release
@@ -18,6 +18,7 @@ Changes in FLTK 1.4.0 Released: ??? ?? 2019
New Features and Extensions
- (add new items here)
+ - New fl_putenv() is a cross-platform putenv() wrapper (see docs).
- New Fl::keyboard_screen_scaling(0) call stops recognition of ctrl/+/-/0/
keystrokes as scaling all windows of a screen.
- New "Preview" switch added to the GTK native file chooser dialog
diff --git a/FL/fl_utf8.h b/FL/fl_utf8.h
index 5f759418a..faa69e7e8 100644
--- a/FL/fl_utf8.h
+++ b/FL/fl_utf8.h
@@ -3,17 +3,17 @@
*
* Author: Jean-Marc Lienher ( http://oksid.ch )
* Copyright 2000-2010 by O'ksi'D.
- * Copyright 2016-2018 by Bill Spitzak and others.
+ * Copyright 2016-2020 by Bill Spitzak and others.
*
* This library is free software. Distribution and use rights are outlined in
* the file "COPYING" which should have been included with this file. If this
* file is missing or damaged, see the license at:
*
- * http://www.fltk.org/COPYING.php
+ * https://www.fltk.org/COPYING.php
*
* Please report all bugs and problems on the following page:
*
- * http://www.fltk.org/str.php
+ * https://www.fltk.org/str.php
*/
/* Merged in some functionality from the fltk-2 version. IMM.
@@ -191,6 +191,9 @@ FL_EXPORT int fl_rmdir(const char *f);
/* OD: Portable UTF-8 aware getenv wrapper */
FL_EXPORT char* fl_getenv(const char *name);
+/* Portable UTF-8 aware putenv wrapper */
+FL_EXPORT int fl_putenv(const char *var);
+
/* OD: Portable UTF-8 aware execvp wrapper */
FL_EXPORT int fl_mkdir(const char* f, int mode);
diff --git a/src/Fl_System_Driver.H b/src/Fl_System_Driver.H
index c3f8dc013..67f608177 100644
--- a/src/Fl_System_Driver.H
+++ b/src/Fl_System_Driver.H
@@ -4,17 +4,17 @@
// A base class for platform specific system calls
// for the Fast Light Tool Kit (FLTK).
//
-// Copyright 2010-2018 by Bill Spitzak and others.
+// Copyright 2010-2020 by Bill Spitzak and others.
//
// This library is free software. Distribution and use rights are outlined in
// the file "COPYING" which should have been included with this file. If this
// file is missing or damaged, see the license at:
//
-// http://www.fltk.org/COPYING.php
+// https://www.fltk.org/COPYING.php
//
// Please report all bugs and problems on the following page:
//
-// http://www.fltk.org/str.php
+// https://www.fltk.org/str.php
//
/**
@@ -97,7 +97,7 @@ public:
// implement these to support cross-platform file operations
virtual char *utf2mbcs(const char *s) {return (char*)s;}
virtual char *getenv(const char* v) {return NULL;}
- virtual int putenv(char* v) {return -1;}
+ virtual int putenv(const char *var) {return -1;}
virtual int open(const char* f, int oflags, int pmode) {return -1;}
// Note: the default implementation ignores the 'binary' argument.
diff --git a/src/drivers/Android/Fl_Android_System_Driver.H b/src/drivers/Android/Fl_Android_System_Driver.H
index ec6a6d4aa..e3ccb41bd 100644
--- a/src/drivers/Android/Fl_Android_System_Driver.H
+++ b/src/drivers/Android/Fl_Android_System_Driver.H
@@ -3,17 +3,17 @@
//
// Definition of Android system driver for the Fast Light Tool Kit (FLTK).
//
-// Copyright 2010-2018 by Bill Spitzak and others.
+// Copyright 2010-2020 by Bill Spitzak and others.
//
// This library is free software. Distribution and use rights are outlined in
// the file "COPYING" which should have been included with this file. If this
// file is missing or damaged, see the license at:
//
-// http://www.fltk.org/COPYING.php
+// https://www.fltk.org/COPYING.php
//
// Please report all bugs and problems on the following page:
//
-// http://www.fltk.org/str.php
+// https://www.fltk.org/str.php
//
/**
@@ -48,7 +48,7 @@ public:
virtual void fatal(const char *format, va_list args);
virtual char *utf2mbcs(const char *s);
virtual char *getenv(const char *var);
- virtual int putenv(char *var) {return ::putenv(var);} // *FIXME* needs string conversion
+ virtual int putenv(const char *var) { return ::putenv(strdup(var)); }
virtual int open(const char *fnam, int oflags, int pmode);
virtual int open_ext(const char *fnam, int binary, int oflags, int pmode);
virtual FILE *fopen(const char *fnam, const char *mode);
diff --git a/src/drivers/Posix/Fl_Posix_System_Driver.H b/src/drivers/Posix/Fl_Posix_System_Driver.H
index e4d6eba1e..407fa0270 100644
--- a/src/drivers/Posix/Fl_Posix_System_Driver.H
+++ b/src/drivers/Posix/Fl_Posix_System_Driver.H
@@ -1,20 +1,20 @@
//
// "$Id$"
//
-// Definition of Posix system driver
+// Definition of POSIX system driver
// for the Fast Light Tool Kit (FLTK).
//
-// Copyright 2010-2017 by Bill Spitzak and others.
+// Copyright 2010-2020 by Bill Spitzak and others.
//
// This library is free software. Distribution and use rights are outlined in
// the file "COPYING" which should have been included with this file. If this
// file is missing or damaged, see the license at:
//
-// http://www.fltk.org/COPYING.php
+// https://www.fltk.org/COPYING.php
//
// Please report all bugs and problems on the following page:
//
-// http://www.fltk.org/str.php
+// https://www.fltk.org/str.php
//
/**
@@ -52,8 +52,8 @@ public:
virtual int open(const char* f, int oflags, int pmode) {
return pmode == -1 ? ::open(f, oflags) : ::open(f, oflags, pmode);
}
- virtual char *getenv(const char *v) { return ::getenv(v); }
- virtual int putenv(char* v) {return ::putenv(v);}
+ virtual char *getenv(const char *v) { return ::getenv(v); }
+ virtual int putenv(const char *var) {return ::putenv(strdup(var));}
virtual int system(const char* cmd) {return ::system(cmd);}
virtual int execvp(const char *file, char *const *argv) {return ::execvp(file, argv);}
virtual int chmod(const char* f, int mode) {return ::chmod(f, mode);}
diff --git a/src/drivers/WinAPI/Fl_WinAPI_System_Driver.H b/src/drivers/WinAPI/Fl_WinAPI_System_Driver.H
index b4189da4a..d4b12fc34 100644
--- a/src/drivers/WinAPI/Fl_WinAPI_System_Driver.H
+++ b/src/drivers/WinAPI/Fl_WinAPI_System_Driver.H
@@ -1,19 +1,19 @@
//
// "$Id$"
//
-// Definition of Windows system driver for the Fast Light Tool Kit (FLTK).
+// Windows system driver for the Fast Light Tool Kit (FLTK).
//
-// Copyright 2010-2018 by Bill Spitzak and others.
+// Copyright 2010-2020 by Bill Spitzak and others.
//
// This library is free software. Distribution and use rights are outlined in
// the file "COPYING" which should have been included with this file. If this
// file is missing or damaged, see the license at:
//
-// http://www.fltk.org/COPYING.php
+// https://www.fltk.org/COPYING.php
//
// Please report all bugs and problems on the following page:
//
-// http://www.fltk.org/str.php
+// https://www.fltk.org/str.php
//
/**
@@ -46,7 +46,7 @@ public:
virtual void fatal(const char *format, va_list args);
virtual char *utf2mbcs(const char *s);
virtual char *getenv(const char *var);
- virtual int putenv(char *var) {return _putenv(var);} // *FIXME* needs string conversion
+ virtual int putenv(const char *var);
virtual int open(const char *fnam, int oflags, int pmode);
virtual int open_ext(const char *fnam, int binary, int oflags, int pmode);
virtual FILE *fopen(const char *fnam, const char *mode);
diff --git a/src/drivers/WinAPI/Fl_WinAPI_System_Driver.cxx b/src/drivers/WinAPI/Fl_WinAPI_System_Driver.cxx
index b18b9c98e..10c6a7b38 100644
--- a/src/drivers/WinAPI/Fl_WinAPI_System_Driver.cxx
+++ b/src/drivers/WinAPI/Fl_WinAPI_System_Driver.cxx
@@ -1,19 +1,19 @@
//
// "$Id$"
//
-// Definition of Apple Darwin system driver.
+// Definition of Windows system driver for the Fast Light Tool Kit (FLTK).
//
-// Copyright 1998-2018 by Bill Spitzak and others.
+// Copyright 1998-2020 by Bill Spitzak and others.
//
// This library is free software. Distribution and use rights are outlined in
// the file "COPYING" which should have been included with this file. If this
// file is missing or damaged, see the license at:
//
-// http://www.fltk.org/COPYING.php
+// https://www.fltk.org/COPYING.php
//
// Please report all bugs and problems on the following page:
//
-// http://www.fltk.org/str.php
+// https://www.fltk.org/str.php
//
#include "../../config_lib.h"
@@ -196,6 +196,17 @@ char *Fl_WinAPI_System_Driver::getenv(const char *var) {
return wchar_to_utf8(ret, buf);
}
+int Fl_WinAPI_System_Driver::putenv(const char *var) {
+ unsigned len = (unsigned)strlen(var);
+ unsigned wn = fl_utf8toUtf16(var, len, NULL, 0) + 1; // Query length
+ wchar_t *wbuf = (wchar_t *)malloc(sizeof(wchar_t) * wn);
+ wn = fl_utf8toUtf16(var, len, (unsigned short *)wbuf, wn);
+ wbuf[wn] = 0;
+ int ret = _wputenv(wbuf);
+ free(wbuf);
+ return ret;
+}
+
int Fl_WinAPI_System_Driver::open(const char *fnam, int oflags, int pmode) {
utf8_to_wchar(fnam, wbuf);
if (pmode == -1) return _wopen(wbuf, oflags);
diff --git a/src/fl_utf8.cxx b/src/fl_utf8.cxx
index c0f681e49..05ca16dac 100644
--- a/src/fl_utf8.cxx
+++ b/src/fl_utf8.cxx
@@ -5,17 +5,17 @@
//
// Author: Jean-Marc Lienher ( http://oksid.ch )
// Copyright 2000-2010 by O'ksi'D.
-// Copyright 2016-2017 by Bill Spitzak and others.
+// Copyright 2016-2020 by Bill Spitzak and others.
//
// This library is free software. Distribution and use rights are outlined in
// the file "COPYING" which should have been included with this file. If this
// file is missing or damaged, see the license at:
//
-// http://www.fltk.org/COPYING.php
+// https://www.fltk.org/COPYING.php
//
// Please report all bugs and problems on the following page:
//
-// http://www.fltk.org/str.php
+// https://www.fltk.org/str.php
//
#include <FL/Fl.H>
@@ -314,6 +314,38 @@ char *fl_getenv(const char* v) {
}
+/** Cross-platform function to write environment variables with a UTF-8
+ encoded name or value.
+
+ This function is especially useful on the Windows platform where
+ non-ASCII environment variables are encoded as wide characters.
+
+ The given argument \p var must be encoded in UTF-8 in the form "name=value".
+ The \p 'name' part must conform to platform dependent restrictions on
+ environment variable names.
+
+ The string given in \p var is copied and optionally converted to the
+ required encoding for the platform. On platforms other than Windows
+ this function calls putenv directly.
+
+ The return value is zero on success and non-zero in case of error.
+ The value in case of error is platform specific and returned as-is.
+
+ \note The copied string is allocated on the heap and "lost" on some platforms,
+ i.e. calling fl_putenv() to change environment variables frequently may cause
+ memory leaks. There may be an option to avoid this in a future implementation.
+
+ \note This function is not thread-safe.
+
+ \param[in] var the UTF-8 encoded environment variable \p 'name=value'
+ \return 0 on success, non-zero in case of error.
+*/
+
+int fl_putenv(const char* var) {
+ return Fl::system_driver()->putenv(var);
+}
+
+
/** Cross-platform function to open files with a UTF-8 encoded name.
This function is especially useful on the Windows platform where the
@@ -579,11 +611,6 @@ void fl_make_path_for_file( const char *path ) {
free( p );
} // fl_make_path_for_file()
-
-//============================================================
-// this part comes from file src/fl_utf.c of FLTK 1.3
-//============================================================
-
/** Set to 1 to turn bad UTF-8 bytes into ISO-8859-1. If this is zero
they are instead turned into the Unicode REPLACEMENT CHARACTER, of
value 0xfffd.
@@ -1331,10 +1358,6 @@ unsigned fl_utf8from_mb(char* dst, unsigned dstlen, const char* src, unsigned sr
return Fl::system_driver()->utf8from_mb(dst, dstlen, src, srclen);
}
-//============================================================
-// end of the part from file src/fl_utf.c of FLTK 1.3
-//============================================================
-
/** @} */
//
diff --git a/test/demo.cxx b/test/demo.cxx
index 0b72715a1..ddee12234 100644
--- a/test/demo.cxx
+++ b/test/demo.cxx
@@ -3,7 +3,7 @@
//
// Main demo program for the Fast Light Tool Kit (FLTK).
//
-// Copyright 1998-2019 by Bill Spitzak and others.
+// Copyright 1998-2020 by Bill Spitzak and others.
//
// This library is free software. Distribution and use rights are outlined in
// the file "COPYING" which should have been included with this file. If this
@@ -20,23 +20,6 @@
#include <string.h>
#include <stdlib.h>
-// *FIXME*
-// Implement fl_putenv(). Then remove the following comment
-// and the #define's of fl_putenv below
-
-// Visual C++ 2005 incorrectly displays a warning about the use of
-// POSIX APIs on Windows, which is supposed to be POSIX compliant...
-
-#if defined(_MSC_VER)
-# define fl_putenv _putenv
-#else
-# define fl_putenv putenv
-#endif // _MSC_VER
-
-// *FIXME* To do:
-// Check whether '#include <unistd.h>' can be removed since chdir()
-// has been replaced with fl_chdir() (AlbrechtS, Nov 12, 2017)
-
#if defined __APPLE__
#include <ApplicationServices/ApplicationServices.h>
#endif
@@ -459,7 +442,7 @@ int load_the_menu(char* fname) {
}
int main(int argc, char **argv) {
- fl_putenv((char *)"FLTK_DOCDIR=../documentation/html");
+ fl_putenv("FLTK_DOCDIR=../documentation/html");
char buf[FL_PATH_MAX];
strcpy(buf, argv[0]);
#if DEBUG_EXE_WITH_D