예제 #1
0
파일: main.cpp 프로젝트: SouhaibDZ/taquin42
int								main(int argc, char **argv)
{
    char Name = 'M';
    std::string taq = "TaquinA5_2.txt";
    if (argc == 3)
    {
        std::ifstream infile;
        infile.open(argv[2]);
        if (!infile.is_open())
        {
            std::cout << "Error: file <" << argv[2] << ">" << " not found" << std::endl;
            return (-1);
        }
        infile.close();
        taq = argv[2];
        FileLoader				F;
        std::string				S;
        Puzzle					P;
        SolutionGenerator		SG;
        short unsigned int**	Tab;
        clock_t					timeDeb, timeEnd;
        std::list<Puzzle>		OpenedList, ClosedList;
        int x = 0, y = 0, fg = -42;

        timeDeb = clock();
        DisplayLogo();
        F.LoadFile(taq.c_str(), S);
        std::istringstream		In(S);
        P.SetAlgo(Name);
        Tab = P.CreatePuzzle(S);
        std::list<Puzzle>::iterator FirstPuzzle;
        OpenedList.push_back(P);
        while (fg != 0 && !OpenedList.empty())
        {
            FirstPuzzle = OpenedList.begin();
            fg = Resume(FirstPuzzle, OpenedList, ClosedList, timeEnd);
            ProcessUp(FirstPuzzle, OpenedList, ClosedList);
            ProcessDown(FirstPuzzle, OpenedList, ClosedList);
            ProcessRight(FirstPuzzle, OpenedList, ClosedList);
            ProcessLeft(FirstPuzzle, OpenedList, ClosedList);

            (*FirstPuzzle).ClearListTab();
            ClosedList.push_back(*FirstPuzzle);
            OpenedList.erase(FirstPuzzle);
        }
        if (fg != 0)
            std::cout << "NO SOLUTION FOR THIS TAQUIN!!!" << std::endl;
        std::cout << "CLOSED LIST NUMBER OF CONTENTS	: \t\t[" << ClosedList.size() << "]"<< std::endl;
        std::cout << "TIME ELAPSED			: \t\t[" << static_cast<double>(timeEnd - timeDeb) << "] ms." << std::endl;
        ShowNbMoves();
        std::cout << "Cleaning..." << std::endl;
        Clean(OpenedList, ClosedList);
        std::cout << "Clean done" << std::endl;
    }
    return (0);
}
void Preprocessor::RecursivePreprocess( std::string filename, FileLoader& file_source, LexemList& lexems, DefineTable& define_table )
{
    unsigned int start_line = CurrentLine;
    LinesThisFile = 0;
    CurrentFile = filename;
    SetFileMacro( define_table, CurrentFile );
    SetLineMacro( define_table, LinesThisFile );

    // Path formatting must be done in main application
    std::string CurrentFileRoot = RootPath + CurrentFile;
    if( std::find( FilesPreprocessed.begin(), FilesPreprocessed.end(), CurrentFileRoot ) == FilesPreprocessed.end() )
        FilesPreprocessed.push_back( CurrentFileRoot );

    std::vector<char> data;
    bool              loaded = file_source.LoadFile( RootPath, filename, data );
    if( !loaded )
    {
        PrintErrorMessage( std::string( "Could not open file " ) + RootPath + filename );
        return;
    }

    if( data.size() == 0 )
        return;
    char* d_end = &data[data.size() - 1];
    ++d_end;
    Lex( &data[0], d_end, lexems );

    LexemList::iterator itr = lexems.begin();
    LexemList::iterator end = lexems.end();
    LLITR               old = end;
    while( itr != end )
    {
        if( itr->Type == Lexem::NEWLINE )
        {
            if( itr != old )
            {
                CurrentLine++;
                LinesThisFile++;
                SetLineMacro( define_table, LinesThisFile );
            }
            old = itr;
            ++itr;
        }
        else if( itr->Type == Lexem::PREPROCESSOR )
        {
            LLITR     start_of_line = itr;
            LLITR     end_of_line = ParsePreprocessor( lexems, itr, end );

            LexemList directive( start_of_line, end_of_line );

            if( SkipPragmas && directive.begin()->Value == "#pragma" )
            {
                itr = end_of_line;
                Lexem wspace;
                wspace.Type = Lexem::WHITESPACE;
                wspace.Value = " ";
                for( LLITR it = start_of_line; it != end_of_line;)
                {
                    ++it;
                    it = lexems.insert( it, wspace );
                    ++it;
                }
                continue;
            }

            itr = lexems.erase( start_of_line, end_of_line );

            std::string value = directive.begin()->Value;
            if( value == "#define" )
            {
                ParseDefine( define_table, directive );
            }
            else if( value == "#ifdef" )
            {
                std::string           def_name;
                ParseIf( directive, def_name );
                DefineTable::iterator dti = define_table.find( def_name );
                if( dti == define_table.end() )
                {
                    LLITR splice_to = ParseIfDef( itr, end );
                    itr = lexems.erase( itr, splice_to );
                }
            }
            else if( value == "#ifndef" )
            {
                std::string           def_name;
                ParseIf( directive, def_name );
                DefineTable::iterator dti = define_table.find( def_name );
                if( dti != define_table.end() )
                {
                    LLITR splice_to = ParseIfDef( itr, end );
                    itr = lexems.erase( itr, splice_to );
                }
            }
            else if( value == "#if" )
            {
                bool satisfied = EvaluateExpression( define_table, directive ) != 0;
                if( !satisfied )
                {
                    LLITR splice_to = ParseIfDef( itr, end );
                    itr = lexems.erase( itr, splice_to );
                }
            }
            else if( value == "#endif" )
            {
                // ignore
            }
            else if( value == "#include" )
            {
                if( LNT )
                    LNT->AddLineRange( PrependRootPath( filename ), start_line, CurrentLine - LinesThisFile );
                unsigned int save_lines_this_file = LinesThisFile;
                std::string  file_name;
                ParseIf( directive, file_name );

                std::string file_name_ = RemoveQuotes( file_name );
                if( IncludeTranslator )
                    IncludeTranslator->Call( file_name_ );
                if( std::find( FileDependencies.begin(), FileDependencies.end(), file_name_ ) == FileDependencies.end() )
                    FileDependencies.push_back( file_name_ );

                LexemList next_file;
                RecursivePreprocess( AddPaths( filename, file_name_ ), file_source, next_file, define_table );
                lexems.splice( itr, next_file );
                start_line = CurrentLine;
                LinesThisFile = save_lines_this_file;
                CurrentFile = filename;
                SetFileMacro( define_table, CurrentFile );
                SetLineMacro( define_table, LinesThisFile );
            }
            else if( value == "#pragma" )
            {
                ParsePragma( directive );
            }
            else if( value == "#message" )
            {
                std::string message;
                ParseTextLine( directive, message );
                PrintMessage( message );
            }
            else if( value == "#warning" )
            {
                std::string warning;
                ParseTextLine( directive, warning );
                PrintWarningMessage( warning );
            }
            else if( value == "#error" )
            {
                std::string error;
                ParseTextLine( directive, error );
                PrintErrorMessage( error );
            }
            else
            {
                PrintErrorMessage( "Unknown directive '" + value + "'." );
            }
        }
        else if( itr->Type == Lexem::IDENTIFIER )
        {
            itr = ExpandDefine( itr, end, lexems, define_table );
        }
        else
        {
            ++itr;
        }
    }

    if( LNT )
        LNT->AddLineRange( PrependRootPath( filename ), start_line, CurrentLine - LinesThisFile );
}