Rendering files from C++ Node.js addon -


i render files in node.js c++ addon. want apply file processing , render output browser via node.js

here c++ code

    std::ifstream in(filename, std::ios::binary);      in.seekg (0, in.end);     int length = in.tellg();     in.seekg (0, in.beg);      char * buffer = new char [length];     in.read (buffer,length);     in.close();      return buffer; 

following v8 code add bindings node.js, here buffer output above c++ code.

    local<function> cb = local<function>::cast(args[1]);     const unsigned argc = 1;     local<value> argv[argc] = {local<value>::new(string::new(buffer))};     cb->call(context::getcurrent()->global(), argc, argv); 

this code works normal text files. i'm getting problem when reading text files having unicode characters. eg,

original text file

test start billél last 

when receiving in node, get

test start bill�l last 

similarly when reading jpg, png files output file different original file. please help.

i having problems well. found implementation in v8 examples google. example found handles utf8 encoded files found here:

https://code.google.com/p/v8/source/browse/trunk/samples/shell.cc#218

i adapted source this:

const char* readfile(const char* filename, int* filesize) {     // reference c-string version of file     char *filebuffer = 0;      // attempt open file     file* fd = fopen(filename, "rb");      // clear file size     *filesize = 0;      // file valid     if(fd != 0)     {        // size of file        fseek(fd, 0, seek_end);        *filesize = ftell(fd);        rewind(fd);         // allocate file buffer file contents        filebuffer = (char*)malloc(*filesize + 1);        filebuffer[*filesize] = 0;         // copy file contents        (int charcount = 0; charcount < *filesize;)        {            int charread = static_cast<int>(fread(&filebuffer[charcount], 1, *filesize - charcount, fd));            charcount += charread;        }         // close file        fclose(fd);     }      return filebuffer; } 

also, make sure when create v8 string create string::utf8value.

string::utf8value v8utf8string(...); 

then use string::utf8value char* use following function:

https://code.google.com/p/v8/source/browse/trunk/samples/shell.cc#91


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -