Recent Posts

Pages: 1 2 3 4 5 6 7 8 9 10
1
I was hoping you guys could review this code, and I'm aware of a few things that need to be changed off the bat, so I'm asking apart from what I'm already aware needs changing.

1) writing one byte at a time is slow. I was wondering if you knew what buffer size you'd find to be the most acceptable, (not too large or too small)
2) this is currently under universal, but can be moved to extensions so that we can do platform checks in the makefile and not the sources
Code: [Select]
  int file_bin_open(string fname, int mode) {
    #if defined(_WIN32)
    wstring wfname = widen(fname);
    FILE *fp = nullptr;
    switch (mode) {
      case  0: { fp = _wfopen(wfname.c_str(), L"rb, ccs=UTF-8" ); break; }
      case  1: { fp = _wfopen(wfname.c_str(), L"wb, ccs=UTF-8" ); break; }
      case  2: { fp = _wfopen(wfname.c_str(), L"w+b, ccs=UTF-8"); break; }
      case  3: { fp = _wfopen(wfname.c_str(), L"ab, ccs=UTF-8" ); break; }
      case  4: { fp = _wfopen(wfname.c_str(), L"a+b, ccs=UTF-8"); break; }
      default: return -1;
    }
    if (fp) { int fd = _dup(_fileno(fp));
    fclose(fp); return fd; }
    #else
    FILE *fp = nullptr;
    switch (mode) {
      case  0: { fp = fopen(fname.c_str(), "rb" ); break; }
      case  1: { fp = fopen(fname.c_str(), "wb" ); break; }
      case  2: { fp = fopen(fname.c_str(), "w+b"); break; }
      case  3: { fp = fopen(fname.c_str(), "ab" ); break; }
      case  4: { fp = fopen(fname.c_str(), "a+b"); break; }
      default: return -1;
    }
    if (fp) { int fd = dup(fileno(fp));
    fclose(fp); return fd; }
    #endif
    return -1;
  }
 
  int file_bin_rewrite(int fd) {
    #if defined(_WIN32)
    return _chsize(fd, 0);
    #else
    return ftruncate(fd, 0);
    #endif
  }
 
  int file_bin_close(int fd) {
    return close(fd);
  }
 
  long file_bin_size(int fd) {
    #if defined(_WIN32)
    struct _stat info = { 0 };
    int result = _fstat(fd, &info);
    #else
    struct stat info = { 0 };
    int result = fstat(fd, &info);
    #endif
    if (result != -1) {
      return info.st_size;
    }
    return 0;
  }

  long file_bin_position(int fd) {
    #if defined(_WIN32)
    return _lseek(fd, 0, SEEK_CUR);
    #else
    return lseek(fd, 0, SEEK_CUR);
    #endif
  }
 
  long file_bin_seek(int fd, long pos) {
    #if defined(_WIN32)
    return _lseek(fd, pos, SEEK_CUR);
    #else
    return lseek(fd, pos, SEEK_CUR);
    #endif
  }

  int file_bin_read_byte(int fd) {
    int byte = -1;
    #if defined(_WIN32)
    int num = (int)_read(fd, &byte, 1);
    #else
    int num = (int)read(fd, &byte, 1);
    #endif
    if (num == -1) return -1;
    return byte;
  }

  int file_bin_write_byte(int fd, int byte) {
    #if defined(_WIN32)
    return (int)_write(fd, &byte, 1);
    #else
    return (int)write(fd, &byte, 1);
    #endif
  }

  int file_text_open_read(string fname) {
    return file_bin_open(fname, 0);
  }

  int file_text_open_write(string fname) {
    return file_bin_open(fname, 1);
  }

  int file_text_open_append(string fname) {
    return file_bin_open(fname, 3);
  }

  long file_text_write_string(int fd, string str) {
    for (unsigned i = 0; i < str.length(); i++) {
      if (file_bin_write_byte(fd, str[i]) == -1) {
        return -1;
      }
    }
    return (long)str.length();
  }

  long file_text_write_real(int fd, double val) {
    string str = std::to_string(val);
    return file_text_write_string(fd, str);
  }

  int file_text_writeln(int fd) {
    return file_bin_write_byte(fd, '\n');
  }

  bool file_text_eof(int fd) {
    return (file_bin_position(fd) >= file_bin_size(fd));
  }

  bool file_text_eoln(int fd) {
    file_bin_seek(fd, -1);
    bool res = ((char)file_bin_read_byte(fd) == '\n');
    return (file_text_eof(fd) || res);
  }

  double file_text_read_real(int fd) {
    bool dot = false, sign = false;
    string str; char byte = (char)file_bin_read_byte(fd);
    if (byte == '\n') byte = (char)file_bin_read_byte(fd);
    if (byte == '.' && !dot) {
      dot = true;
    } else if (!is_digit(byte) && byte != '+' &&
      byte != '-' && byte != '.') {
      return 0;
    } else if (byte == '+' || byte == '-') {
      sign = true;
    }
    if (byte == -1) goto finish;
    str.resize(str.length() + 1, ' ');
    str[str.length() - 1] = byte;
    if (sign) {
      byte = (char)file_bin_read_byte(fd);
      if (byte == '.' && !dot) {
        dot = true;
      } else if (!is_digit(byte) && byte != '.') {
        return strtod(str.c_str(), nullptr);
      }
      if (byte == -1) goto finish;
      str.resize(str.length() + 1, ' ');
      str[str.length() - 1] = byte;
    }
    while (byte != '\n' && !(file_bin_position(fd) > file_bin_size(fd))) {
      byte = (char)file_bin_read_byte(fd);
      if (byte == '.' && !dot) {
        dot = true;
      } else if (byte == '.' && dot) {
        break;
      } else if (!is_digit(byte) && byte != '.') {
        break;
      } else if (byte == '\n' || file_bin_position(fd) > file_bin_size(fd)) {
        break;
      }
      if (byte == -1) goto finish;
      str.resize(str.length() + 1, ' ');
      str[str.length() - 1] = byte;
    }
    finish:
    return strtod(str.c_str(), nullptr);
  }

  string file_text_read_string(int fd) {
    int byte = -1; string str;
    while ((char)byte != '\n' && !file_text_eof(fd)) {
      byte = file_bin_read_byte(fd);
      str.resize(str.length() + 1, ' ');
      str[str.length() - 1] = byte;
    }
    if (str[str.length() - 2] != '\r' && str[str.length() - 1] == '\n') {
      file_bin_seek(fd, -1);
    }
    if (str[str.length() - 2] == '\r' && str[str.length() - 1] == '\n') {
      file_bin_seek(fd, -2);
    }
    return str;
  }

  string file_text_readln(int fd) {
    int byte = -1; string str;
    while ((char)byte != '\n' && !file_text_eof(fd)) {
      byte = file_bin_read_byte(fd);
      str.resize(str.length() + 1, ' ');
      str[str.length() - 1] = ((byte == -1) ? 0 : byte);
      if (byte == -1) break;
    }
    return str;
  }

  string file_text_read_all(int fd) {
    string str;
    while (!file_text_eof(fd)) {
      char byte = (char)file_bin_read_byte(fd);
      str.resize(str.length() + 1, ' ');
      str[str.length() - 1] = ((byte == -1) ? 0 : byte);
      if (byte == -1) break;
    }
    return str;
  }

  int file_text_open_from_string(string str) {
    int fd[2];
    #if defined(_WIN32)
    if (_pipe(fd, str.length(), O_BINARY) < 0) return -1;
    #else
    if (pipe(fd) < 0) return -1;
    #endif
    file_text_write_string(fd[1], str);
    file_bin_close(fd[1]);
    return fd[0];
  }
 
  int file_text_close(int fd) {
    return file_bin_close(fd);
  }

This enables UTF-8 support on windows as well as makes use of real file desciptors instead of utilizing the asset array, both of which are improvements if you ask me.

Edit: removed unrelated rabbit trail
2
Works in Progress / retro fps dungeon crawler
« Last post by hpg678 on September 24, 2021, 05:48:46 am »
This is an alternate way to create a 3d dungeon crawler game.


3
Announcements / Back online, new hardware
« Last post by Josh @ Dreamland on August 15, 2021, 07:28:04 pm »
Everything's upgraded to PHP 7 and we're on a new Debian instance. I think everything's working now.

I should probably advise everyone to reset their password here and anywhere else you've used the same one.  Please don't re-use passwords, ever.  Sorry again for the inconvenience.

I'm hoping to migrate the forums and Wiki to use GitHub auth soon.
Pages: 1 2 3 4 5 6 7 8 9 10