Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 9th, 2012  |  syntax: None  |  size: 0.57 KB  |  hits: 16  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Handle<Value> read(const Arguments &args)
  2.         {
  3.                 char *buffer = malloc(sizeof(buffer)*4096), *p;
  4.                 size_t i = 0, alloced = 4096;
  5.                 int c;
  6.                
  7.                 if(buffer == NULL)
  8.                         return ThrowException(String::New("Malloc buffer error"));
  9.  
  10.                 while((c=getc(file)) != EOF)
  11.                 {
  12.                         if(i + 2 == alloced)
  13.                         {
  14.                                 p = realloc(buffer, alloced + 4096);
  15.                                 if(p == NULL)
  16.                                 {
  17.                                         free(buffer);
  18.                                         return ThrowException(String::New("Read Memory error"));
  19.                                 }
  20.  
  21.                                 buffer = p;
  22.                                 alloced += 4096;
  23.                         }
  24.                         buffer[i++] = c;
  25.                 }
  26.                 buffer[i] = 0;
  27.                
  28.                 return String::New(buffer);
  29.         }