Ejemplo n.º 1
0
/* open file and return fd; to make our life easier, always open RW */
int fs_open(const char *filename, int flags, int mode)
{
    int flag, fd;
    struct inode_s *ino, *dir;

    ino = dir = NULL;

    flag = (flags & O_CREAT) ? FS_SEARCH_CREAT : FS_SEARCH_GET;

    dir = current_dir();

    if ( (ino = find_inode(dir, filename, flag)) == NULL)
        goto err;

    release_inode(dir);

    if (ino->i_zone[0] == 0 && (flags & O_CREAT))
        fill_inode(ino, mode);

    if (flags & O_TRUNC) {
        ino->i_size = 0;
        ino->i_dirty = 1;
    }

    if ( (fd = get_fd(ino, (flags & O_APPEND) ? ino->i_size : 0)) == ERROR)
        goto err;

    return fd;

err:
    release_inode(dir);
    release_inode(ino);

    return ERROR;
}
Ejemplo n.º 2
0
void SaveFileDialog::Confirm() {
    Logger().debugStream() << "SaveFileDialog::Confirm: Confirming";
    
    if(!CheckChoiceValidity()){
        Logger().debugStream() << "SaveFileDialog::Confirm: Invalid choice. abort.";
        return;
    }

    /// Check if we chose a directory
    std::string choice = m_name_edit->Text();
    fs::path current_dir ( m_current_dir_edit->Text() );
    if ( !choice.empty() ) {
        fs::path chosen = current_dir / choice;
        if ( fs::is_directory ( chosen ) ) {
            Logger().debugStream() << "SaveFileDialog::Confirm: " << chosen << " is a directory. Listing content.";
            UpdateDirectory ( PathString ( chosen ) );
            return;
        } else {
            Logger().debugStream() << "SaveFileDialog::Confirm: File " << chosen << " chosen.";
        }
    } else {
        Logger().debugStream() << "SaveFileDialog::Confirm: Returning no file.";
    }

    CloseClicked();
}
namespace path {
//todo review and make names consistent!

typedef std::vector<std::string> files_t;

bool make_dir(std::string const& folder);
std::string make_temp_dir(std::string const& prefix, std::string const& suffix);
void remove_dir(std::string const& folder);
bool is_regular_file(std::string const& path);
std::string append_path(std::string const& prefix, std::string const& suffix);
std::string current_dir();

//todo why non-cons argument?!
void make_full_path(std::string& path);
std::string filename(std::string const& path);
std::string basename(std::string const& path);
std::string extension(std::string const& path);
std::string parent_path(std::string const& path);
bool check_existence(std::string const& path);
void remove_if_exists(std::string const& path);

//todo move to cpp and reduce code duplication!!!
/**
 * Checks if file exists.
 * Analogs: http://www.techbytes.ca/techbyte103.html , http://www.gamedev.net/topic/211918-determining-if-a-file-exists-c/
 */
inline bool FileExists(std::string filename) {
    struct stat st_buf;
    return stat(filename.c_str(), &st_buf) == 0 && S_ISREG(st_buf.st_mode);
}

/**
 * Exit(1) if file doesn't exists, writes FATAL log message.
 */
inline void CheckFileExistenceFATAL(std::string filename) {
  if(!FileExists(filename))
      FATAL_ERROR("File " << filename << " doesn't exist or can't be read!");
}

inline void make_dirs(const std::string& path) {
  VERIFY(!path.empty());

  size_t slash_pos = 0;
  while ((slash_pos = path.find_first_of('/', slash_pos + 1)) != std::string::npos) {
    make_dir(path.substr(0, slash_pos));
  }
  if (path[path.size() - 1] != '/') {
    make_dir(path);
  }
}

// doesn't support symlinks
std::string resolve(std::string const& path);
std::string make_relative_path(std::string p, std::string base = current_dir());
}
Ejemplo n.º 4
0
void HeightmapDialog::pickHeightmapFile()
{
  QString   filter    = "Images (*.png *.jpg *.bmp)";
  QString   base_path = QDir::currentPath();
  QString   path      = QFileDialog::getOpenFileName(this, "Select a heightmap", base_path, filter);
  QFileInfo info(path);
  QDir      current_dir(base_path);

  if (info.exists())
    ui->heightmapFile->setText(current_dir.relativeFilePath(path));
}
Ejemplo n.º 5
0
std::string SaveFileDialog::Result() const {
    std::string filename = m_name_edit->Text();
    if ( filename.empty() ) {
        return "";
    } else {
        // Ensure the file has an extension
        std::string::size_type end = filename.length();
        std::size_t ext_length = m_extension.length();
        if ( filename.length() < ext_length || filename.substr ( end-ext_length, ext_length ) != m_extension ) {
            filename.append ( m_extension );
        }
        fs::path current_dir ( m_current_dir_edit->Text() );
        return PathString ( ( current_dir / filename ).string() );
    }
}
Ejemplo n.º 6
0
void move_files(std::string const& old, std::string const& new_)
{
    for ( fs::directory_iterator current_dir(new_.c_str()); *current_dir; ++current_dir )
    {
        std::string const entry_name( *current_dir            );
        std::string const      entry( old + '/' + entry_name  );
        std::string const  new_entry( new_ + '/' + entry_name );
        
        if(fs::extension(entry_name) == ".hpp")
            move_file(entry, new_entry);
            
        if(fs::is_directory(new_entry))
            move_files(entry, new_entry);
    }
}
Ejemplo n.º 7
0
int main(int argc, char **argv)
{
    boost::filesystem::path current_dir(boost::filesystem::current_path());
    std::string training_data_file = current_dir.string() + "/training_data.txt";

    if (!boost::filesystem::exists(training_data_file))
    {
        std::cout << "File `" << training_data_file + "` does not exist!\nAborting." << std::endl;
        exit(1);
    }

    neuralNetworkTraining(training_data_file);

    return 0;
}
Ejemplo n.º 8
0
PNode DataList::FindByPath(const std::string& str)
{
    if (str.empty())
        return this;
    int i = 0;
    std::string buff;
    PDataList current_dir(this);
    if (str[i] == '/') { // go to root
        PDataList tmp = Parent();
        while (tmp.Ok()) {
            current_dir = tmp;
            tmp = tmp->Parent();
        }
    } else {
        buff += str[0];
    }
    i = 1;
    while ((unsigned int)(i) < str.size()) {
        if (str[i] == '/') {
            if (buff == "..") {
                current_dir = current_dir->Parent();
                if (!current_dir.Ok())
                    return NULL;
            } else if (buff != "." && !buff.empty()) { //
                PNode node = current_dir->Find(buff);
                if (!node.Ok())
                    return NULL;
                PDataList datalist(node);
                if (datalist.Ok()) {
                    current_dir = datalist;
                } else
                    return NULL;
            }
            buff = "";
        } else {
            buff += str[i];
        }
        ++i;
    }
    if (current_dir.Ok()) {
        if (!buff.empty()) {
            return current_dir->Find(buff);
        } else
            return PNode(current_dir);
    } else {
        return NULL;
    }
}
Ejemplo n.º 9
0
void delete_files(std::string const& old, std::string const& new_)
{
    for ( fs::directory_iterator current_dir(old.c_str()); *current_dir; ++current_dir )
    {
        std::string const entry_name( *current_dir            );
        std::string const      entry( old + '/' + entry_name  );
        std::string const  new_entry( new_ + '/' + entry_name );
        
        if(fs::extension(entry_name) == ".hpp" && !fs::exists(new_entry.c_str()))
        {
            fs::remove(entry.c_str());
        }
            
        if(fs::is_directory(entry))
            delete_files(entry, new_entry);
    }
}
Ejemplo n.º 10
0
/* rename oldpath to newpath */
int fs_rename(const char *oldpath, const char *newpath)
{
    struct inode_s *dir, *last_dir, *ino;
    char path[MAX_PATH], name[MAX_NAME];

    dir = last_dir = ino = NULL;

    /* get last component of path */
    process_path(newpath, path, name);
    if (name[0] == '\0') {
        char tmp[MAX_PATH];
        process_path(oldpath, tmp, name);
    }

    dir = current_dir();
    /* get last directory of target path */
    if ( (last_dir = find_inode(dir, path, FS_SEARCH_GET)) == NULL)
        goto err;

    /* remove entry from the old directory */
    if ( (ino = find_inode(dir, oldpath, FS_SEARCH_REMOVE)) == NULL)
        goto err;

    /* new entry in the last directory of path (or old one if file exists) */
    if (add_entry(last_dir, ino->i_num, name) == ERROR)
        goto err;

    /* check if oldpath was a dir, and update '..' in that case */
    if (IS_DIR(ino->i_mode)) {
        add_entry(ino, last_dir->i_num, "..");
        last_dir->i_nlinks++;
    }

    release_inode(last_dir);
    release_inode(dir);
    release_inode(ino);

    return OK;

err:
    release_inode(last_dir);
    release_inode(dir);
    release_inode(ino);

    return ERROR;
}
Ejemplo n.º 11
0
/* create new directory */
int fs_mkdir(const char *pathname, mode_t mode)
{
    struct inode_s *ino, *dir, *tmpdir;
    char path[MAX_PATH], name[MAX_NAME];

    ino = dir = tmpdir = NULL;

    process_path(pathname, path, name);
    tmpdir = current_dir();
    /* get inode numbers from parent and new dir */
    if ( (dir = find_inode(tmpdir, path, FS_SEARCH_GET)) == NULL)
        goto err;
    release_inode(tmpdir);
    if ( (ino = find_inode(dir, name, FS_SEARCH_ADD)) == NULL)
        goto err;

    /* fill new dir inode */
    fill_inode(ino, mode);
    ino->i_mode = (ino->i_mode & ~I_TYPE) | I_DIRECTORY;

    /* add '.' */
    empty_entry(ino, ino->i_num, ".");
    ino->i_nlinks++;

    /* and '..' */
    empty_entry(ino, dir->i_num, "..");
    dir->i_nlinks++;
    dir->i_dirty = 1;

    /* update dir size */
    ino->i_size = DIRENTRY_SIZE * 2;

    release_inode(dir);
    release_inode(ino);

    return OK;

err:
    release_inode(tmpdir);
    release_inode(dir);
    release_inode(ino);

    return ERROR;
}
Ejemplo n.º 12
0
/* remove file/dir from fs */
int fs_unlink(const char *pathname)
{
    struct inode_s *ino, *dir;

    ino = dir = NULL;

    dir = current_dir();
    if ( (ino = find_inode(dir, pathname, FS_SEARCH_REMOVE)) == NULL)
        goto err;
    rm_inode(ino->i_num);
    release_inode(dir);
    release_inode(ino);

    return OK;

err:
    release_inode(dir);
    release_inode(ino);
    return ERROR;
}
Ejemplo n.º 13
0
    inline int remove_all_rec( int& ec )
    {
        int count = 0;
        for ( directory_iterator current_dir( "." ); *current_dir; ++current_dir )
        {
            if(is_symlink(*current_dir) || !is_directory(*current_dir))
            {
                count += remove(*current_dir, ec);
                continue;
            }

            {
                current_path_saver const cps;
                current_path(*current_dir);
                count += remove_all_rec(ec);
            }

            count += remove(*current_dir, ec);
        }
        return count;
    }
