Re:Locale problem

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

Re:Locale problem

marcus.cf
I tried the following and it worked on Linux. It didn't 
work on Windows probably because the C compiler didn't 
support locales the same way Linux did (don't remember 
if it was me who compiled my Windows version of Lua)

os.setlocale('pt_BR', 'numeric')
io.read('*n') -- accepts the number 5,6 and not 5.6

os.setlocale('C', 'numeric')
io.read('*n') -- back to '.'

> Hi, All.
> I have the next problem:
> I use Lua 5.0 and I want my code to not be dependented
> by locale "decimal symbol".
> I decide to use the point as decimal symbol, but if in
> Windows is chosen comma as decimal symbol, Lua show
> error...
> I want to change locale settings in my program at the
> begining and when Lua finish, to restore settings for
> Number:decimal symbols...
> I see that I have to use
> os.setlocale("numeric","..."), but I dont know exactly
> how:-(
> Can you anybody help me?

 
__________________________________________________________________________
Acabe com aquelas janelinhas que pulam na sua tela.
AntiPop-up UOL - É grátis!
http://antipopup.uol.com.br/



Reply | Threaded
Open this post in threaded view
|

Re: Locale problem

marcos-2
marcus.cf wrote:
I tried the following and it worked on Linux. It didn't work on Windows probably because the C compiler didn't support locales the same way Linux did (don't remember if it was me who compiled my Windows version of Lua)

os.setlocale('pt_BR', 'numeric')
io.read('*n') -- accepts the number 5,6 and not 5.6

os.setlocale('C', 'numeric')
io.read('*n') -- back to '.'


On Windows :
os.setlocale('Portuguese - Brazil')
io.read('*n') -- accepts the number 5,6 and not 5.6

os.setlocale('English')
io.read('*n') -- back to '.'

--
Marcos

Reply | Threaded
Open this post in threaded view
|

loadlib question

jose marin2
In reply to this post by marcus.cf
Hello.

loadlib uses Window's LoadLibrary to load the lib's
dll, but doesn't perform any FreeLibrary.

Is this a problem?


______________________________________________________________________

Conheça a nova central de informações anti-spam do Yahoo! Mail:
http://www.yahoo.com.br/antispam

Reply | Threaded
Open this post in threaded view
|

RE: loadlib question

Virgil Smith
That depends on how you plan to use the DLL.  Perhaps you will never call
the DLL.  After all once you load a DLL it's code is free to do anything it
"wants" as part of your process (such as spawn a thread and keep running).

It's amazingly simple to write your own library loading system for Lua if
you so choose, and I have to admit that I'm not a loadlib expert because I
wrote my own system (originally using Lua 4.1).  However, that gave me a
certain perspective when I discovered loadlib.

I, like you, was surprised by the lack of an unload (i.e. a GC) mechanism.
Personally, I assumed that a library load operation should return a table of
functions available from the library and so loadlib returning "whatever"
surprised me a bit.  This also brought up, for me, the issue that tables do
not get to process a GC event.  Userdata's do, but tables don't.

The "solution" to this problem is extremely simple.  It is up to the library
author to handle the issue.  How?  The library returns whatever value makes
sense for that particular library.  If that happens to be a "table" of
callable functions then it can return that table.  If the library should be
"Freed" when no longer needed then it should return a userdata whose __gc
metamethod handles all of the clean up details.  To associate a table of
functions with such a userdata simply make a table of functions and then set
it as the __index metamethod for the userdata (or use a function to do the
lookup in C).

At least that is my understanding of the situation.  I hope this helps.

Minor Warning:  I've never tried calling FreeLibrary from inside the code
for the DLL itself so I don't <actually> know that a DLL CAN clean up after
itself inside a GC event handler.



-----Original Message-----
From: [hidden email]
[[hidden email] Behalf Of Jose Marin
Sent: Tuesday, January 13, 2004 12:29 PM
To: Lua list
Subject: loadlib question


Hello.

loadlib uses Window's LoadLibrary to load the lib's
dll, but doesn't perform any FreeLibrary.

Is this a problem?


______________________________________________________________________

Conheça a nova central de informações anti-spam do Yahoo! Mail:
http://www.yahoo.com.br/antispam



Reply | Threaded
Open this post in threaded view
|

RE: Locale problem

Tai Meng
In reply to this post by marcus.cf
You might want to be careful about the ',' separator. For example,

os.setlocale('Portuguese - Brazil')
num = 5,6 --> num has a value of 5, though 5.6 might have been expected
num = tonumber( "5,6" ) --> num has a value of 5.6

There are other places too like specifying table fields i.e. t = {1,23} vs t = {tonumber( "1,23" )}.

Tai

-----Original Message-----
From: Marcos E. Wurzius [[hidden email]]
Sent: Tuesday, January 13, 2004 8:11 AM
To: Lua list
Subject: Re: Locale problem


marcus.cf wrote:
> I tried the following and it worked on Linux. It didn't 
> work on Windows probably because the C compiler didn't 
> support locales the same way Linux did (don't remember 
> if it was me who compiled my Windows version of Lua)
> 
> os.setlocale('pt_BR', 'numeric')
> io.read('*n') -- accepts the number 5,6 and not 5.6
> 
> os.setlocale('C', 'numeric')
> io.read('*n') -- back to '.'
> 

On Windows :
os.setlocale('Portuguese - Brazil')
io.read('*n') -- accepts the number 5,6 and not 5.6

os.setlocale('English')
io.read('*n') -- back to '.'

-- 
Marcos

Reply | Threaded
Open this post in threaded view
|

RE: Locale problem

marcus.cf
In reply to this post by marcus.cf
> On Windows :
> os.setlocale('Portuguese - Brazil')
> io.read('*n') -- accepts the number
5,6 and not 5.6
> 
> os.setlocale('English')
> io.read('*n') -- back to '.'
> 
> -- 
> Marcos
> 

Wow! I would never guess that! Thanks
for the information!!


> You might want to be careful about the ',' separator. For example,
> 
> os.setlocale('Portuguese - Brazil')
> num = 5,6 --> num has a value of 5, though 5.6 might have been expected
> num = tonumber( "5,6" ) --> num has a value of 5.6
> 
> There are other places too like specifying table fields i.e. t = {1,23} vs t = {tonumber( "1,23" )}.
> 
> Tai

Sure. I don't expect the language to
change with os.setlocale. That would
be a mess. os.setlocale only changes
the library behavior.

 
__________________________________________________________________________
Acabe com aquelas janelinhas que pulam na sua tela.
AntiPop-up UOL - É grátis!
http://antipopup.uol.com.br/



Reply | Threaded
Open this post in threaded view
|

RE: Locale problem

Stoil Todorov
Tank You, guys
But I don't want to change all local settings:-(
I want to change only digital sign - I want to be sure
 that digital sign will be point during executing Lua
code...
Is it possible?

--- "marcus.cf" <[hidden email]> wrote:
> > On Windows :
> > os.setlocale('Portuguese - Brazil')
> > io.read('*n') -- accepts the number
> 5,6 and not 5.6
> >
> > os.setlocale('English')
> > io.read('*n') -- back to '.'
> >
> > --
> > Marcos
> >
> 
> Wow! I would never guess that! Thanks
> for the information!!
> 
> 
> > You might want to be careful about the ','
> separator. For example,
> >
> > os.setlocale('Portuguese - Brazil')
> > num = 5,6 --> num has a value of 5, though 5.6
> might have been expected
> > num = tonumber( "5,6" ) --> num has a value of 5.6
> >
> > There are other places too like specifying table
> fields i.e. t = {1,23} vs t = {tonumber( "1,23" )}.
> >
> > Tai
> 
> Sure. I don't expect the language to
> change with os.setlocale. That would
> be a mess. os.setlocale only changes
> the library behavior.
> 
> 
>
__________________________________________________________________________
> Acabe com aquelas janelinhas que pulam na sua tela.
> AntiPop-up UOL - É grátis!
> http://antipopup.uol.com.br/
> 
> 


__________________________________
Do you Yahoo!?
Yahoo! Hotjobs: Enter the "Signing Bonus" Sweepstakes
http://hotjobs.sweepstakes.yahoo.com/signingbonus

Reply | Threaded
Open this post in threaded view
|

Re: Locale problem

David Jones-2

On Jan 15, 2004, at 19:00, Stoil Todorov wrote:

Tank You, guys
But I don't want to change all local settings:-(
I want to change only digital sign - I want to be sure
 that digital sign will be point during executing Lua
code...
Is it possible?

I think so:

os.setlocale("C", "numeric")

or you can do it from C:

setlocale(LC_NUMERIC, "C")

Technically this also changes some other aspects of number formatting, such as the thousands grouping character. See the reference for the C function localeconv. Standard C does not use the other aspects of number formatting, but your host application or the operating system might.

Note that os.setlocale can be used to retrieve the current locale settings as well. So if you want to switch between the current locale and C do something like this:

numeric_locale = os.setlocale(nil, 'numeric')

os.setlocale('pl_PL', 'numeric')

-- do stuff using comma as the decimal point (at least on Mac OS X 10.3).

-- restore the old locale setting

os.setlocale(numeric_locale, 'numeric')

I have to say that this feature is not well documented, but obvious enough if you know the C side of things.

David Jones


Reply | Threaded
Open this post in threaded view
|

Re: Locale problem

Stoil Todorov
First, thanks for support!:-)
Second I still have problems with locals:-(
In code, I call os.setlocale("C", "numeric") in first
line, because I use data with decimal point, but I get
error message: "Syntax error during pre-compilation.:
[string "Run_Code"]:144: malformed number near `0.05'"
How can I avoid this?

--- David Jones <[hidden email]> wrote:
> 
> On Jan 15, 2004, at 19:00, Stoil Todorov wrote:
> 
> > Thank You, guys
> > But I don't want to change all local settings:-(
> > I want to change only digital sign - I want to be
> sure
> >  that digital sign will be point during executing
> Lua
> > code...
> > Is it possible?
> 
> I think so:
> 
> os.setlocale("C", "numeric")
> 
> or you can do it from C:
> 
> setlocale(LC_NUMERIC, "C")
> 
> Technically this also changes some other aspects of
> number formatting, 
> such as the thousands grouping character.  See the
> reference for the C 
> function localeconv.  Standard C does not use the
> other aspects of 
> number formatting, but your host application or the
> operating system 
> might.
> 
> Note that os.setlocale can be used to retrieve the
> current locale 
> settings as well.  So if you want to switch between
> the current locale 
> and C do something like this:
> 
> numeric_locale = os.setlocale(nil, 'numeric')
> 
> os.setlocale('pl_PL', 'numeric')
> 
> -- do stuff using comma as the decimal point (at
> least on Mac OS X 
> 10.3).
> 
> -- restore the old locale setting
> 
> os.setlocale(numeric_locale, 'numeric')
> 
> I have to say that this feature is not well
> documented, but obvious 
> enough if you know the C side of things.
> 
> David Jones
> 


__________________________________
Do you Yahoo!?
Yahoo! Finance: Get your refund fast by filing online.
http://taxes.yahoo.com/filing.html

Reply | Threaded
Open this post in threaded view
|

Re: Locale problem

David Jones-2

On Feb 05, 2004, at 17:23, Stoil Todorov wrote:

First, thanks for support!:-)
Second I still have problems with locals:-(
In code, I call os.setlocale("C", "numeric") in first
line, because I use data with decimal point, but I get
error message: "Syntax error during pre-compilation.:
[string "Run_Code"]:144: malformed number near `0.05'"
How can I avoid this?

I'm not entirely sure, but I think the following is what is happening:

You need to make sure that the locale is changed before the Lua chunk is compiled.

This is probably best achieved by calling

setlocale(LC_NUMERIC, "C")

in your C program before loading/compiling/running any Lua code. You'll need to #include <locale.h> as well. Early on in main() is probably a good place.

If you must do it in Lua then you need to execute

os.setlocale('c', 'numeric')

in a separate chunk to the rest of your code. I don't how your Lua code and your host application are organised, but putting that in a separate file or string that is executed before other Lua code should work.

It doesn't work to put os.setlocale('c', 'numeric') in the Lua file that you want to use decimal points in. That's because the file has been lexed and compiler before the locale has changed, so the lexer - the part that recognises numbers - will use the old locale. I should have spotted that before.

Hope this helps.

David Jones