#include <stdint.h>
#include <cstring>
#include <cstdio>

namespace std {

  FILE *fopen(const char *filename, const char *mode) {

    bool read = false;
    bool write = false;
    bool append = false;
    bool extended = false;
    bool create = false;

    for (const char *p = mode; *p; ++p)
      switch (*p) {
      case 'r':
        read = true;
        continue;
      case 'w':
        write = true;
        continue;
      case 'a':
        append = true;
        continue;
      case '+':
        extended = true;
        continue;
      case 'x':
        create = true;
        continue;
      default:
        continue;
      }

    __euler_stream_handle handle;
    __euler_stream_result res = __euler_open_file(
      filename, strlen(filename), handle, write || append, create);

    if (res != __EULER_SR_SUCCESS)
      return 0;

    euler::file_stream *f = new euler::file_stream(handle, read || extended,
      write || extended, write && !append, append);

    if (f->good)
      return f;

    delete f;
    return 0;

  }

}
