home *** CD-ROM | disk | FTP | other *** search
/ FM Towns: Free Software Collection 11 / FM Towns Free Software Collection 11.iso / t_os / lib / objcol2 / src / filesel.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-08-12  |  10.0 KB  |  506 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <dir.h>
  4. #include <sys/extender.h>
  5. #include <register.h>
  6. #include <libio.h>
  7. #include <egb.h>
  8. #include "sort.h"
  9. #include "symbol.h"
  10. #include "fsdat.c"
  11.  
  12. #define ERR        1
  13. #define NOERR    0
  14.  
  15. #define A_RAF    0x00
  16. #define A_ROF    0x01
  17. #define A_HND    0x02
  18. #define A_SYS    0x04
  19. #define A_SUB    0x10
  20.  
  21. #define    ON        1
  22. #define    OFF        0
  23.  
  24. typedef struct
  25. {
  26.     char    fullname[16];
  27.     char    outname[16];
  28.     char    attrib;
  29. } FILEINF ;
  30.  
  31.  
  32. extern int    FS_fileSelect( char *, char *, char * );
  33. extern int    FS_locate( int, int );
  34. static int    FS_x = 0;
  35. static int    FS_y = 0;
  36. static char    c_dir[256];
  37. static char    drv[2];
  38. static char *egbwork;
  39. static int    diskch( int d );
  40.  
  41. static int    diskch( int d )
  42. {
  43.     struct    SREGS    seg;
  44.     union    REGS    creg,rreg;
  45.  
  46.     int        s,o;
  47.     char    buf[512];
  48.  
  49.     nativetoreal( buf, 1, &s, &o );
  50.     segread( &seg );
  51.     creg.h.al = d;
  52.     creg.x.bx = o;
  53.     seg.ss = s;
  54.     creg.x.cx = 0;
  55.     creg.x.dx = 0;
  56.     intreal( 0x25, &creg, &rreg, &seg );
  57.  
  58.     return ( rreg.x.cflag & 1 );
  59. }
  60.  
  61. int        FS_locate( int x, int y )
  62. {
  63.     if( x<0 || x>511 || y<0 || y>263 )
  64.         return ERR;
  65.  
  66.     FS_x = x;
  67.     FS_y = y;
  68.  
  69.     return NOERR;
  70. }
  71.  
  72. static void    FS_end( char *grp_buf )
  73. {
  74.     MOS_disp( 0 );
  75.     EGB_writeMode( egbwork, 0 );
  76.     EGB_putRect( egbwork, 0, grp_buf, FS_x, FS_y, 127+FS_x, 215+FS_y );
  77.     return;
  78. }
  79.  
  80. static int    upButton( int cx, int cy )
  81. {
  82.     static int    UB_flg = OFF;
  83.  
  84.     if( cx<108+FS_x || cy<49+FS_y || cx>122+FS_x || cy>63+FS_y )
  85.     {
  86.         if( UB_flg == ON )
  87.         {
  88.             UB_flg = OFF;
  89.             MOS_disp( 0 );
  90.             EGB_writeMode( egbwork, 4 );
  91.             EGB_paintMode( egbwork, 0x22 );
  92.             EGB_foreColor( egbwork, 15 );
  93.             EGB_paintColor( egbwork, 15 );
  94.             EGB_box( egbwork, 108+FS_x, 49+FS_y, 122+FS_x, 63+FS_y );
  95.             MOS_disp( 1 );
  96.         }
  97.         return 0;
  98.     }
  99.  
  100.     if( UB_flg != ON )
  101.     {
  102.         UB_flg = ON;
  103.         MOS_disp( 0 );
  104.         EGB_writeMode( egbwork, 4 );
  105.         EGB_paintMode( egbwork, 0x22 );
  106.         EGB_foreColor( egbwork, 15 );
  107.         EGB_paintColor( egbwork, 15 );
  108.         EGB_box( egbwork, 108+FS_x, 49+FS_y, 122+FS_x, 63+FS_y );
  109.         MOS_disp( 1 );
  110.     }
  111.  
  112.     return 1;
  113. }
  114.  
  115. static int    downButton( int cx, int cy )
  116. {
  117.     static int    DB_flg = OFF;
  118.  
  119.     if( cx<108+FS_x || cy<196+FS_y || cx>122+FS_x || cy>210+FS_y )
  120.     {
  121.         if( DB_flg == ON )
  122.         {
  123.             DB_flg = OFF;
  124.             MOS_disp( 0 );
  125.             EGB_writeMode( egbwork, 4 );
  126.             EGB_paintMode( egbwork, 0x22 );
  127.             EGB_foreColor( egbwork, 15 );
  128.             EGB_paintColor( egbwork, 15 );
  129.             EGB_box( egbwork, 108+FS_x, 196+FS_y, 122+FS_x, 210+FS_y );
  130.             MOS_disp( 1 );
  131.         }
  132.         return 0;
  133.     }
  134.  
  135.     if( DB_flg != ON )
  136.     {
  137.         DB_flg = ON;
  138.         MOS_disp( 0 );
  139.         EGB_writeMode( egbwork, 4 );
  140.         EGB_paintMode( egbwork, 0x22 );
  141.         EGB_foreColor( egbwork, 15 );
  142.         EGB_paintColor( egbwork, 15 );
  143.         EGB_box( egbwork, 108+FS_x, 196+FS_y, 122+FS_x, 210+FS_y );
  144.         MOS_disp( 1 );
  145.     }
  146.  
  147.     return 1;
  148. }
  149.  
  150. static int    incDriveNum( int cx, int cy )
  151. {
  152.     static int    ID_flg = OFF;
  153.  
  154.     if( cx<39+FS_x || cy<27+FS_y || cx>55+FS_x || cy>44+FS_y )
  155.     {
  156.         if( ID_flg == ON )
  157.         {
  158.             ID_flg = OFF;
  159.             MOS_disp( 0 );
  160.             EGB_writeMode( egbwork, 4 );
  161.             EGB_paintMode( egbwork, 0x22 );
  162.             EGB_foreColor( egbwork, 15 );
  163.             EGB_paintColor( egbwork, 15 );
  164.             EGB_box( egbwork, 39+FS_x, 27+FS_y, 55+FS_x, 44+FS_y );
  165.             MOS_disp( 1 );
  166.         }
  167.         return 0;
  168.     }
  169.  
  170.     if( ID_flg != ON )
  171.     {
  172.         ID_flg = ON;
  173.         MOS_disp( 0 );
  174.         EGB_writeMode( egbwork, 4 );
  175.         EGB_paintMode( egbwork, 0x22 );
  176.         EGB_foreColor( egbwork, 15 );
  177.         EGB_paintColor( egbwork, 15 );
  178.         EGB_box( egbwork, 39+FS_x, 27+FS_y, 55+FS_x, 44+FS_y );
  179.         MOS_disp( 1 );
  180.     }
  181.  
  182.     return 1;
  183.  
  184. }
  185.  
  186. static int    decDriveNum( int cx, int cy )
  187. {
  188.     static int    DD_flg = OFF;
  189.  
  190.     if( cx<9+FS_x || cy<27+FS_y || cx>25+FS_x || cy>44+FS_y )
  191.     {
  192.         if( DD_flg == ON )
  193.         {
  194.             DD_flg = OFF;
  195.             MOS_disp( 0 );
  196.             EGB_writeMode( egbwork, 4 );
  197.             EGB_paintMode( egbwork, 0x22 );
  198.             EGB_foreColor( egbwork, 15 );
  199.             EGB_paintColor( egbwork, 15 );
  200.             EGB_box( egbwork, 9+FS_x, 27+FS_y, 25+FS_x, 44+FS_y );
  201.             MOS_disp( 1 );
  202.         }
  203.         return 0;
  204.     }
  205.  
  206.     if( DD_flg != ON )
  207.     {
  208.         DD_flg = ON;
  209.         MOS_disp( 0 );
  210.         EGB_writeMode( egbwork, 4 );
  211.         EGB_paintMode( egbwork, 0x22 );
  212.         EGB_foreColor( egbwork, 15 );
  213.         EGB_paintColor( egbwork, 15 );
  214.         EGB_box( egbwork, 9+FS_x, 27+FS_y, 25+FS_x, 44+FS_y );
  215.         MOS_disp( 1 );
  216.     }
  217.  
  218.     return 1;
  219. }
  220.  
  221. static int    fileNameCheck( int cx, int cy )
  222. {
  223.     static int    s = 0;
  224.  
  225.     if( cx<10+FS_x || cy<50+FS_y || cx>105+FS_x || cy>209+FS_y )
  226.     {
  227.         if( s > 0 )
  228.         {
  229.             s = 0;
  230.         }
  231.         return 0;
  232.     }
  233.  
  234.     if( s != (cy-FS_y-50)/16 + 1 )
  235.     {
  236.         s = (cy-FS_y-50)/16 + 1;
  237.     }
  238.  
  239.     return s;
  240. }
  241.  
  242. int        FS_fileSelect( char *w, char *wild, char *path )
  243. {
  244.     struct ffblk    fb;
  245.     int        attrib = 0x17;
  246.     char    wildcard[60];
  247.     FILEINF    f_inf[128];
  248.     int        fnum,ptop,i,j,sw,cx,cy,sel,loop_f;
  249.     char    *fname;
  250.     char    buf1[64];
  251.     char    testname[256];
  252.     char    grp_buf[13824];
  253.     char    *s,*t,buf2[8];
  254.     char    *sortbuf[128];
  255.     int        sortlank[128];
  256.  
  257.     egbwork = w;
  258.     drv[0] = 'A' + _dos_get_default_disk_number();
  259.     drv[1] = 0;
  260. /*    getcwd( c_dir, 60 );*/
  261.     _dos_get_current_directory( drv[0] - 'A' + 1, buf1 );
  262.     strcpy( c_dir, "\\" );
  263.     strcat( c_dir, buf1 );
  264.  
  265.     EGB_writeMode( egbwork, 0 );
  266.     EGB_getRect( egbwork, grp_buf, FS_x, FS_y, 127+FS_x, 215+FS_y );
  267.     EGB_putRect( egbwork, 0, FS_grp_data, FS_x, FS_y, 127+FS_x, 215+FS_y );
  268.  
  269. START:
  270.     MOS_disp( 0 );
  271.  
  272.     strcpy( wildcard, drv );
  273.     strcat( wildcard, ":" );
  274.     strcat( wildcard, c_dir );
  275.     if( *(c_dir+1) )
  276.         strcat( wildcard, "\\" );
  277.     strcat( wildcard, wild );
  278.  
  279.     fnum = 0;
  280.     if( findfirst( wildcard, &fb, attrib ) == 0 )
  281.     {
  282.         strcpy( f_inf[fnum].fullname, fb.ff_name );
  283.         s = buf1;
  284.         t = fb.ff_name;
  285.         while( *t != 0 && *t != '.' )
  286.         {
  287.             *s++ = *t++;
  288.         }
  289.         *s = 0;
  290.  
  291.         if( *t == 0 )
  292.             *buf2 = 0;
  293.         else
  294.         {
  295.             ++t;
  296.             strcpy( buf2, t );
  297.         }
  298.         if( (f_inf[fnum].attrib = fb.ff_attrib) & A_SUB )
  299.         {
  300.             strcpy( f_inf[fnum].outname, "<" );
  301.             strcat( f_inf[fnum].outname, fb.ff_name );
  302.             while( strlen( f_inf[fnum].outname ) < 11 )
  303.                 strcat( f_inf[fnum].outname, " " );
  304.             strcat( f_inf[fnum].outname, ">" );
  305.         }
  306.         else
  307.         {
  308.             strcpy( f_inf[fnum].outname, buf1 );
  309.             while( strlen( f_inf[fnum].outname ) < 8 )
  310.                 strcat( f_inf[fnum].outname, " " );
  311.             strcat( f_inf[fnum].outname, "." );
  312.             strcat( f_inf[fnum].outname, buf2 );
  313.         }
  314.         ++fnum;
  315.  
  316.         while( findnext( &fb ) == 0 )
  317.         {
  318.             strcpy( f_inf[fnum].fullname, fb.ff_name );
  319.             s = buf1;
  320.             t = fb.ff_name;
  321.             while( *t != 0 && *t != '.' )
  322.             {
  323.                 *s++ = *t++;
  324.             }
  325.             *s = 0;
  326.  
  327.             if( *t == 0 )
  328.                 *buf2 = 0;
  329.             else
  330.             {
  331.                 ++t;
  332.                 strcpy( buf2, t );
  333.             }
  334.             if( (f_inf[fnum].attrib = fb.ff_attrib) & A_SUB )
  335.             {
  336.                 strcpy( f_inf[fnum].outname, "<" );
  337.                 strcat( f_inf[fnum].outname, fb.ff_name );
  338.                 while( strlen( f_inf[fnum].outname ) < 11 )
  339.                     strcat( f_inf[fnum].outname, " " );
  340.                 strcat( f_inf[fnum].outname, ">" );
  341.             }
  342.             else
  343.             {
  344.                 strcpy( f_inf[fnum].outname, buf1 );
  345.                 while( strlen( f_inf[fnum].outname ) < 8 )
  346.                     strcat( f_inf[fnum].outname, " " );
  347.                 strcat( f_inf[fnum].outname, "." );
  348.                 strcat( f_inf[fnum].outname, buf2 );
  349.             }
  350.             ++fnum;
  351.         }
  352.     }
  353.  
  354.     for( i=0; i<fnum; i++ )
  355.         sortbuf[i] = f_inf[i].outname;
  356.     sort( SORT_SHELL, SORT_STR, sortbuf, fnum, sortlank );
  357.  
  358.     EGB_writeMode( egbwork, 0 );
  359.     EGB_paintMode( egbwork, 0x22 );
  360.     EGB_foreColor( egbwork, 0 );
  361.     EGB_paintColor( egbwork, 0 );
  362.     EGB_box( egbwork, 10+FS_x, 8+FS_y, 105+FS_x, 23+FS_y );
  363.     EGB_box( egbwork, 58+FS_x, 28+FS_y, 105+FS_x, 43+FS_y );
  364.     EGB_box( egbwork, 28+FS_x, 28+FS_y, 36+FS_x, 43+FS_y );
  365.  
  366.     EGB_foreColor( egbwork, 15 );
  367.     strcpy( buf1, c_dir );
  368.     buf1[12] = 0;
  369.     symbol( egbwork, 10+FS_x, 23+FS_y, buf1 );
  370.     symbol( egbwork, 58+FS_x, 43+FS_y, wild );
  371.     symbol( egbwork, 28+FS_x, 43+FS_y, drv );
  372.  
  373.     do
  374.     {
  375.         MOS_rdpos( &sw, &cx, &cy );
  376.     }
  377.     while( sw );
  378.  
  379.     loop_f = 1;
  380.     ptop = 0;
  381.     while( loop_f )
  382.     {
  383.         EGB_writeMode( egbwork, 0 );
  384.         EGB_paintMode( egbwork, 0x22 );
  385.         EGB_foreColor( egbwork, 0 );
  386.         EGB_paintColor( egbwork, 0 );
  387.         EGB_box( egbwork, 10+FS_x, 50+FS_y, 105+FS_x, 209+FS_y );
  388.  
  389.         EGB_foreColor( egbwork, 15 );
  390.         for( j=0; j<10; j++ )
  391.         {
  392.             if( ptop+j < fnum )
  393.                 symbol( egbwork, 10+FS_x, j*16+65+FS_y, f_inf[sortlank[ptop+j]].outname );
  394.         }
  395.  
  396.         MOS_disp( 1 );
  397.  
  398.         sw = 0;
  399.         while( (sw & 2) == 0 )
  400.         {
  401.             MOS_rdpos( &sw, &cx, &cy );
  402.  
  403.             if( incDriveNum( cx, cy ) && (sw & 1) )
  404.             {
  405.                 i = 0;
  406.                 while(1)
  407.                 {
  408.                     drv[0] = ( drv[0] == 'Z' )? 'A':++(drv[0]);
  409.                     if( diskch( drv[0] - 'A' ) == 0 )
  410.                         break;
  411.                 }
  412.                 _dos_get_current_directory( drv[0] - 'A' + 1, buf1 );
  413.                 strcpy( c_dir, "\\" );
  414.                 strcat( c_dir, buf1 );
  415.                 loop_f = 0;
  416.                 sw = 5;
  417.                 break;
  418.             }
  419.             if( decDriveNum( cx, cy ) && (sw & 1) )
  420.             {
  421.                 i = 0;
  422.                 while(1)
  423.                 {
  424.                     drv[0] = ( drv[0] == 'A' )? 'Z':--(drv[0]);
  425.                     if( diskch( drv[0] - 'A' ) == 0 )
  426.                         break;
  427.                 }
  428.                 _dos_get_current_directory( drv[0] - 'A' + 1, buf1 );
  429.                 strcpy( c_dir, "\\" );
  430.                 strcat( c_dir, buf1 );
  431.                 loop_f = 0;
  432.                 sw = 5;
  433.                 break;
  434.             }
  435.  
  436.             if( upButton( cx, cy ) && (sw & 1) && ptop>0 )
  437.             {
  438.                 --ptop;
  439.                 break;
  440.             }
  441.             if( downButton( cx, cy ) && (sw & 1) && ptop+10<fnum )
  442.             {
  443.                 ++ptop;
  444.                 break;
  445.             }
  446.             if( ( sel = fileNameCheck( cx, cy ) ) && (sw & 1) )
  447.             {
  448.                 if( ptop+sel-1 < fnum )
  449.                 {
  450.                     loop_f = 0;
  451.                     break;
  452.                 }
  453.             }
  454.         }
  455.         if( sw & 2 )
  456.         {
  457.             FS_end( grp_buf );
  458.             return ERR;
  459.         }
  460.     }
  461.  
  462.     if( sw == 5 )
  463.         goto START;
  464.  
  465.     if( f_inf[sortlank[ptop+sel-1]].attrib & A_SUB )
  466.     {
  467.         if( strcmp( f_inf[sortlank[ptop+sel-1]].fullname, "." ) == 0 )
  468.         {
  469.             goto START;
  470.         }
  471.         if( strcmp( f_inf[sortlank[ptop+sel-1]].fullname, ".." ) == 0 )
  472.         {
  473.             char    *s;
  474.             s = c_dir;
  475.  
  476.             while( *s )
  477.                 ++s;
  478.  
  479.             while( *s != '\\' )
  480.                 --s;
  481.  
  482.             if( s == c_dir )
  483.                 *(c_dir+1) = 0;
  484.             else
  485.                 *s = 0;
  486.  
  487.             goto START;
  488.         }
  489.  
  490.         if( *(c_dir+1) )
  491.             strcat( c_dir, "\\" );
  492.         strcat( c_dir, f_inf[sortlank[ptop+sel-1]].fullname );
  493.         goto START;
  494.     }
  495.  
  496.     strcpy( path, drv );
  497.     strcat( path, ":" );
  498.     strcat( path, c_dir );
  499.     if( *(c_dir+1) )
  500.         strcat( path, "\\" );
  501.     strcat( path, f_inf[sortlank[ptop+sel-1]].fullname );
  502.  
  503.     FS_end( grp_buf );
  504.     return NOERR;
  505. }
  506.