Example #1
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
}
Example #2
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;
}
Example #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);
    }
}
Example #4
0
static inline void wbExportCSV_write(wbExportCSV_t csv, void *data,
                                     int rows, int columns, char sep,
                                     wbType_t type) {
  int ii, jj;
  wbFile_t file;
  FILE *handle;
  char seperator[2];

  if (csv == nullptr || wbExportCSV_getFile(csv) == nullptr) {
    return;
  }

  file = wbExportCSV_getFile(csv);

  handle = wbFile_getFileHandle(file);

  if (handle == nullptr) {
    return;
  }

  if (sep == '\0') {
    seperator[0] = ',';
  } else {
    seperator[0] = sep;
  }
  seperator[1] = '\0';

  for (ii = 0; ii < rows; ii++) {
    for (jj = 0; jj < columns; jj++) {
      if (type == wbType_integer) {
        int elem = ((int *)data)[ii * columns + jj];
        fprintf(handle, "%d", elem);
      } else if (type == wbType_ubit8) {
        int elem = ((unsigned char *)data)[ii * columns + jj];
        fprintf(handle, "%d", elem);
      } else {
        wbReal_t elem = ((wbReal_t *)data)[ii * columns + jj];
        fprintf(handle, "%f", elem);
      }
      if (jj == columns - 1) {
        fprintf(handle, "\n");
      } else {
        fprintf(handle, "%s", seperator);
      }
    }
  }

  return;
}
Example #5
0
size_t wbFile_size(wbFile_t file) {
    size_t len;
    FILE * handle;

    if (file == NULL) {
        return 0;
    }

    handle = wbFile_getFileHandle(file);

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

    return len;
}
Example #6
0
static inline void wbExportRaw_write(wbExportRaw_t raw, void *data,
                                     int rows, int columns,
                                     wbType_t type) {
  int ii, jj;
  FILE *handle;
  wbFile_t file;

  if (raw == nullptr || wbExportRaw_getFile(raw) == nullptr) {
    return;
  }

  file = wbExportRaw_getFile(raw);

  handle = wbFile_getFileHandle(file);

  if (handle == nullptr) {
    return;
  }

  if (columns == 1) {
    fprintf(handle, "%d\n", rows);
  } else {
    fprintf(handle, "%d %d\n", rows, columns);
  }

  for (ii = 0; ii < rows; ii++) {
    for (jj = 0; jj < columns; jj++) {
      if (type == wbType_integer) {
        int elem = ((int *)data)[ii * columns + jj];
        fprintf(handle, "%d", elem);
      } else if (type == wbType_ubit8) {
        int elem = ((unsigned char *)data)[ii * columns + jj];
        fprintf(handle, "%d", elem);
      } else {
        wbReal_t elem = ((wbReal_t *)data)[ii * columns + jj];
        fprintf(handle, "%f", elem);
      }
      if (jj == columns - 1) {
        fprintf(handle, "\n");
      } else {
        fprintf(handle, " ");
      }
    }
  }

  return;
}
Example #7
0
void wbFile_write(wbFile_t file, const void *buffer, size_t size,
                  size_t count) {
  size_t res;
  FILE *handle;

  if (file == NULL) {
    return;
  }

  handle = wbFile_getFileHandle(file);

  res = fwrite(buffer, size, count, handle);
  if (res != count) {
    wbLog(ERROR, "Failed to write data to ", wbFile_getFileName(file));
  }

  return;
}
Example #8
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 ;
}
Example #9
0
char * wbFile_read(wbFile_t file, size_t size, size_t count) {
    size_t res;
    char * buffer;
    FILE * handle;

    if (file == NULL) {
        return NULL;
    }

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

    res = fread(buffer, size, count, handle);
    if (res != count) {
        wbLog(ERROR, "Failed to read data from ", wbFile_getFileName(file));
        wbDelete(buffer);
        return NULL;
    }

    return buffer;
}
Example #10
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;
}
Example #11
0
static inline void wbExportText_write(wbExportText_t text,
                                      const char *data, int length) {
  int ii;
  FILE *handle;
  wbFile_t file;

  if (text == nullptr || wbExportText_getFile(text) == nullptr) {
    return;
  }

  file = wbExportText_getFile(text);

  handle = wbFile_getFileHandle(file);

  if (handle == nullptr) {
    return;
  }

  for (ii = 0; ii < length; ii++) {
    fprintf(handle, "%c", data[ii]);
  }

  return;
}
Example #12
0
static char * buffer = NULL;

char * wbFile_readLine(wbFile_t file) {

    if (file == NULL) {
        return NULL;
    }
    if (buffer == NULL) {
        buffer = wbNewArray(char, MAX_CHARS_PER_LINE);
    }
#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));