home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / pmfo002a.zip / cf.c < prev    next >
C/C++ Source or Header  |  1998-12-31  |  5KB  |  221 lines

  1. //
  2. // C(hange)F(ont) v0.01
  3. //
  4. //=============================================================
  5. // (c) VicTor Smirnoff, 1998            <root@water.karelia.su>
  6. //-------------------------------------------------------------
  7. // tFont v1.0 by Tomas Oegren was used as prototype
  8. // (c) Tomas Oegren, 1996                <stric@freenet.hut.fi>
  9. //=============================================================
  10. #define INCL_VIO
  11. #define INCL_DOSERRORS
  12.  
  13. #include <os2.h>
  14. #include <stdlib.h>
  15. #include <stdio.h>
  16. #include <string.h>
  17.  
  18. #define MY_ERROR_SIZE   ERROR_USER_DEFINED_BASE+1
  19.  
  20. void main( int, char ** );
  21. void PrintError( APIRET, char * );
  22. void HowUse( void );
  23. APIRET ReadFromFile( char *, char *, int );
  24.  
  25. void main( int argc, char ** argv )
  26. {
  27.    VIOFONTINFO font;
  28.    VIOMODEINFO mode;
  29.    APIRET      rc;
  30.    PVOID       buf;
  31.    USHORT      cp;
  32.    BOOL        onlyShow;
  33.    char        fontFile[_MAX_PATH];
  34.  
  35.    onlyShow = (argc<=1);
  36.  
  37.    if ( argc>2 )
  38.    {
  39.       HowUse();
  40.       return;
  41.    }
  42.  
  43.    if ( argc==2 )
  44.    {
  45.       if ( !strcmp(argv[1],"-?") ||
  46.            !strcmp(argv[1],"-h") ||
  47.            !strcmp(argv[1],"-H") )
  48.       {
  49.          HowUse();
  50.          return;
  51.       } else strcpy(fontFile,argv[1]);
  52.    }
  53.  
  54.    mode.cb = sizeof(VIOMODEINFO);
  55.  
  56.    rc = VioGetMode( &mode, 0 );
  57.    if (rc)
  58.    {
  59.       PrintError( rc , "VioGetMode" );
  60.       return;
  61.    }
  62.  
  63.    if ( onlyShow )
  64.    {
  65.       printf("\n  Mode: 0x0%x\n Color: %lu\nColumn: %lu\n   Row: %lu\n H-res: %lu\n V-res: %lu\n",
  66.          mode.fbType, mode.color, mode.col, mode.row, mode.hres, mode.vres);
  67.  
  68.       rc = VioGetCp( 0, &cp, 0 );
  69.       if (rc)
  70.       {
  71.          if (rc!=ERROR_VIO_USER_FONT)
  72.          {
  73.             PrintError( rc , "VioGetCp" );
  74.             return;
  75.          }
  76.       }
  77.       
  78.       if (rc!=ERROR_VIO_USER_FONT)  printf("\nCodepage: %lu\n", cp);
  79.       else  printf("\nCodepage: selected user font table\n");
  80.    }
  81.  
  82.    if ( !onlyShow && ((mode.hres>640) || (mode.vres>400)) )
  83.    {
  84.       if (mode.hres>640) mode.hres = 640;
  85.       if (mode.vres>400) mode.vres = 400;
  86.  
  87.       mode.cb = 12;
  88.  
  89.       rc = VioSetMode( &mode, 0 );
  90.       if (rc)
  91.       {
  92.          PrintError( rc , "VioSetMode" );
  93.          return;
  94.       }
  95.  
  96.    }
  97.  
  98.    font.cb = sizeof(VIOFONTINFO);
  99.    font.type = 0;
  100.    font.cbData = 0;
  101.    font.pbData = NULLHANDLE;
  102.  
  103.    rc = VioGetFont( &font, 0 );
  104.    if (rc)
  105.    {
  106.       PrintError( rc , "VioGetFont" );
  107.       return;
  108.    }
  109.  
  110.    if ( onlyShow )
  111.    {
  112.       printf("\nFontsize: %lux%lu\nTotal bytes needed for font: %lu\n",
  113.          font.cxCell, font.cyCell, font.cbData);
  114.       return;
  115.    }
  116.  
  117.    rc = DosAllocMem( (PPVOID)&buf,
  118.                      (ULONG)font.cbData,
  119.            PAG_WRITE | PAG_READ | PAG_COMMIT | OBJ_TILE );
  120.    if (rc)
  121.    {
  122.       PrintError( rc , "DosAllocMem" );
  123.       return;
  124.    }
  125.  
  126.    rc = ReadFromFile( fontFile, buf, font.cbData );
  127.    if (rc)
  128.    {
  129.       PrintError( rc , fontFile );
  130.       DosFreeMem( buf );
  131.       return;
  132.    }
  133.  
  134.    font.cb = sizeof(VIOFONTINFO);
  135.    font.type = 0;
  136.    font.pbData = buf;    // thunk pointer32 to pointer16
  137.  
  138.    rc = VioSetFont( &font, 0 );
  139.    if (rc)  PrintError( rc , "VioSetFont" );
  140.    DosFreeMem( buf );
  141.  
  142.    return;
  143.  
  144. }
  145.  
  146. void HowUse(void)
  147. {
  148.    printf("\nC(hange)F(ont) v0.01\n\n");
  149.    printf("Usage:\tcf.exe [-?|-h|<filename>]\n");
  150.    printf("\twithout params      Display status only\n");
  151.    printf("\t<filename>          Name of a font file\n");
  152.    printf("\t-?                  This help\n");
  153.    printf("\t-h                  This help\n");
  154.    printf("\nThis program changes the current font in a fullscreen OS/2 VIO\n\n");
  155. }
  156.  
  157. void PrintError( APIRET rc, char * t )
  158. {
  159.    switch (rc)
  160.    {
  161.    case ERROR_VIO_EXTENDED_SG:
  162.       printf("\nYou can't run this program in an Windowed session.\n");
  163.       break;
  164.    case ERROR_VIO_MODE:
  165.       printf("\nSorry, your drivers does not support this call.\nYou must exit this session, since it's \"broken\" now.\n");
  166.       break;
  167.    case MY_ERROR_SIZE:
  168.       printf("\nFilesize differs from size needed for the font or\nan read error has occurred\n");
  169.       break;
  170.    default:
  171.       printf("\n%s: an error has occurred, errorcode = %ld\n",t,rc);
  172.       break;
  173.    }
  174. }
  175.  
  176. APIRET ReadFromFile( char * fileName, char * dataBuffer, int dataSize )
  177. {
  178.    ULONG        FileInfoLevel;
  179.    FILESTATUS3  FileInfoBuf;
  180.    ULONG        FileInfoBufSize;
  181.    ULONG userAct, fileSize;
  182.    HFILE hFile;
  183.    APIRET rc;
  184.  
  185.    userAct = FILE_EXISTED;
  186.    rc = DosOpen( fileName,
  187.                  &hFile,
  188.                  &userAct,
  189.                  (ULONG)0L,
  190.                  FILE_NORMAL,
  191.                  OPEN_ACTION_OPEN_IF_EXISTS,
  192.                  OPEN_FLAGS_FAIL_ON_ERROR |
  193.                  OPEN_SHARE_DENYWRITE |
  194.                  OPEN_ACCESS_READONLY,
  195.                  NULL );
  196.    if ( rc ) return(rc);
  197.  
  198.    FileInfoLevel = 1;
  199.    FileInfoBufSize = sizeof(FILESTATUS3);
  200.    rc = DosQueryFileInfo(hFile, FileInfoLevel,
  201.                          &FileInfoBuf, FileInfoBufSize);
  202.    fileSize = FileInfoBuf.cbFile;
  203.    if ( rc ) return(rc);
  204.    if ( fileSize!=dataSize ) return(MY_ERROR_SIZE);
  205.  
  206.    rc = DosRead( hFile,
  207.                  dataBuffer,
  208.                  fileSize,
  209.                  &userAct );
  210.    if ( rc )
  211.    {
  212.       DosClose(hFile);
  213.       return(rc);
  214.    }
  215.    DosClose(hFile);
  216.    if ( userAct!=dataSize ) return(MY_ERROR_SIZE);
  217.    return(0);
  218. }
  219.  
  220.  
  221.