# HG changeset patch
# User Júlio Hoffimann <julio.hoffimann@gmail.com>
# Date 1358207444 10800
# Node ID a62e0c5329259e560381ec4c31e46de2bc2a6451
# Parent  cb47c8cc105d62753e76703125060f271638fd91
Added core functions: locale() and setlocale()

diff -r cb47c8cc105d -r a62e0c532925 libinterp/corefcn/locale.cc
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libinterp/corefcn/locale.cc	Mon Jan 14 20:50:44 2013 -0300
@@ -0,0 +1,140 @@
+/*
+
+Copyright (C) 2012 Júlio Hoffimann
+
+This file is part of Octave.
+
+Octave is free software; you can redistribute it and/or modify it
+under the terms of the GNU General Public License as published by the
+Free Software Foundation; either version 3 of the License, or (at your
+option) any later version.
+
+Octave is distributed in the hope that it will be useful, but WITHOUT
+ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+for more details.
+
+You should have received a copy of the GNU General Public License
+along with Octave; see the file COPYING.  If not, see
+<http://www.gnu.org/licenses/>.
+
+*/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <string>
+#include <locale>
+#include <stdexcept>
+
+#include "defun.h"
+#include "error.h"
+#include "gripes.h"
+#include "oct-obj.h"
+#include "utils.h"
+
+DEFUN (locale, args, ,
+  "-*- texinfo -*-\n\
+@deftypefn  {Built-in Function} {@var{l} =} locale ()\n\
+@deftypefnx {Built-in Function} {@var{bool} =} locale (@var{l})\n\
+Get the user's locale or check if the specified one is installed\n\
+on the system.\n\
+\n\
+On GNU/Linux you can check the installed locales by issuing @code{locale -a}\n\
+at the shell.\n\
+\n\
+@example\n\
+@group\n\
+locale ()\n\
+   @result{} \"en_US.UTF-8\"\n\
+\n\
+locale (\"pt_BR\")\n\
+   @result{} true\n\
+\n\
+locale (\"fr_FR\")\n\
+   @result{} false\n\
+@end group\n\
+@end example\n\
+@seealso{setlocale}\n\
+@end deftypefn")
+{
+  int nargin = args.length ();
+  octave_value retval;
+
+  if (nargin > 1)
+    print_usage ();
+
+  if (nargin == 0)
+  {
+    retval = std::locale("").name ();
+  }
+  else
+  {
+    try
+    {
+      std::string loc_name = args(0).string_value ();
+      std::locale _(loc_name.c_str ());
+
+      retval = true;
+
+      // the empty string represents the user's locale
+      // in C++, this should be hidden from the end user
+      if (loc_name.empty ())
+        retval = false;
+    }
+    catch (std::runtime_error& e)
+    {
+      retval = false;
+    }
+  }
+  return retval;
+}
+
+
+/*
+%!assert (~isempty(locale ()))
+%!assert (locale (locale ()))
+*/
+
+
+DEFUN (setlocale, args, ,
+  "-*- texinfo -*-\n\
+@deftypefn {Built-in Function} {@var{old} =} setlocale (@var{new})\n\
+Set the locale and return the previous.\n\
+@example\n\
+@group\n\
+setlocale (locale ())\n\
+   @result{} \"C\"\n\
+\n\
+setlocale (locale ())\n\
+   @result{} \"en_US.UTF-8\"\n\
+@end group\n\
+@end example\n\
+@seealso{locale}\n\
+@end deftypefn")
+{
+  int nargin = args.length ();
+  octave_value retval;
+
+  retval = std::locale().name ();
+
+  if (nargin != 1)
+    print_usage ();
+
+  if (Flocale (args, 1)(0).bool_value ())
+    // Replaces the global C++ locale, which means all future calls to std::locale
+    // default constructor will return a copy of this locale. It also replaces
+    // the C locale as if by std::setlocale(LC_ALL, loc_name.c_str())
+    std::locale::global(std::locale(args(0).string_value ().c_str ()));
+  else
+    error ("setlocale: requested locale is not installed on your system");
+
+  return retval;
+}
+
+
+/*
+%!assert (setlocale (locale ()), "C")
+%!assert (setlocale (locale ()), locale())
+ */
diff -r cb47c8cc105d -r a62e0c532925 libinterp/corefcn/module.mk
--- a/libinterp/corefcn/module.mk	Sun Jan 13 11:29:50 2013 -0800
+++ b/libinterp/corefcn/module.mk	Mon Jan 14 20:50:44 2013 -0300
@@ -60,6 +60,7 @@
   corefcn/hex2num.cc \
   corefcn/inv.cc \
   corefcn/kron.cc \
+  corefcn/locale.cc \
   corefcn/lookup.cc \
   corefcn/lsode.cc \
   corefcn/lu.cc \
diff -r cb47c8cc105d -r a62e0c532925 libinterp/interpfcn/file-io.cc
--- a/libinterp/interpfcn/file-io.cc	Sun Jan 13 11:29:50 2013 -0800
+++ b/libinterp/interpfcn/file-io.cc	Mon Jan 14 20:50:44 2013 -0300
@@ -1348,7 +1348,14 @@
 /*
 %!test
 %! assert (sscanf ("1,2", "%f", "C"), 1)
-%! assert (sscanf ("1,2", "%f", "fr_FR"), 1.2)
+%!
+%! if (locale ("pt_BR"))
+%!   assert (sscanf ("1,2", "%f", "pt_BR"), 1.2)
+%! endif
+%!
+%! if (locale ("fr_FR"))
+%!   assert (sscanf ("1,2", "%f", "fr_FR"), 1.2)
+%! endif
 */
 
 DEFUN (scanf, args, nargout,
