1

I try to integrate lua to my project (lua 5.2.1) And i have no problem to compile it.

But my problem is my project use my own system for read/write file from the file system.

So I start to modify lua for replacing each call of fopen / fclose / fread / fwrite ...

But the problem is Lua is too much mix if stdio fct ans use some FILE function I don't have equivalent in my project (and no so easy to reimplement), like : ungetc setvbuff

And so on....

My question ^^ Some aleeady try to do that ? And if yes how ? Does someone now a extension of lua who have this functionality (some c library use callback fct to ask open/close a file) ?

It's seams strange to me that lua who is really use on multi-platfroms os or even embeded system make a so strong use of std lib, abitualy for really cross platform lib every type and fct from std are typedefed for easy platfroms specifics change.

Thanks for any help you can give me :)

user1482649
  • 45
  • 1
  • 7
  • Not sure, but I think you skipped [I/O Library in PiL](http://www.lua.org/pil/21.1.html). – hjpotter92 Aug 28 '12 at 17:53
  • Lua aims at freestanding C for the core but the Lua standard libraries cannot do that, because their goal is to export facilities to Lua programs, including what is available in the C standard library. See http://stackoverflow.com/questions/4998822/what-parts-of-c-are-most-portable/5002994#5002994 – lhf Aug 28 '12 at 17:54
  • "It's seams strange to me that lua who is really use on multi-platfroms os or even embeded system make a so strong use of std lib" Why? Considering that the C standard library is *standard*, that means that if you have a C compiler, odds are pretty good you have access to the C standard library that comes with virtually every C compiler. Thus, Lua should be as cross-platform as C is. If you're working in an environment that doesn't support the C standard library, then its up to you to deal with that kind of non-standard environment. – Nicol Bolas Aug 28 '12 at 18:03
  • of course C standard library is working everywhere you can compile C, but the implementation and performance could change with the compiler or the platform, so typedef the standard type and #define standard function could let the user of the lib "overriding" your library usage of the standard library by his need. for example ogg lib re-define malloc with ogg_alloc, and use in all his code ogg_alloc when he need memory, if you recompile ogg for your project, and you want to use a memory analyzer by looking every call of malloc/free, you can change the define of ogg_alloc by your own function – user1482649 Aug 28 '12 at 19:09
  • instead of calling directly malloc, what I will expect from lua is to don't use directly fopen in is code, but to use a define lua_fopen who will be define as fopen until you change it because your platform required more than a normal fopen for example. and same for every other use of other standard function. ps : fct is function, sorry for the shortcut – user1482649 Aug 28 '12 at 19:11

1 Answers1

3

You're not asking about "Lua the language's" file IO; you're talking about "Lua the C library's" file IO. This was not clear from the question.

Lua has provisions for this. luaL_loadfile, for example, is just syntactic sugar around a function that opens the given file, loads it, closes the file, and then calls luaL_loadstring on it.

If you're in an environment where file IO has to go through different channels, you shouldn't be trying to make luaL_loadfile work with the new file IO. You should be writing a new function which loads a file from your filesystem and calls luaL_loadstring.

In short: you shouldn't be using any Lua APIs that require files. Lua's file-based functions are convenience functions; nothing more. Lua itself already has provisions for this; every file-based API has a non-file-based version that can work just as well. You should not be modifying Lua itself.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • my own system for manage file is write in C++, I don't want to replace the lua IO lib, what I want is more in C after open a lua_State, I call luaL_loadFile( L, "myfile.lua" ), and this function, inside the lua library, use fopen and I want to replace fopen (who is a in the c std ) by : MyFunctionToOpenAFile(...) again, is working for simple file function like open/fclose/fread/fwrite .... But Lua use some standard File function I don't support for File like : ungetc (for what I could find this function re-push the character you just read in the file with getc ) – user1482649 Aug 28 '12 at 19:00