Ejemplo n.º 14
0
int set_dir(PluginInstance* plugin, const wchar_t* dir, bool show_error) {
  FilePath fp_dir(dir);
  FilePath current_dir(plugin->current_dir);
  bool parent_dir = wcscmp(dir, L"..") == 0;
  if (current_dir.is_root_path() && parent_dir) {
    if (g_plugin_options.exit_on_dot_dot) far_control_ptr(plugin, FCTL_CLOSEPANEL, nullptr);
    return TRUE;
  }
  else if (parent_dir) {
    current_dir = current_dir.get_partial_path(current_dir.size() - 1);
  }
  else {
    current_dir.combine(fp_dir);
  }
  UnicodeString cd = current_dir.get_full_path();
  if (!current_dir.is_root_path()) {
    plugin->last_object = cd;
    if (!dir_exists(cd, plugin->session)) FAIL(SystemError(ERROR_PATH_NOT_FOUND));
  }
  plugin->current_dir = cd;
  return TRUE;
}
Ejemplo n.º 15
0
/* remove directory (only if it is empty) */
int fs_rmdir(const char *pathname)
{
    struct inode_s *ino, *dir, *tmpdir;
    char path[MAX_PATH], name[MAX_NAME];

    ino = dir = tmpdir = NULL;

    process_path(pathname, path, name);
    tmpdir = current_dir();
    /* get inode numbers from parent and new dir */
    if ( (dir = find_inode(tmpdir, path, FS_SEARCH_GET)) == NULL)
        goto err;
    release_inode(tmpdir);
    if ( (ino = find_inode(dir, name, FS_SEARCH_ADD)) == NULL)
        goto err;

    /* check if its a dir and it is empty */
    if (!IS_DIR(ino->i_mode) || ino->i_size != DIRENTRY_SIZE * 2)
        goto err;

    /* this cant give an error */
    release_inode(find_inode(dir, name, FS_SEARCH_REMOVE));

    /* free blocks and inode */
    rm_inode(ino->i_num);

    release_inode(dir);
    release_inode(ino);

    return OK;

err:
    release_inode(tmpdir);
    release_inode(dir);
    release_inode(ino);

    return ERROR;
}
Ejemplo n.º 16
0
int fs_chdir(const char *path)
{
    struct inode_s *ino, *dir;

    ino = dir = NULL;

    dir = current_dir();
    if ( (ino = find_inode(dir, path, FS_SEARCH_GET)) == NULL)
        goto err;
    release_inode(dir);

    if (!IS_DIR(ino->i_mode))
        goto err;

    set_current_dir(ino);
    release_inode(ino);

    return OK;

err:
    release_inode(dir);
    release_inode(ino);
    return ERROR;
}
Ejemplo n.º 17
0
   void ftw::scan_folders() {

      if( !path_exists( _impl->_path.path() ) ) {
         throw bad_ftw( "folder doesn't exist: " + _impl->_path.path() );
      }

      if( !is_directory( _impl->_path.path() ) ) {
         throw bad_ftw( "input is not a folder: " + _impl->_path.path() );
      }

      //boost::regex pattern("*"); // list all files starting with a
      boost::filesystem::path  current_dir( _impl->_path.path() ); //

      for( recursive_directory_iterator iter( current_dir ), end;
            iter != end;
            ++iter ) {

         boost::filesystem::path p = iter->path();

         if( boost::filesystem::is_directory( p ) ) {
            std::string dname = p.string();
            size_t i = index( dname, ".tmp" );

            if( i != string::npos ) {
               i = 0;
            }

            bool b = exclude_folder( dname, _impl->_folder_exclude );

            if( b == false ) {
               t_filename fn;
               fn.path( dname );
               _impl->_folders.push_back( fn );
            }
         }

         if( boost::filesystem::is_regular_file( p ) ) {
            std::string name = p.string();
            t_filename fn = t_filename( name );
            string dir = fn.path();
            size_t i = index( dir, ".tmp" );

            if( i != string::npos ) {
               i = 0;
            }

            bool bdir = exclude_folder( dir, _impl->_folder_exclude );

            if( bdir == false ) {
               bool b_include = include_file( name, _impl->_file_include );

               if( b_include == true ) {
                  _impl->_files.push_back( name );
               }
            }

            //    if (regex_match(name, pattern))
            //      std::cout << iter->path() << "\n";
         }
      }
   }
