Esempio n. 1
0
char *wbFile_read(wbFile_t file, size_t size, size_t count) {
  size_t res;
  char *buffer;
  size_t bufferLen;
  FILE *handle;

  if (file == NULL) {
    return NULL;
  }
#ifndef LAZY_FILE_LOAD
  if (wbFile_getData(file) != NULL) {
    char *data = wbFile_getData(file) + wbFile_getDataOffset(file);
    wbFile_setDataOffset(file, wbFile_getDataOffset(file) + size * count);
    return data;
  }
#endif /* LAZY_FILE_LOAD */

  handle = wbFile_getFileHandle(file);
  bufferLen = size * count + 1;
  buffer = wbNewArray(char, bufferLen);

  res = fread(buffer, size, count, handle);
  // make valid C string
  buffer[size * res] = '\0';

  return buffer;
}
Esempio n. 2
0
char *wbFile_readLine(wbFile_t file) {

  if (file == NULL) {
    return NULL;
  }
#ifdef LAZY_FILE_LOAD
  FILE *handle;
  memset(buffer, 0, MAX_CHARS_PER_LINE);

  handle = wbFile_getFileHandle(file);

  if (fgets(buffer, MAX_CHARS_PER_LINE - 1, handle)) {
    return buffer;
  } else {
    //wbLog(ERROR, "Was not able to read line from ", wbFile_getFileName(file));
    return NULL;
  }
#else
  size_t newOffset;
  size_t lenToNewLine = 0;
  const char *tmp;

  if (wbFile_getData(file) == NULL) {
    wbFile_setData(file, wbFile_read(file));
    fclose(wbFile_getFileHandle(file));
    wbFile_setFileHandle(file, NULL);
    wbFile_setDataOffset(file, 0);
    wbFile_setLength(file, strlen(wbFile_getData(file)));
  }

  memset(buffer, 0, MAX_CHARS_PER_LINE);

  if (wbFile_getDataOffset(file) >= wbFile_getLength(file)) {
    return NULL;
  }

  newOffset = wbFile_getDataOffset(file);
  tmp = wbFile_getData(file) + wbFile_getDataOffset(file);
  while (newOffset < wbFile_getLength(file) && *tmp != '\n') {
    tmp++;
    lenToNewLine++;
    newOffset++;
  }

  memcpy(buffer, wbFile_getData(file) + wbFile_getDataOffset(file),
         lenToNewLine);
  wbFile_setDataOffset(file, newOffset + 1);

  return buffer;
#endif
}
Esempio n. 3
0
void wbFile_delete(wbFile_t file) {
    if (file != NULL) {
        int idx = wbFile_getIndex(file);
        if (wbFile_getFileName(file) != NULL) {
            wbDelete(wbFile_getFileName(file));
        }
        if (wbFile_getMode(file) != NULL) {
            wbDelete(wbFile_getMode(file));
        }
        if (wbFile_getFileHandle(file) != NULL) {
            fclose(wbFile_getFileHandle(file));
        }
        if (idx >= 0) {
            wbAssert(wbFile_handles[idx] == file);
            wbFile_handles[idx] = NULL;
        }
        if (wbFile_getData(file) != NULL) {
            wbDelete(wbFile_getData(file));
        }
        wbDelete(file);
    }
}
Esempio n. 4
0
size_t wbFile_size(wbFile_t file) {
  size_t len;
  FILE *handle;

  if (file == NULL) {
    return 0;
  }
#ifndef LAZY_FILE_LOAD
  if (wbFile_getData(file) != NULL) {
    if (wbFile_getLength(file) == 0) {
      wbFile_setLength(file, strlen(wbFile_getData(file)));
    }
    return wbFile_getLength(file);
  }
#endif /* LAZY_FILE_LOAD */

  handle = wbFile_getFileHandle(file);

  fseek(handle, 0, SEEK_END);
  len = ftell(handle);
  rewind(handle);

  return len;
}
Esempio n. 5
0
void wbFile_rewind(wbFile_t file) {

    if (file == NULL) {
        return ;
    }

    if (wbFile_getData(file) != NULL) {
        FILE * handle;
        handle = wbFile_getFileHandle(file);

        rewind(handle);
    }
#ifndef LAZY_FILE_LOAD
    else {
        wbFile_setDataOffset(file, 0);
    }
#endif

    return ;
}