home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 8 Other / 08-Other.zip / tfont10.zip / tFont.c < prev    next >
C/C++ Source or Header  |  1996-08-13  |  4KB  |  130 lines

  1. #define COPYRIGHT "(C) Tomas Ögren '96"
  2. #define INCL_VIO                          /* I want some Vio functions.. */
  3. #include <os2.h>                                         /* Misc stuff.. */
  4. #include <os2thunk.h>  /* included for the 32 bit to 16 bit pointer conv */
  5. #include <stdio.h>                            /* standard input output.. */
  6. #include <getopt.h>                           /* for the option switches */
  7.  
  8. int opt_displayonly=0,opt_force=0;                       /* some options */
  9.  
  10. ULONG filesize(FILE*f)                           /* returns the filesize */
  11. {
  12.     ULONG i,j;
  13.     i=ftell(f);
  14.     fseek(f,0,SEEK_END);
  15.     j=ftell(f);
  16.     fseek(f,i,SEEK_SET);
  17.     return j;
  18. }
  19.  
  20. int RUSure(char*prompt)                               /* Are you sure... */
  21. {
  22.     int c=255;
  23.     printf("%s [y/N]  ",prompt);
  24.     c=toupper(getchar());
  25.     if (c=='Y') return 1;
  26.     return 0;
  27. }
  28.  
  29. void usage(void)                                                /* Usage */
  30. {
  31.     printf("Usage:\ttFont.exe [-?|-h|-d|-f] <Filename>\n");
  32.     printf("\t<Filename>          Installs the font <Filename>\n");
  33.     printf("\t-f                  Force yes on all questions\n");
  34.     printf("\t-d                  Display status only\n");
  35.     printf("\t-?                  This help\n");
  36.     printf("\t-h                  This help\n");
  37.     printf("\nThis program changes the current font in a fullscreen OS/2 VIO\n");
  38.     printf("See docs for more information.\n");
  39.     printf("%s\n",COPYRIGHT);
  40.     exit(1);
  41. }
  42.  
  43. void PrintError(APIRET err)                             /* Prints errors */
  44. {
  45.     switch (err) {
  46.     case 494:
  47.         printf("You can't run this program in an Windowed session.\n");
  48.         break;
  49.     case 355:
  50.         printf("Sorry, your drivers does not support this call.\nGet newer drivers and try again or give up.. :(.\nYou must exit this session, since it's \"broken\" now.\n");
  51.         break;
  52.     default:
  53.         printf("An error has occurred, errorcode: %ld\n",err);
  54.         break;
  55.     }
  56. }
  57.  
  58. int main(int argc,char*argv[])                                /* Main .. */
  59. {
  60.     VIOFONTINFO font;
  61.     UCHAR buf[65535];
  62.     UCHAR *fname;
  63.     FILE*file;
  64.     APIRET rc;
  65.     int c;
  66.  
  67.     printf("tFont v1.0 by Tomas Ögren <stric@freenet.hut.fi>\n\n");
  68.     fflush(stdout);
  69.     opterr = 0;
  70.     while ((c = getopt (argc, argv, "df")) != EOF)
  71.         switch (c)
  72.         {
  73.         case 'd':
  74.             opt_displayonly = 1;
  75.             break;
  76.         case 'f':
  77.             opt_force = 1;
  78.             break;
  79.         default:
  80.             usage ();
  81.         }
  82.     if (!opt_displayonly)
  83.         if (argc - optind != 1)
  84.             usage ();
  85.  
  86.     fname=argv[optind];
  87.     font.cb=sizeof(font);                 /* How large is this structure */
  88.     font.type=0;              /* Dunno, If you know what it is, mail me! */
  89.     font.cbData=65535;                        /* How large is my buffer? */
  90.     font.pbData=_emx_32to16(buf);  /* Wants an 16:16 pointer, converting */
  91.  
  92.     rc=VioGetFont (&font,0);           /* Retrieve data for current font */
  93.     if (rc)
  94.     {
  95.         PrintError(rc);
  96.         return 2;
  97.     }
  98.     if (!opt_displayonly)
  99.         if ((file=fopen(fname,"rb"))==NULL)
  100.         {
  101.             printf("Error opening file '%s'!\n",argv[optind]);
  102.             return 1;
  103.         }
  104.     printf("Fontsize: %lux%lu\nTotal bytes needed for font: %lu",font.cxCell, font.cyCell, font.cbData);
  105.     if (opt_displayonly)
  106.         exit(0);
  107.     if (filesize(file)!=font.cbData)
  108.     {
  109.         printf(", size of %s is %u",fname,filesize(file));
  110.         if (!opt_force)
  111.         {
  112.             if (RUSure("\nFilesize differs from size needed for the font..\nInstall anyway?")==0)
  113.             {
  114.                 printf("Aborting...\n");
  115.                 exit(1);
  116.             }
  117.         }
  118.     }
  119.     printf("\n");
  120.     fread(buf,1,font.cbData,file);
  121.     fclose(file);
  122.     rc=VioSetFont(&font,0);                         /* Put it all back.. */
  123.     if (rc)
  124.     {
  125.         PrintError(rc);
  126.         return 1;
  127.     }
  128.     printf("All Ok, font '%s' installed successfully.\n",fname);
  129. }
  130.