Ejemplo n.º 18
0
void writeKernelInit(std::ostream & kernel_file, const char * dirname, std::string & subfolder, bool is_float)
{
    //extract alignment information from subfolder string:
    std::istringstream stream(subfolder.substr(5, subfolder.size()-5));
    unsigned int alignment = 0;
    stream >> alignment;
    if (alignment == 0)
        std::cerr << "ERROR: Could not extract alignment from " << subfolder << std::endl;

    kernel_file << "   template <>" << std::endl;
    kernel_file << "   struct " << dirname;
    if (is_float)
        kernel_file << "<float, ";
    else
        kernel_file << "<double, ";
    kernel_file << alignment << ">" << std::endl;
    kernel_file << "   {" << std::endl;

    kernel_file << "    static std::string program_name()" << std::endl;
    kernel_file << "    {" << std::endl;
    kernel_file << "      return \"";
    if (is_float)
        kernel_file << "f";
    else
        kernel_file << "d";
    kernel_file << "_" << dirname << "_" << alignment << "\";" << std::endl;
    kernel_file << "    }" << std::endl;

    kernel_file << "    static void init(viennacl::ocl::context & ctx)" << std::endl;
    kernel_file << "    {" << std::endl;
    if (is_float)
      kernel_file << "      viennacl::ocl::DOUBLE_PRECISION_CHECKER<float>::apply(ctx);" << std::endl;
    else
      kernel_file << "      viennacl::ocl::DOUBLE_PRECISION_CHECKER<double>::apply(ctx);" << std::endl;
    kernel_file << "      static std::map<cl_context, bool> init_done;" << std::endl;
    kernel_file << "      if (!init_done[ctx.handle().get()])" << std::endl;
    kernel_file << "      {" << std::endl;
    kernel_file << "        std::string source;" << std::endl;
    kernel_file << "        source.reserve(8192);" << std::endl; //to avoid some early reallocations
    if (!is_float)
      kernel_file << "        std::string fp64_ext = ctx.current_device().double_support_extension();" << std::endl;

    //iterate over all kernels in align1-folder:
    std::string current_dir(dirname);
    current_dir += "/align1";
    fs::path filepath = fs::system_complete( fs::path( current_dir ) );

    fs::directory_iterator end_iter;
    //write and register single precision sources:
    for ( fs::directory_iterator cl_itr( filepath );
          cl_itr != end_iter;
          ++cl_itr )
    {
#ifdef USE_OLD_BOOST_FILESYSTEM_VERSION
        std::string fname = cl_itr->path().filename();
#else
        std::string fname = cl_itr->path().filename().string();
#endif
        size_t pos = fname.find(".cl");
        if ( pos == std::string::npos )
          continue;

        if (fname.substr(fname.size()-3, 3) == ".cl")
        {
            //add kernel source to program string:
            std::string kernel_name_ending = fname.size() > 8 ? fname.substr(fname.size()-7, 4) : " ";
            if (kernel_name_ending == "_amd")
              kernel_file << "        if (ctx.current_device().local_mem_size() > 20000)" << std::endl << "  ";  //fast AMD kernels require more than 20 kB of local memory

            kernel_file << "        source.append(";
            if (!is_float)
                kernel_file << "viennacl::tools::make_double_kernel(";
            kernel_file << dirname << "_align" << getBestKernel(dirname, fname, alignment) << "_" << fname.substr(0, fname.size()-3);
            if (!is_float)
                kernel_file << ", fp64_ext)";
            kernel_file << ");" << std::endl;
        }
    } //for

    kernel_file << "        std::string prog_name = program_name();" << std::endl;
    kernel_file << "        #ifdef VIENNACL_BUILD_INFO" << std::endl;
    kernel_file << "        std::cout << \"Creating program \" << prog_name << std::endl;" << std::endl;
    kernel_file << "        #endif" << std::endl;
    kernel_file << "        ctx.add_program(source, prog_name);" << std::endl;

    kernel_file << "        init_done[ctx.handle().get()] = true;" << std::endl;
    kernel_file << "       } //if" << std::endl;
    kernel_file << "     } //init" << std::endl;
    kernel_file << "    }; // struct" << std::endl << std::endl;
}
Ejemplo n.º 19
0
bool TreeLeaf::importNode ( const QString& str_directory_path, bool b_recursive_import )
{
    //get name of created node
    QDir income_dir (str_directory_path);
    //QString str_new_node_name       = income_dir.dirName();
    const QStringList str_list_of_dirs  = income_dir.entryList(QDir::AllDirs|QDir::NoDotAndDotDot|QDir::Readable|QDir::NoSymLinks);
    QStringList str_list_of_files       = income_dir.entryList(QDir::Files|QDir::Readable|QDir::NoSymLinks);
    //
    //remove file "index.txt" from the list of imported files
    //
    int i_rem_index = str_list_of_files.indexOf("index.txt",0);
    if (-1 != i_rem_index)
        str_list_of_files.removeAt(i_rem_index);
    //
    //check all list of dirs, extract "dir name" add ".txt"
    //
    QStringList::const_iterator itr = str_list_of_dirs.begin();
    for (; itr != str_list_of_dirs.end(); ++itr)
    {
        QDir current_dir (*itr);
        const QString str_new_node_name = current_dir.dirName();
        //
        QString str_descriptor_file_name;
        bool b_is_html = false;
        //call search in file list here
        const int i_file_index = findDescriptorFile( str_list_of_files, str_new_node_name, str_descriptor_file_name, b_is_html);
        //
        QString str_descriptor_read_buffer;
        //
        if ( -1 != i_file_index )
        {
            QFile f (str_directory_path + g_strPATH_DELIMETER + str_descriptor_file_name);
            //
            if ( f.open(QIODevice::ReadOnly) )
            {
                QTextStream stream( &f );
                //
                while ( !stream.atEnd() )
                {
                    str_descriptor_read_buffer += stream.readLine(); // line of text excluding '\n'
                    str_descriptor_read_buffer += tr("\n");
                };
                //
                f.close();
                str_list_of_files.removeAt(i_file_index);
            };
        }; // if ( -1 != i_file_index )

        // create child node here using str_new_node_name , str_descriptor_read_buffer
        TreeLeaf* ptr_new_node = new TreeLeaf(this,
                                              -1,
                                              this->getTreeID(),
                                              str_new_node_name,
                                              m_ptrParentTree );
        //
        if (str_descriptor_read_buffer.length() > 0)
            ptr_new_node->setDescriptor(str_descriptor_read_buffer, b_is_html);
        // if b_recursive_import
        if (true == b_recursive_import)
        {
            ptr_new_node->importNode(str_directory_path + g_strPATH_DELIMETER + str_new_node_name, b_recursive_import);
        };
        // call import for this node, pass str_directory_path + "/" + str_new_node_name
        continue;
    };
    //all nodes created, the rest in the str_list_of_files are attachments.
    QStringList str_attachments;
    //
    QStringList::const_iterator itr_files = str_list_of_files.begin();
    //
    for (; itr_files != str_list_of_files.end(); ++itr_files)
    {
        QString str_new_attach = str_directory_path + g_strPATH_DELIMETER + (*itr_files);
        str_attachments.append(str_new_attach);
    };
    //
    // TODO node attachments imported as non-protected.
    //
    if (str_attachments.size() > 0)
        this->addAttachments(str_attachments,false, false, false);
    //
    return true;
}