That is just what I did here https://github.com/mingodad/tinycc I called it
virtual_io.

If you checkout it and execute the script "mk-it" it will create a self
contained tinycc, the same principle can be used to use it as a library.

Cheers !


On Tue, Apr 15, 2014 at 4:58 PM, Josip Habjan <[email protected]> wrote:

> Hi,
>
> Is there a chance to add a simple function that will allow users to load
> all required files from memory? For instance: I made a .NET wrapper for
> tcclib and I have all 'lib' and 'include' files compiled in a single .NET
> dll as embedded resources. Now, I don't want to export embedded files out
> from the dll to disk, and then run compiled, instead i would like compiler
> to ask me where he can find requested file. (In my case I want it to be
> read from memory).
>
> What I'm thinking is some function that will allow file redirection. This
> would be easy for non windows systems which supports "fmemopen" function as
> they can easily redirect some file request to the file descriptor from
> memory returned by "fmemopen". For windows users the only option is a
> memory buffer.
>
> What I had in mind is something like:
>
> typedef struct RedirectedFile
> {
> int fd;
> void *buf;
>  unsigned long buflen;
> unsigned char filename[1024];
> } RedirectedFile;
>
> LIBTCCAPI void tcc_set_file_redirection_func(TCCState *s,
> int(*get_file_func)(const char *filename, RedirectedFile *rf),
> void(*release_file_func)(void* buff, int fd));
>
> typedef struct FileOrBuffer
> {
> int state;
> int is_fd;
> int fd;
>  void *buf;
> unsigned long len;
> unsigned long pos;
>  int is_redirect;
> } FileOrBuffer;
>
> typedef struct BufferedFile {
> ...   FileOrBuffer fob;  /* ex: int fd */
>     ...
> } BufferedFile;
>
> and then in tcc_open before opening a file I would simply call
> "get_file_func"...
>
> I know this takes a lot of changes for all file reads and seeks, but..
> actualy, it's not that complicated as I had to replace and add only 2
> functions:
>
> PUB_FUNC int tcc_fobread(FileOrBuffer *fob, void *dest, int size)
> {
> if (fob->is_fd)
> return read(fob->fd, dest, size);
> else {
>  long available = fob->len - fob->pos;
>
> if (size > available) {
> size = available;
>  }
>
> memcpy(dest, (char*)fob->buf + fob->pos, size);
>
> fob->pos += size;
>
> return size;
> }
> }
>
> PUB_FUNC long tcc_foblseek(FileOrBuffer *fob, long offset, int origin)
> {
> if (fob->is_fd)
> return lseek(fob->fd, offset, origin);
> else {
>  long pos;
> switch (origin) {
> case SEEK_SET: {
>  if (offset >= 0) {
> pos = offset;
> }
> else {
>  pos = 0;
> }
> break;
> }
>  case SEEK_CUR: {
> if (offset >= 0 || -offset <= fob->pos) {
> pos = fob->pos + offset;
>  }
> else {
> pos = 0;
> }
>  break;
> }
> case SEEK_END: {
> pos = fob->len + offset;
>  break;
> }
> default:
> return -1;
>  }
>
> if (pos > fob->len) {
> return -1;
>  }
>
> fob->pos = pos;
> return pos;
>  }
> }
>
> C is not really familiar to me, I'm more .NET oriented, but I gave it a
> try and came up with the solution. Here is a complete source code that
> works:
>
> https://www.dropbox.com/s/4y30qrejkmocvqn/tcc-src.zip
>
> Thanks,
> jhabjan
>
> _______________________________________________
> Tinycc-devel mailing list
> [email protected]
> https://lists.nongnu.org/mailman/listinfo/tinycc-devel
>
>
_______________________________________________
Tinycc-devel mailing list
[email protected]
https://lists.nongnu.org/mailman/listinfo/tinycc-devel

Reply via email to