Exemplo n.º 1
0
int main()
{
	// push waypoints to a routes' map
	waypoints.push_back(waypoint(-4.0, -3.0)); // 0
	waypoints.push_back(waypoint(-3.0, -1.0)); // 1
	waypoints.push_back(waypoint(1.0, -4.0)); // 2
	waypoints.push_back(waypoint(3.0, -2.0)); // 3
	waypoints.push_back(waypoint(-1.0, 1.0)); // 4
	waypoints.push_back(waypoint(4.0, -3.0)); // 5
	waypoints.push_back(waypoint(2.0, 1.0)); // 6
	waypoints.push_back(waypoint(-2.0, 2.0)); // 7
	waypoints.push_back(waypoint(5.0, -1.0)); // 8
	waypoints.push_back(waypoint(6.0, 2.0)); // 9
	waypoints.push_back(waypoint(-1.0, 4.0)); // 10
	waypoints.push_back(waypoint(4.0, 4.0)); // 11
	waypoints.push_back(waypoint(8.0, 3.0)); // 12
	waypoints.push_back(waypoint(1.0, 6.0)); // 13
	waypoints.push_back(waypoint(6.0, 5.0)); // 14
	waypoints.push_back(waypoint(2.0, 7.0)); // 15
	waypoints.push_back(waypoint(7.0, 7.0)); // 16
	waypoints.push_back(waypoint(4.0, 7.0)); // 17
	waypoints.push_back(waypoint(6.0, 9.0)); // 18
	waypoints.push_back(waypoint(8.0, 9.0)); // 19

	obstacles.push_back(obstacle(0.0,5.0,1.0)); // 0
	obstacles.push_back(obstacle(3.0,3.0,2.0)); // 1
	obstacles.push_back(obstacle(3.5,0.0,1.0)); // 2
	obstacles.push_back(obstacle(8.0,7.0,2.5)); // 3

	// define exponential traverser type and create an exemplar
	typedef ExpTr<node_value,cost_value,expand,less<cost_value> > ExpTr_t;
	ExpTr_t j;

	cout << "in exp traverser forward search ...\n";
	j = ExpTr_t(0);
	// set a maximal lag length
	j.getexpfunc().setMaxLagLen(5.0); // or 6.0
	// add two defined handlers to enable calculation of an expansions number
	j.set_handler_on_expand_root(OnExpandRoot);
	j.set_handler_on_select_cursor(OnSelectCursor);
	// launch A* algorithm (combination of a predefined heuristics with a best - first
	// search that is provided by exponential traverser is A*!)
	ForwardSearch(j, goal);
	ShowPath(j);
	
	// show founded path
	cout << "number of expansions = " << num_expansions << '\n';
	cout << "...out \n\n";

	cout << "Search is completed.\n";

	return 0;

}
Exemplo n.º 2
0
/*-------------------------------------------------------------------------*/
APIRET OpenFile( CHAR *netfile )
{
    ReadOnly = FALSE;

    if(( PktFile = _fsopen( netfile, "rb+", SH_DENYWR )) == NULL )
    {
        if(( PktFile = _fsopen( netfile, "rb", SH_DENYWR )) == NULL )
        {
            ShowError( "Can't open file '%s'", ShowPath( netfile, 50 ));
            return( ERROR_OPEN_FAILED );
        }
        ReadOnly = TRUE;
    }
    
    setvbuf( PktFile, iobuf, _IOFBF, SIZEIOBUF );
    
    return( NO_ERROR );
}
Exemplo n.º 3
0
/*
*主函数
*/
int main()
{
    loop:
    printf("plase input the number of row m(m>0,m<100):");
    scanf("%d",&m);
    printf("plase input the number of line n(n>0,n<100):");
    scanf("%d",&n);
    if(m<0||m>100||n<0||n>100){
    printf("The number is error,process will exit !\n");
    exit(-1);
    }
    printf("The character is 'a',it is area.\n");
    printf("The character is 'b',it is wall.\n");
    printf("\n");
    InitMaze();
    printf("The oid Maze:\n");
    printf("\n");
    drawMaze();
    printf("\n show the path ?(y/n)");
    fflush(stdin);
    if(toupper(getch())=='Y')
    {
               printf("\n");
               ShowPath(); 
               printf("\n go on play ?(y/n)");
               fflush(stdin);
               if(toupper(getch())=='Y')
               {
                   goto loop;                 
               }
               else
               {
                   exit(1);
               }                
    }
    else
    {
             exit(1);
    }
    getch();
    return 0;
}
Exemplo n.º 4
0
main()
{
PATH path;
NODE root = {
0, /* 花费下界 */
{{{I, 1, 2, 7, 5}, /* 花费矩阵 */
  {1, I, 4, 4, 3},
  {2, 4, I, 1, 2},
  {7, 4, 1, I, 3},
  {5, 3, 2, 3, I}}, 5}, /* 城市数目 */
{{0}, 0}, /* 经历过的路径 */
NULL, NULL /* 左枝与右枝 */
};

/* 归约,建立根结点 */
root.bound += Simplify(&root.matrix);
/* 进入搜索循环 */
path = BABA(root);
ShowPath(path, root.matrix);

}
Exemplo n.º 5
0
int main()
{

// grid map
// vertical edges
    C[0][1] = 11;
    C[1][2] = 10;
    C[2][3] = 8;
    C[3][4] = 7;
    C[4][5] = 9;
    C[5][6] = 10;
    C[10][11] = 9;
    C[11][12] = 8;
    C[12][13] = 7;
    C[13][14] = 6;
    C[14][15] = 8;
    C[15][16] = 12;
    C[20][21] = 10;
    C[21][22] = 10;
    C[22][23] = 8;
    C[23][24] = 8;
    C[24][25] = 9;
    C[25][26] = 13;
    C[30][31] = 12;
    C[31][32] = 11;
    C[32][33] = 9;
    C[33][34] = 7;
    C[34][35] = 10;
    C[35][36] = 13;
    C[40][41] = 13;
    C[41][42] = 10;
    C[42][43] = 9;
    C[43][44] = 8;
    C[44][45] = 11;
    C[45][46] = 14;
    C[50][51] = 14;
    C[51][52] = 11;
    C[52][53] = 10;
    C[53][54] = 10;
    C[54][55] = 12;
    C[55][56] = 15;
    C[60][61] = 14;
    C[61][62] = 12;
    C[62][63] = 11;
    C[63][64] = 11;
    C[64][65] = 13;
    C[65][66] = 14;
    C[70][71] = 12;
    C[71][72] = 10;
    C[72][73] = 8;
    C[73][74] = 10;
    C[74][75] = 12;
    C[75][76] = 13;
    C[80][81] = 10;
    C[81][82] = 9;
    C[82][83] = 7;
    C[83][84] = 9;
    C[84][85] = 12;
    C[85][86] = 14;

// horizontal edges
    C[0][10] = 12;
    C[10][20] = 11;
    C[20][30] = 10;
    C[30][40] = 9;
    C[40][50] = 13;
    C[50][60] = 14;
    C[60][70] = 17;
    C[70][80] = 20;
    C[1][11] = 11;
    C[11][21] = 9;
    C[21][31] = 8;
    C[31][41] = 7;
    C[41][51] = 9;
    C[51][61] = 13;
    C[61][71] = 14;
    C[71][81] = 19;
    C[2][12] = 10;
    C[12][22] = 8;
    C[22][32] = 8;
    C[32][42] = 8;
    C[42][52] = 10;
    C[52][62] = 12;
    C[62][72] = 13;
    C[72][82] = 15;
    C[3][13] = 12;
    C[13][23] = 13;
    C[23][33] = 12;
    C[33][43] = 10;
    C[43][53] = 11;
    C[53][63] = 13;
    C[63][73] = 15;
    C[73][83] = 20;
    C[4][14] = 14;
    C[14][24] = 13;
    C[24][34] = 12;
    C[34][44] = 10;
    C[44][54] = 9;
    C[54][64] = 8;
    C[64][74] = 11;
    C[74][84] = 15;
    C[5][15] = 17;
    C[15][25] = 14;
    C[25][35] = 13;
    C[35][45] = 10;
    C[45][55] = 11;
    C[55][65] = 13;
    C[65][75] = 14;
    C[75][85] = 17;
    C[6][16] = 20;
    C[16][26] = 18;
    C[26][36] = 16;
    C[36][46] = 15;
    C[46][56] = 14;
    C[56][66] = 12;
    C[66][76] = 15;
    C[76][86] = 17;

// diagonal edges
    C[5][16] = 28;
    C[4][15] = 23;
    C[15][26] = 20;
    C[3][14] = 18;
    C[14][25] = 21;
    C[25][36] = 27;
    C[2][13] = 19;
    C[13][24] = 20;
    C[24][35] = 21;
    C[35][46] = 25;
    C[1][12] = 18;
    C[12][23] = 17;
    C[23][34] = 21;
    C[34][45] = 19;
    C[45][56] = 27;
    C[0][11] = 20;
    C[11][22] = 18;
    C[22][33] = 18;
    C[33][44] = 19;
    C[44][55] = 22;
    C[55][66] = 26;
    C[10][21] = 20;
    C[21][32] = 19;
    C[32][43] = 18;
    C[43][54] = 20;
    C[54][65] = 22;
    C[65][76] = 28;
    C[20][31] = 20;
    C[31][42] = 18;
    C[42][53] = 19;
    C[53][64] = 21;
    C[64][75] = 22;
    C[75][86] = 29;
    C[30][41] = 20;
    C[41][52] = 19;
    C[52][63] = 22;
    C[63][74] = 23;
    C[74][85] = 25;
    C[40][51] = 23;
    C[51][62] = 24;
    C[62][73] = 22;
    C[73][84] = 24;
    C[50][61] = 24;
    C[61][72] = 23;
    C[72][83] = 24;
    C[60][71] = 26;
    C[71][82] = 26;
    C[70][81] = 29;

    typedef LinTr<int,int,expand,greater<int> > LinTr_t; // linear ttraverser
    typedef ExpTr<int,int,expand,less<int> > ExpTr_t; // exponential traverser
    typedef ExpTr<int,int,bidir_expand,less<int> > RevExpTr_t; // second traverser for bi-directional search

    LinTr_t i;
    ExpTr_t j;
    RevExpTr_t k;

    // LINEAR TRAVERSER IN WORK
    /*
    cout << "in lin traverser forward search ...\n";
    cout << "eps = " << eps << '\n';
    i = LinTr_t(start);
    i.set_handler_on_expand_root(OnExpandRoot);
    i.set_handler_on_select_cursor(OnSelectCursor);
    ForwardSearch(i, goal);
    ShowPath(i);
    cout << "number of expansions = " << num_expansions << '\n';
    cout << "...out \n\n";
    /**/

    /*
    cout << "in lin traverser search bounded by " << thresh << "...\n";
    BoundedSearch(i = LinTr_t(start), goal, thresh);
    ShowPath(i);
    cout << "...out \n\n";

    cout << "in lin traverser search bounded by " << next_thresh << "... continue previous one ... \n";
    BoundedSearch(i, goal, next_thresh);
    ShowPath(i);
    cout << "...out \n\n";

    cout << "in lin traverser search bounded by " << next_thresh2 << "... continue previous one ...\n";
    BoundedSearch(i, goal, next_thresh2);
    ShowPath(i);
    cout << "...out \n\n";
    */

    /*
    cout << "in lin traverser search bounded by " << less_than_opt_value << "...\n";
    BoundedSearch(i = LinTr_t(start), goal, less_than_opt_value);
    ShowPath(i);
    cout << "...out \n\n";
    */

    /*
    cout << "in lin traverser optimal search bounded by " << thresh << " from initial state ...\n";
    i = LinTr_t(start);
    	BoundedOptSearch(i, goal, thresh);
    ShowPath(i);
    cout << "...out\n\n";
    */

    /*
    cout << "in lin traverser optimal search bounded by " << opt_value << " from initial state ...\n";
    	BoundedOptSearch(i = LinTr_t(start), goal, opt_value);
    ShowPath(i);
    cout << "...out\n\n";
    */

    /**/
    cout << "in lin traverser branch and bound ...\n";
    i = LinTr_t(start);
    int eps = i.getexpfunc().GetEps();
    cout << "eps = " << eps << '\n';
    i.set_handler_on_expand_root(OnExpandRoot);
    i.set_handler_on_select_cursor(OnSelectCursor);
    BranchAndBound(i, goal);
    ShowPath(i);
    cout << "number of expansions = " << num_expansions << '\n';
    cout << "...out \n\n";

    cout << "in lin traverser branch and bound ...\n";
    i = LinTr_t(start);
    i.getexpfunc().SetEps(10);
    expand &e = i.getexpfunc();
    e.SetEps(12);
    eps = i.getexpfunc().GetEps();
    cout << "eps = " << eps << "\n";
    i.set_handler_on_expand_root(OnExpandRoot);
    i.set_handler_on_select_cursor(OnSelectCursor);
    BranchAndBound(i, goal);
    ShowPath(i);
    cout << "number of expansions = " << num_expansions << '\n';
    cout << "...out\n\n";

    cout << "in lin traverser search bounded by " << 140 << "...\n";
    cout << "eps = " << eps << '\n';
    i = LinTr_t(start);
    i.set_handler_on_expand_root(OnExpandRoot);
    i.set_handler_on_select_cursor(OnSelectCursor);
    BoundedSearch(i, goal, 140);
    ShowPath(i);
    cout << "number of expansions = " << num_expansions << '\n';
    cout << "...out\n\n";

    /**/

// EXPONENTIAL TRAVERSER IN WORK
    /**/
    cout << "in exp traverser forward search ...\n";
    j = ExpTr_t(start);
    j.getexpfunc().SetEps(0);
    eps = j.getexpfunc().GetEps();
    cout << "eps = " << eps << '\n';
    j.set_handler_on_expand_root(OnExpandRoot);
    j.set_handler_on_select_cursor(OnSelectCursor);
    ForwardSearch(j, goal);
    ShowPath(j);
    cout << "number of expansions = " << num_expansions << '\n';
    cout << "...out \n\n";
    /**/


    cout << "in exp traverser search bounded by " << thresh << "...\n";
    BoundedSearch(j = ExpTr_t(start), goal, thresh);
    ShowPath(j);
    cout << "...out \n\n";
    /**/

    /*
    cout << "in exp traverser search bounded by " << next_thresh << "... continue previous one...\n";
    BoundedSearch(j, goal, next_thresh);
    ShowPath(j);
    cout << "...out \n\n";
    /**/

    /*
     	cout << "in exp traverser optimal search from initial state bounded by " << thresh << "...\n";
    BoundedOptSearch(j = ExpTr_t(start), goal, thresh);
    ShowPath(j);
    cout << "...out \n\n";
    /**/

    /*
     	cout << "in exp traverser branch and bound ...\n";
    BranchAndBound(j = ExpTr_t(start), goal);
    ShowPath(j);
    cout << "...out \n\n";
    */

    /**/
    cout << "in exp traverser iterative deepening ...\n";
    j = ExpTr_t(start);
    j.getexpfunc().SetEps(10);
    eps = j.getexpfunc().GetEps();
    cout << "eps = " << eps << '\n';
    j.set_handler_on_expand_root(OnExpandRoot);
    j.set_handler_on_select_cursor(OnSelectCursor);
    IterativeDeepening(j, goal);
    ShowPath(j);
    cout << "number of expansions = " << num_expansions << '\n';
    cout << "...out \n\n";
    /**/

    /*
    cout << "in exp traverser bi-directional search ...\n";
    BidirectionalSearch( j = ExpTr_t(start), k = RevExpTr_t(final) );
    ShowStickedPaths(j,k);
    cout << "...out \n\n";
    */

    cout << "All searches completed.\n";

    return 0;
}
Exemplo n.º 6
0
INT SetAttr( VOID )
{
    INT              attr, done, code;
    KEY              k;
    PACKET           packet;
    
    SetBar( BarNull );

    wattr = WDefine( 0, 6, 32, 23, 12, W_NOSCROLL | W_NOCURSOR | W_SHADOW, BORDER2, " Attributes " );

    WPrints ( wattr, 1,  0, "Private             <Alt-P>" );
    WPrints ( wattr, 1,  1, "Crash               <Alt-C>" );
    WPrints ( wattr, 1,  2, "Received            <Alt-R>" );
    WPrints ( wattr, 1,  3, "Sent                <Alt-S>" );
    WPrints ( wattr, 1,  4, "File Attach         <Alt-A>" );
    WPrints ( wattr, 1,  5, "Transit             <Alt-J>" );
    WPrints ( wattr, 1,  6, "Orphan              <Alt-O>" );
    WPrints ( wattr, 1,  7, "Kill/Sent           <Alt-K>" );
    WPrints ( wattr, 1,  8, "Local               <Alt-W>" );
    WPrints ( wattr, 1,  9, "Hold                <Alt-H>" );
    WPrints ( wattr, 1, 10, "File Request        <Alt-F>" );
    WPrints ( wattr, 1, 11, "Return Rcpt Request <Alt-M>" );
    WPrints ( wattr, 1, 12, "Return Rcpt         <Alt-N>" );
    WPrints ( wattr, 1, 13, "Audit Request       <Alt-Q>" );
    WPrints ( wattr, 1, 14, "File Update Request <Alt-U>" );
    WPrints ( wattr, 1, 15, "Zap all attribs     <Alt-Z>" );

    WActive( wattr );
    attr = Current -> attr;
    done = 0;
    while( !done )
    {
        ViewAttr();
        
        k = MGetKey( FALSE );
        
        switch( k.key )
        {
            case ALT_P: Current -> attr ^= PKT_PVT; break;
            case ALT_C: Current -> attr ^= PKT_CRA; break;
            case ALT_R: Current -> attr ^= PKT_RCV; break;
            case ALT_S: Current -> attr ^= PKT_SNT; break;
            case ALT_A: Current -> attr ^= PKT_ATT; break;
            case ALT_J: Current -> attr ^= PKT_TRS; break;
            case ALT_O: Current -> attr ^= PKT_ORP; break;
            case ALT_K: Current -> attr ^= PKT_KS;  break;
            case ALT_W: Current -> attr ^= PKT_LOC; break;
            case ALT_H: Current -> attr ^= PKT_HLD; break;
            case ALT_F: Current -> attr ^= PKT_FRQ; break;
            case ALT_M: Current -> attr ^= PKT_RRQ; break;
            case ALT_N: Current -> attr ^= PKT_RRC; break;
            case ALT_Q: Current -> attr ^= PKT_ARQ; break;
            case ALT_U: Current -> attr ^= PKT_URQ; break;
            case ALT_Z: Current -> attr  = 0;       break;
                
            case KEY_ESC: case KEY_ENTER: case KEY_GRENTER: case KEY_F10:
                done = 1;
                break;
        }
    }
    
    WRelease( wattr );
    
    if( k.key != KEY_ESC && attr != Current -> attr )
    {
        PktUpdate = 1;
        if( mode == FILE_TYPE_PKT )
        {
            FidoMsgGetStrSeek( PktFile, Current -> tell, SEEK_SET );
            if( fread ( &packet, sizeof( packet ), 1, PktFile ) != 1 )
                ShowError( "Error read file '%s'", ShowPath( netfile, 50 ));
            else
            {
                packet.attr = Current -> attr;
                FidoMsgGetStrSeek( PktFile, Current -> tell, SEEK_SET );
                if( fwrite( &packet, sizeof( packet ), 1, PktFile ) != 1 )
                    ShowError( "Error write to file '%s'", ShowPath( netfile, 50 ));
            }
        }
        if( mode == FILE_TYPE_MSG )
        {
            if(( code = OpenFile( Current -> name )) != 0 )
                return( code );
            if( fread( &msg, sizeof( msg ), 1, PktFile ) != 1 )
                ShowError( "Error read file '%s'", ShowPath( netfile, 50 ));
            else
            {
                msg.attrib = Current -> attr;
                fseek ( PktFile, 0, SEEK_SET );
                if( fwrite( &msg, sizeof( msg ), 1, PktFile ) != 1 )
                    ShowError( "Error write to file '%s'", ShowPath( netfile, 50 ));
            }
            CloseFile();
        }
    }
    else
        Current -> attr = attr;

    ViewAttr();

    return( 0 );
}
Exemplo n.º 7
0
/*-------------------------------------------------------------------------*/
APIRET ReadMsgFile( CHAR *netfile )
{
    APIRET           rc;
    CHAR            *string;
    INDEXPKT        *top;

    if(( rc = OpenFile( netfile )) != 0 )
        return( rc );
    
    if( fread( &msg, sizeof( msg ), 1, PktFile ) != 1 )
    {
        CloseFile();
        ShowError( "Error read file '%s'", ShowPath( netfile, 50 ));
        return( ERROR_READ_FAULT );
    }

    filename = netfile;

    string = malloc( MAX_BUFFER );

    if( string == NULL )
        return( ERROR_NOT_ENOUGH_MEMORY );

    pktcount = 1;

    AddNextStruct( pktIndex, top );

    if( top == NULL )
    {
        free( string );
        return( ERROR_NOT_ENOUGH_MEMORY );
    }

    Current = top;

    top -> AddrFrom.Zone      = 0;
    top -> AddrFrom.Net       = msg.orig_net;
    top -> AddrFrom.Node      = msg.orig_node;
    top -> AddrFrom.Point     = 0;
    top -> AddrFrom.Domain[0] = 0;

    top -> AddrTo.Zone        = 0;
    top -> AddrTo.Net         = msg.dest_net;
    top -> AddrTo.Node        = msg.dest_node;
    top -> AddrTo.Point       = 0;
    top -> AddrTo.Domain[0]   = 0;

    top -> attr               = msg.attrib;

    strcpy( string, msg.to   ); string[ MAX_TO ]   = 0; top -> to   = strdup( string );
    strcpy( string, msg.from ); string[ MAX_FROM ] = 0; top -> from = strdup( string );
    strcpy( string, msg.subj ); string[ MAX_SUBJ ] = 0; top -> subj = strdup( string );
    strcpy( string, msg.date ); string[ MAX_DATE ] = 0; top -> date = strdup( string );

    free( string );

    top -> area    = NULL;
    top -> sel     = ' ';
    top -> change  = 0;
    top -> size    = filelength( fileno( PktFile ));
    top -> name    = strdup( netfile );
    
    CloseFile();

    rc = ReadText( 1 );

    return( rc  );
}
Exemplo n.º 8
0
/*-------------------------------------------------------------------------*/
APIRET ReadText( INT check )
{
    APIRET           rc;
    INT              i = 0, len = 0;
    INT              x = maxXtext - 1;
    CHAR             last = '\n', *s, *string;
    INDEXTXT        *top, *start;

    FreeTxtIndex();

    string = malloc( MAX_BUFFER );

    if( string == NULL )
        return( ERROR_NOT_ENOUGH_MEMORY );

    if( mode == FILE_TYPE_PKT )
        FidoMsgGetStrSeek( PktFile, Current -> telltxt, SEEK_SET );

    if( mode == FILE_TYPE_MSG )
    {
        if(( rc = OpenFile( Current -> name )) != NO_ERROR )
            return( rc );
        if( fread( &msg, sizeof( msg ), 1, PktFile ) != 1 )
        {
            free( string );
            CloseFile();
            ShowError( "Error read file '%s'", ShowPath( Current -> name, 50 ));
            return( ERROR_READ_FAULT );
        }
        FidoMsgGetStrSeek( PktFile, 0, SEEK_CUR );
    }
        
    intl = origin = id = 0;
    MsgID.Zone = -1;
    
    while( last && FidoMsgGetStr( string, MAX_BUFFER - 1, PktFile, &last ) != NULL )
    {
        if( check )
            CheckString( string, i, Current );

        start = NULL;

        while( 1 )
        {
            len = strlen( string );
            s = &string[ len ];

            if( len > x )
            {
                if(( s = StrChar( string, x, ' ' )) == NULL )
                    if(( s = StrChar( string, x, ',' )) == NULL )
                        if(( s = StrChar( string, x, '.' )) == NULL )
                            if(( s = StrChar( string, x, ';' )) == NULL )
                                s = StrChar( string, x, ':' );
                if( s == NULL )
                {
                    len = x;
                    s = &string[len];
                }
                else
                {
                    len = s - string + 1;
                    s++;
                }
            }

            AddNextStruct( txtIndex, top );

            if( top == NULL )
            {
                free( string );
                return( ERROR_NOT_ENOUGH_MEMORY );
            }

            top -> str = malloc( len + 1 );
            memcpy( top -> str, string, len + 1 );
            top -> str[len] = 0;

            if( !start )
            {
                start = top;
                start -> color = GetLineColor( i, string );
            }
            else
                top -> color = start -> color;

            memcpy( string, s, strlen( s ) + 1 );

            txtcount++;

            if( string[0] == 0 )
                break;
        }
        i++;
        s = &start -> str[1];
    }

    free( string );
    
    if( check )
        CheckString( NULL, 0, Current );
    
    if( mode == FILE_TYPE_MSG )
        CloseFile();

    return( NO_ERROR );
}
Exemplo n.º 9
0
/*-------------------------------------------------------------------------*/
APIRET ReadPktFile( CHAR *netfile )
{
    APIRET           rc;
    INT              i, line;
    SHORT            sep;
    CHAR             last, *string;
    LONG             tell;
    PACKET           packet;
    INDEXPKT        *top;
#ifndef _LITE_
    INDEXPKT        *index, **inew;
    INT            (*func)() = NULL;
#endif

    if(( rc = OpenFile( netfile )) != 0 )
        return( rc );

    if( fread( &pkt, sizeof( pkt ), 1, PktFile ) != 1 )
    {
        CloseFile();
        ShowError( "Error read file '%s'", ShowPath( netfile, 50 ));
        return( ERROR_READ_FAULT );
    }
    if( pkt.ver != 2 )
    {
        CloseFile();
        ShowError( "File '%s' is't PKT", ShowPath( netfile, 50 ));
        return( ERROR_BAD_FORMAT );
    }

    string = malloc( MAX_BUFFER );

    if( string == NULL )
        return( ERROR_NOT_ENOUGH_MEMORY );

    WActive( wscan );

    while( 1 )
    {
        sep = 0;

        if( fread( &sep, sizeof( sep ), 1, PktFile ) != 1 )
        {
            ShowError( "Error structure in '%s'\nIncomplete pkt. Rest of packet ignored.", ShowPath( netfile, 50 ));
            BadPkt = 1;
            break;
        }

        tell = ftell( PktFile );
        
        if( sep == 0 )
        {
            if( fread( &sep, 1, 1, PktFile ) == 1 )
            {
                ShowError( "Error structure in '%s'\nFound more data, offset: 0x%08lX. Rest of packet ignored.", ShowPath( netfile, 50 ), tell );
                BadPkt = 1;
            }
            break;
        }

        if( sep != 2 )
        {
            ShowError( "Error structure in '%s'\nError separator: 0x%02hX, offset: 0x%08lX. Rest of packet ignored.", ShowPath( netfile, 50 ), sep, tell - 2 );
            BadPkt = 1;
            break;
        }

        if( fread( &packet, sizeof( packet ), 1, PktFile ) != 1 )
        {
            ShowError( "Error structure in '%s'\nIncomplete pkt. Rest of packet ignored.", ShowPath( netfile, 50 ));
            BadPkt = 1;
            break;
        }

        AddNextStruct( pktIndex, top );

        if( top == NULL )
        {
            free( string );
            WDeactive( wscan );
            return( ERROR_NOT_ENOUGH_MEMORY );
        }

        top -> tell = tell;

        if( !( pktcount % 5 ))
        {
            WSetXY ( wscan, 19, 2 );
            if( pktcount == 0 )
                WPrintf( wscan, "%4u", 1 );
            else
                WPrintf( wscan, "%4u", pktcount );
        }

        top -> AddrFrom.Zone      = 0;
        top -> AddrFrom.Net       = packet.orig_net;
        top -> AddrFrom.Node      = packet.orig_node;
        top -> AddrFrom.Point     = 0;
        top -> AddrFrom.Domain[0] = 0;

        top -> AddrTo.Zone        = 0;
        top -> AddrTo.Net         = packet.dest_net;
        top -> AddrTo.Node        = packet.dest_node;
        top -> AddrTo.Point       = 0;
        top -> AddrTo.Domain[0]   = 0;

        top -> attr               = packet.attr;

        strncpy( string, packet.date, sizeof( packet.date ));
        string[sizeof(packet.date)] = 0; top -> date = strdup( string );

        FidoMsgGetStrSeek( PktFile, 0, SEEK_CUR );

        FidoMsgGetStr( string, MAX_BUFFER - 1, PktFile, &last );
        string[ MAX_TO ] = 0; top -> to = strdup( string );

        FidoMsgGetStr( string, MAX_BUFFER - 1, PktFile, &last );
        string[ MAX_FROM ] = 0; top -> from = strdup( string );

        FidoMsgGetStr( string, MAX_BUFFER - 1, PktFile, &last );
        string[ MAX_SUBJ ] = 0; top -> subj = strdup( string );

        top -> area   = NULL;
        top -> sel    = ' ';
        top -> change = 0;

        for( i = 0; i < MAX_LINE; i++ )
            top -> quick[i] = NULL;

        top -> telltxt = FidoMsgGetStrTell();
        line = i = 0;

        while( FidoMsgGetStr( string, MAX_BUFFER - 1, PktFile, &last ) != NULL )
        {
            CheckString( string, i, top );

            if( i && strncmp( string, "AREA:", 5 ))
            {
                if( Quick && line < MAX_LINE && string[0] != '\001' && StrSkipSpace( string ))
                {
                    top -> quick[line] = strdup( string );
                    top -> color[line] = GetLineColor( i, string );
                    line++;
                }
            }
            if( last == 0 )
                break;
            i++;
        }

        CheckString( NULL, 0, top );

        top -> size = FidoMsgGetStrTell() - top -> telltxt - 1;
        fseek( PktFile, FidoMsgGetStrTell(), SEEK_SET );
        pktcount++;
    }

    free( string );

#ifndef _LITE_

    if( cfgCurrent.sort && pktcount )
    {
        pktOrigin = malloc( pktcount * sizeof( *pktOrigin ));
        if( pktOrigin )
            for( i = 0, index = pktIndex; index; index = index -> next, i++ )
                pktOrigin[i] = index;

        if( cfgCurrent.psort && ( inew = calloc( pktcount, sizeof( *inew ))) != NULL )
        {
            for( i = 0, index = pktIndex; index; index = index -> next, i++ )
                inew[i] = index;

            if( typesort.from ) func = SortFrom;
            if( typesort.to   ) func = SortTo;
            if( typesort.area ) func = SortArea;
            if( typesort.subj ) func = SortSubj;
            if( typesort.date ) func = SortDate;
            if( typesort.size ) func = SortSize;

            if( func )
                MShellSort( inew, pktcount, sizeof( *inew ), func );

            pktIndex = inew[0];
        
            for( i = 1; i < pktcount; i++ )
                inew[i-1] -> next = inew[i];
            inew[i-1] -> next = NULL;

            free( inew );
        }
    }

#endif
    
    WDeactive( wscan );

    return( NO_ERROR );
}