Visualizzazione dei risultati da 1 a 4 su 4

Discussione: [C++/STL]Ricerca

  1. #1

    [C++/STL]Ricerca

    Mi sto facendo una mia lib comene x tutti i miei programmi, che sia il + possibili portabile.
    Sto facendo la generica classe CParse per caricare files di conf.
    Questa sara la base x CCOnfigParse.
    codice:
    // Parse.hpp
    #ifndef PARSE_HPP_
    #define PARSE_HPP_
    #include "Define.hpp"
    #include <iostream>
    #include <fstream>
    #include <sstream>
    #include <string>
    #include <cstring>
    
    using namespace std;
    
    N_BEG
    /**
      Name:CParse  \n
      Author:Kleidemos \n
      Description:Parserizza una configurazione generica  \n
      Indicata come base per sviluppi \n
    */
    class CParse
    {
        friend CParse& operator+=(CParse &p, string _add);
            public:
                    CParse() { _buf = " "; };
                    CParse(string buf){ _buf = buf; };
                    CParse(char * buf){ _buf = buf; };
                    ~CParse();
                    /// Read a generic config
                    void Read(istream &is = cin, string * buf_r = "");
                    /// Write a generic config
                    void Write(ostream &os = cout, string buf = " ");
                    /// Add a text
                    inline void Add(string add) { _buf += add; };
                    /// Find a info
                    string Find(string info);
            private:
                    string _buf;        
    };
    N_END
    
    #endif
    Questo è la sua dichiarazione.
    Ho implementato +=, Write, Add.
    Ma nn so come implementare Find.
    Questo metodo dovrebbe cercarmi nel buffer caricato con Read(ancora da sistemare) un determinato valore(tipo file ini opt=valore).
    Ma nn so come implementarla
    Al momento ho implementato solo un metodo per sapere se è un files di config o no.
    codice:
     
    /**
      Name:Find\n
      ReturnL'informazione trovata\n
      Argoments:L' informazione da cercare\n
      Description:Find a info\n
    */
    string CParse::Find(string info)
    {
        const string CONFIG_MARK = "[CONFIG]";
        if(_buf.substr(0, CONFIG_MARK.size()) == CONFIG_MARK){
           /// TODO
           string::size_type val;
           val = _buf.find(info, 0 );
           if(val != string::npos){// trovata
                  string ret;
                  
           }else{// non trovata
                  return "Error!\n";
           }
    
        }else{
            return "Error!\n";
        }
    }
    La stupidità umana e l'universo sono infinite.
    Della seconda non sono certo(Einstein)

    Gnu/Linux User

  2. #2
    Utente bannato
    Registrato dal
    Sep 2003
    Messaggi
    1,012
    Potresti fare qualcosa tipo:

    Cerchi la opzione opt(parametro)
    Se,eliminando gli spazi, il carattere successivo è '=' :
    Trova al stringa tra l'uguale ed il codice di fine riga/ fine file / fine stringa
    Elimina gli spazi
    restituisci questa stringa

    Per esempio se un file contiene:
    codice:
    [CONFIG]
    
    ciao
    ciao
    
    colore = arancio    
    numero    =due
    e chiedi il valure di "numero" ti dovrebbe restituire "due"


  3. #3
    codice:
    string CParse::Find(string info)
    {
        const string CONFIG_MARK = "[CONFIG]";
        if(_buf.substr(0, CONFIG_MARK.size()) == CONFIG_MARK)
        {
           /// TODO
           string::size_type val;
           val = _buf.find(info, 0 );
           if(val != string::npos){// trovata
                  string ret;
                  string::size_type equals = pos+1;
                  if(info.at(equals) == "=")
                  {
                      string::size_type fin = _buf.find(";", 0 );// fine dell'info
                      if(fin != string::npos)
                      {
                          ret.assign(equals+1, fin);
                      }else{
                          ret = "Error!";
                      }
                  }
                  
           }else{// non trovata
                  ret = "Error!";
           }
    
        }else{
            ret = "Error!";
        }
        return ret;
    }
    Una cosa simile?

    Pero nn mi va
    La stupidità umana e l'universo sono infinite.
    Della seconda non sono certo(Einstein)

    Gnu/Linux User

  4. #4

    Risolto........

    codice:
    // Parse.hpp
    #ifndef PARSE_HPP_
    #define PARSE_HPP_
    
    #include "Define.hpp"
    
    #include <cstdio>
    #include <iostream>
    #include <fstream>
    #include <sstream>
    #include <string>
    #include <vector>
    #include <cstring>
    
    using namespace std;
    
    N_BEG
    
    /**
      Name:SInfo  \n
      Author:Kleidemos \n
      Description:Configurazione  \n
    */
    typedef struct SInfo
    {
    	string key;
    	string value;
    } Info;
    
    
    /**
      Name:CParse  \n
      Author:Kleidemos \n
      Description:Parser classs for generic text  \n
    */
    class CParse
    {
            public:
                    CParse(){ _filename = "prog.conf"; };
                    CParse(string name){ _filename = name; };
                    CParse(char * name){ _filename = name; };
                    ~CParse(){};
                    virtual void Read(char separator = 'v');
                    virtual void Write(char separator = 'v', string key = "", string value = "");
    		    void Test();
    		    virtual string Find(string key);
            private:
                    string _filename;
                    vector<Info> conf;        
    };
    
    N_END
    
    #endif
    codice:
    // Parse.cpp
    
    #include "Parse.hpp"
    
    N_BEG
    /**
      Name:Read\n
      Return: -\n
      Argoments: A separator [ DEFAULT: v ] \n
      Description:Puts a generic config into internal buffer\n
    */
    void CParse::Read(char separator)
    {
    	ifstream IN(_filename.c_str());
    	char code;
    	string key, value;
    	Info inf;
    	while(IN)
    	{
    		IN >> code;
    		if(code == separator) // a key find
    		{
    
    			IN >> key >> value;		
    			inf.value = value;
    			inf.key = key;
    			conf.push_back(inf);
    		}
    		/**
    		Exclude:
    		- ;, # For comment
    		*/
    		else
    		{
    			key = " ";
    			value = " ";
    			// conf.push_back(inf);
    		}
    	}
    	IN.close();
    }
    /**
      Name:Write\n
      Return:-\n
      Argoments: 
      - The name of options 
      - The value\n
      - A separator [ DEFAULT: v ]
    
      Description:Write a generic config value
    */
    void CParse::Write(char separator, string key, string value)
    {
    	ofstream OUT(_filename.c_str(), ios::app);
    	OUT << separator << " " << key << " " << value << "\n";
    	OUT.close();
    }
    /**
      Name:Find\n
      Return:A Value of option\n
      Argoments: The name of option \n
      Description: Findo a option\n
    */
    string CParse::Find(string key)
    {
    	string ret;
    	vector<Info>::iterator It = conf.begin();
    	for(; It != conf.end();++It)
    	{
    		if(It->key == key)
    			ret = It->value;
    	}
    	return ret;
    }
    /**
      Name:Test\n
      Return:-\n
      Argoments: -
      Description: Test the class\n
      ONLY FOR DEBUG
    */
    void CParse::Test()
    {
    	// Inserting
    	Write('v', "prova", "si"); // write prova
    	Write('v', "provas", "no");// write provas
    	Write('v', "Schermo", "1024x768");// write Schermo
      Write('n', "Schermos", "1024x768");// write error value
    	// Reading
    	Read('v'); // puts read file
    	// Printing
    	typedef vector<Info>::iterator It;
    	It it = conf.begin(); 
    	for(;it != conf.end();++it) // iterator
    	{
    		cout << it->key << " => " << it->value << "\n"; 
    	}
    	// Finding
    	cout << "Finding prova => " << Find("prova") << "\n";
    	cout << "Finding provas => " << Find("provas") << "\n";
    	cout << "Finding Schermo => " << Find("Schermo") << "\n";
    	cout << "Finding Schermos => " << Find("Schermos") << "\n";// try to find error value
    	
    }
    
    N_END
    La stupidità umana e l'universo sono infinite.
    Della seconda non sono certo(Einstein)

    Gnu/Linux User

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.