⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rtl_file.cpp

📁 C-smile OOL is a scripting language with C++-like grammar. It has compiler, VM running bytecodes and
💻 CPP
📖 第 1 页 / 共 2 页
字号:
  {
    INSTANCE *me = (INSTANCE *) _this_.v.v_thing;

#ifdef _WIN32
    char drive [ _MAX_DRIVE ];
    char dir   [ _MAX_DIR   ];
    char fname [ _MAX_FNAME ];
    char ext   [ _MAX_EXT   ];

    _splitpath ( me->path, drive, dir, fname, ext );

    if ( argc == 1 ) // set
    {
      char path_buffer [ _MAX_PATH ];

      if ( !argv [ 0 ].is_string () )
        error_parameters ();

      _makepath ( path_buffer, string ( argv [ 0 ] ), dir, fname, ext );
      me->path = path_buffer;
    }

    return VALUE ( (const char *) drive );
#else
    return VALUE ( (const char *) "" );
#endif
  }

  VALUE
    NODE::atime ( int argc, VALUE *argv )
  {
    INSTANCE *me = (INSTANCE *) _this_.v.v_thing;

    if ( argc == 1 ) // set
      error_read_only ();

    struct stat st;

    if ( !get_stat ( me->path, st ) )
      error_ne ( me );

    date_time dt ( st.st_atime );
    return VALUE ( double ( dt.absolute_millis () ) );
  }

  VALUE
    NODE::ctime ( int argc, VALUE *argv )
  {
    INSTANCE *me = (INSTANCE *) _this_.v.v_thing;

    if ( argc == 1 ) // set
      error_read_only ();

    struct stat st;

    if ( !get_stat ( me->path, st ) )
      error_ne ( me );

    date_time dt ( st.st_ctime );
    return VALUE ( double ( dt.absolute_millis () ) );
  }

  VALUE
    NODE::mtime ( int argc, VALUE *argv )
  {
    INSTANCE *me = (INSTANCE *) _this_.v.v_thing;

    if ( argc == 1 ) // set
      error_read_only ();

    struct stat st;

    if ( !get_stat ( me->path, st ) )
      error_ne ( me );

    date_time dt ( st.st_mtime );
    return VALUE ( double ( dt.absolute_millis () ) );
  }

  VALUE
    NODE::path ( int argc, VALUE *argv )
  {
    INSTANCE *me = (INSTANCE *) _this_.v.v_thing;

    if ( argc == 1 )
    { // set
      chktype ( 0, DT_STRING );
      me->path = CSTR ( argv [ 0 ].v.v_string );
    }
    return VALUE ( me->path );
  }

  VALUE
    NODE::is_folder ( int argc, VALUE *argv )
  {
    INSTANCE *me = (INSTANCE *) _this_.v.v_thing;

    if ( argc == 1 ) // set
      error_read_only ();

    struct stat st;

    if ( !get_stat ( me->path, st ) )
      return VALUE ( false );

    return VALUE ( ( st.st_mode & _S_IFDIR ) != 0 );
  }

  VALUE
    NODE::is_file ( int argc, VALUE *argv )
  {
    INSTANCE *me = (INSTANCE *) _this_.v.v_thing;

    if ( argc == 1 ) // set
      error_read_only ();

    struct stat st;

    if ( !get_stat ( me->path, st ) )
      return VALUE ( false );

    return VALUE ( ( st.st_mode & _S_IFDIR ) == 0 );
  }

  VALUE
    NODE::nodes ( int argc, VALUE *argv )
  {
    INSTANCE *me = (INSTANCE *) _this_.v.v_thing;

    struct stat st;

    if ( !get_stat ( me->path, st ) )
      error_ne ( me );
    if ( st.st_mode & _S_IFDIR == 0 )
      error_nd ( me );

    ARRAY *arr = new ARRAY ( 0 );

#ifdef _WIN32
    string wildcard;
    int attributes_include = _A_ARCH | _A_NORMAL | _A_RDONLY | _A_SUBDIR;
    int attributes_exclude = _A_HIDDEN | _A_SYSTEM;

    if ( argc >= 1 )
    {
      chktype ( 0, DT_STRING );
      wildcard = CSTR ( argv [ 0 ].v.v_string );
    }
    else
      wildcard = "*.*";

    if ( argc >= 2 )
    {
      chktype ( 1, DT_INTEGER );
      attributes_include = argv [ 0 ].v.v_integer;
    }

    if ( argc == 3 )
    {
      chktype ( 2, DT_INTEGER );
      attributes_exclude = argv [ 0 ].v.v_integer;
    }

    _finddata_t fdta;
    long hfile;
    string pathname_ = me->path + "\\";
    string pathname = pathname_ + wildcard;
    for ( bool b = ( hfile = _findfirst ( pathname, &fdta ) ) != -1L;
          b;
          b = ( _findnext ( hfile, &fdta ) == 0 ) )
    {
      if (
          ( fdta.attrib & attributes_include ) != 0 &&
          ( fdta.attrib & attributes_exclude ) == 0 &&
            strcmp ( fdta.name, "." ) != 0          &&
            strcmp ( fdta.name, ".." ) != 0
         )
      {
        VALUE v ( new NODE::INSTANCE ( pathname_ + fdta.name ) );
        arr->push ( v );
      }
    }
    _findclose ( hfile );
#endif
    return VALUE ( arr );
  }

  VALUE
    NODE::remove ( int argc, VALUE *argv )
  {
    INSTANCE *me = (INSTANCE *) _this_.v.v_thing;
    struct stat st;

    if ( !get_stat ( me->path, st ) )
      error_ne ( me );

    string fp = me->path;
#ifdef _WIN32
    fp.replace ( "/", "\\" );
#endif
    if ( ( st.st_mode & _S_IFDIR ) == 0 )
      return ( unlink ( me->path ) == 0 );
    else
      return ( rmdir ( me->path ) == 0 );
  }

  VALUE
    NODE::make_folder ( int argc, VALUE *argv )
  {
    INSTANCE *me = (INSTANCE *) _this_.v.v_thing;

#ifdef _WIN32
    int r = mkdir ( me->path );
#else
    int r = mkdir ( me->path, 0644 );
#endif
    if ( r == 0 )
      return 0;

    if ( errno == EEXIST )
      VM::error ( "'%s' already exitst", (const char *) me->path );
    else if ( errno == ENOENT )
      VM::error ( "'%s' path not found", (const char *) me->path );
    else
      VM::error ( "'%s' cannot be created" );

    return VALUE ( false );
  }

  VALUE
    NODE::rename ( int argc, VALUE *argv )
  {
    INSTANCE *me = (INSTANCE *) _this_.v.v_thing;

    struct stat st;

    if ( get_stat ( me->path, st ) )
      VM::error ( "'%s' does not exitst", (const char *) me->path );

    chktype ( 0, DT_STRING );
    string npath = CSTR ( argv [ 0 ].v.v_string );

    int r = ::rename ( me->path, npath );

    if ( r == 0 )
    {
      me->path = npath;
      return VALUE ( true );
    }
    if ( errno == EEXIST )
      VM::error ( "'%s' already exitst", (const char *) me->path );
    else if ( errno == ENOENT )
      VM::error ( "'%s' path not found", (const char *) me->path );
    else
      VM::error ( "'%s' cannot be created" );

    return VALUE ( false );
  }

  VALUE
    NODE::tostring ( int argc, VALUE *argv )
  {
    INSTANCE *me = (INSTANCE *) _this_.v.v_thing;
    return me->path;
  }

  VALUE
    NODE::stream ( int argc, VALUE *argv )
  {
    INSTANCE *me = (INSTANCE *) _this_.v.v_thing;

    struct stat st;
    int flags = sal::file::fo_shared;
    int mode = 0;

    if ( get_stat ( me->path, st ) )
    {
      if ( (st.st_mode & _S_IFDIR ) != 0 )
        error_nf ( me );
    }

    if ( argc != 1 || !argv->is_string () )
      error_parameters ();

    file_stream *fs = new file_stream ();
    fs->name ( me->path );
    if ( !fs->open ( CSTR ( argv [ 0 ].v.v_string ) ) )
    {
      VM::error ( "'%s' not found", (const char *) fs->name () );
    }
    return VALUE ( new STREAM::INSTANCE ( fs, me ) );
  }

  VALUE
    NODE::do_mode_prop ( int argc, VALUE *argv, int mask )
  {
    INSTANCE *me = (INSTANCE *) _this_.v.v_thing;

    if ( argc == 1 ) // set
      error_read_only ();

    struct stat st;

    if ( !get_stat ( me->path, st ) )
      error_ne ( me );

    return VALUE ( (st.st_mode & mask ) != 0 );
  }

  VALUE
    NODE::can_read ( int argc, VALUE *argv )
  {
    return do_mode_prop ( argc, argv, S_IREAD );
  }

  VALUE
    NODE::can_write ( int argc, VALUE *argv )
  {
    return do_mode_prop ( argc, argv, S_IWRITE );
  }

  VALUE
    NODE::can_exec ( int argc, VALUE *argv )
  {
    return do_mode_prop ( argc, argv, S_IEXEC );
  }

  VALUE
    NODE::is_pipe ( int argc, VALUE *argv )
  {
    INSTANCE *me = (INSTANCE *) _this_.v.v_thing;

    if ( argc == 1 ) // set
      error_read_only ();

    struct stat st;
    if ( !get_stat ( me->path, st ) )
      error_ne ( me );

    return VALUE ( false );
  }

  VALUE
    NODE::is_link ( int argc, VALUE *argv )
  {
    INSTANCE *me = (INSTANCE *) _this_.v.v_thing;

    if ( argc == 1 ) // set
      error_read_only ();

#ifdef _WIN32
    char drive [ _MAX_DRIVE ];
    char dir   [ _MAX_DIR   ];
    char fname [ _MAX_FNAME ];
    char ext   [ _MAX_EXT   ];

    _splitpath ( me->path, drive, dir, fname, ext );

    return ( strcmp ( ext, ".lnk" ) == 0 );
#else
    struct stat st;

    if ( get_stat ( me->path, st ) )
    {
      return ( ( st.st_mode & S_IFLNK ) == S_IFLNK );
    }
#endif
  }

};

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -