Пример #1
0
/*  
    Function to iterate and return the next element value.
    NOTE: this is not a method of Array. Rather, it is a callback function for Iterator
 */
static EjsObj *nextValue(Ejs *ejs, EjsIterator *ip, int argc, EjsObj **argv)
{
    EjsFile     *fp;

    fp = (EjsFile*) ip->target;
    if (!ejsIs(ejs, fp, File)) {
        ejsThrowReferenceError(ejs, "Wrong type");
        return 0;
    }

    if (ip->index < fp->info.size) {
#if !ME_CC_MMU || 1
        if (mprSeekFile(fp->file, SEEK_CUR, 0) != ip->index) {
            if (mprSeekFile(fp->file, SEEK_SET, ip->index) != ip->index) {
                ejsThrowIOError(ejs, "Cannot seek to %d", ip->index);
                return 0;
            }
        }
        ip->index++;
        return (EjsObj*) ejsCreateNumber(ejs, mprGetFileChar(fp->file));
#else
        return (EjsObj*) ejsCreateNumber(ejs, fp->mapped[ip->index++]);
#endif
    }

#if ME_CC_MMU && FUTURE
    unmapFile(fp);
    fp->mapped = 0;
#endif

    ejsThrowStopIteration(ejs);
    return 0;
}
Пример #2
0
readBuffer::~readBuffer() {

  delete [] _filename;

  if (_mmap)
    unmapFile(_buffer, _bufferLen);
  else
    delete [] _buffer;

  if (_stdin == false)
    close(_file);
}
Пример #3
0
void KmxController::UpdateMotionParams(){
  char fileName[256];
  absolutePath(settings_file_, fileName);

  MappedFile mmFile;
  if(mmapNamedFile(mmFile, fileName)){
    log_info("Failed to map file: %s", fileName);
    return;
  }
  SetMotionParams(mmFile.addr, mmFile.filesize);

  unmapFile(mmFile);
}
Пример #4
0
int WebController::Setup(){
  log_info("Initialize");
  char fileName[256];
  absolutePath("settings/setup.cnf", fileName);
  MappedFile mmFile;
  if(mmapNamedFile(mmFile, fileName)){
    log_info("Failed to read setup file");
    return -1;
  }

  json_token *setup = NULL;
  setup = parse_json2(mmFile.addr, mmFile.filesize);

  json_str(setup,"machine",settings_file_);
  json_str(setup,"gcodefile",current_gcode_file_);

  free(setup);
  unmapFile(mmFile);
  return 0;
}
Пример #5
0
/*  
    Close the file and free up all associated resources.
    function close(): void
 */
static EjsObj *closeFile(Ejs *ejs, EjsFile *fp, int argc, EjsObj **argv)
{
    if (fp->mode & EJS_FILE_OPEN && fp->mode & EJS_FILE_WRITE) {
        if (mprFlushFile(fp->file) < 0) {
            if (ejs) {
                ejsThrowIOError(ejs, "Cannot flush file data");
            }
            return 0;
        }
    }
    if (fp->file) {
        mprCloseFile(fp->file);
        fp->file = 0;
    }
#if ME_CC_MMU && FUTURE
    if (fp->mapped) {
        unmapFile(fp);
        fp->mapped = 0;
    }
#endif
    fp->mode = 0;
    fp->modeString = 0;
    return 0;
}
Пример #6
0
int
main(int argc, char **argv) {
  size_t   lw;
  uint32  *ww = 0L;
  uint32   idx = 0;
  uint32   err = 0;
  FILE    *out;
  uint32   blockSize = 1048576;
  uint32   numBlocks = 32;

  if (argc == 2)
    numBlocks = strtouint32(argv[1], 0L);

  //  The file must exist, and it must be large enough to contain all
  //  that we want to write.  So, we create the file and fill it with
  //  junk.
  //
  ww  = (uint32 *)malloc(sizeof(uint32) * blockSize);
  if (ww == NULL) {
    fprintf(stderr, "can't allocate %d uint32's for clearing the file.\n", blockSize);
    exit(1);
  }
  errno = 0;
  out = fopen("mmap.test.junk", "w");
  if (errno) {
    fprintf(stderr, "can't open 'mmap.test.junk' to fill with junk: %s\n", strerror(errno));
    exit(1);
  }
  for (idx=0; idx<numBlocks; idx++) {
    fprintf(stderr, "Writing initial blocks: "uint32FMT"/"uint32FMT"\r", idx, numBlocks), fflush(stderr);
    fwrite(ww, sizeof(uint32), 1048576, out);
    if (errno) {
      fprintf(stderr, "can't write to 'mmap.test.junk': %s\n", strerror(errno));
      exit(1);
    }
  }
  fclose(out);
  free(ww);
  fprintf(stderr, "\n");

  //  Now, map it, and fill it with real data.
  //
  ww = (uint32 *)mapFile("mmap.test.junk", &lw, 'w');
  for (idx=0; idx<numBlocks * blockSize; idx++) {
    if ((idx & 0xfff) == 0)
      fprintf(stderr, "Writing: "uint32FMT"/"uint32FMT"\r", idx, numBlocks * blockSize), fflush(stderr);
    ww[idx] = idx;
  }
  unmapFile(ww, lw);
  fprintf(stderr, "\n");

  //  Map again, and check the data.
  //
  ww = mapFile("mmap.test.junk", &lw, 'r');
  for (idx=0; idx<numBlocks * blockSize; idx++) {
    if ((idx & 0xfff) == 0)
      fprintf(stderr, "Verifying: "uint32FMT"/"uint32FMT"\r", idx, numBlocks * blockSize), fflush(stderr);
    if (ww[idx] != idx)
      err++;
  }
  unmapFile(ww, lw);
  fprintf(stderr, "\n");

  unlink("mmap.test.junk");

  return (err != 0);  
}