home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / anwor032.zip / antiword.0.32 / riscos.c < prev    next >
C/C++ Source or Header  |  2001-04-22  |  6KB  |  259 lines

  1. /*
  2.  * riscos.c
  3.  * Copyright (C) 2001 A.J. van Os; Released under GPL
  4.  *
  5.  * Description:
  6.  * RISC OS only functions
  7.  */
  8.  
  9. #include <string.h>
  10. #include "kernel.h"
  11. #include "swis.h"
  12. #include "antiword.h"
  13.  
  14. #if !defined(DrawFile_Render)
  15. #define DrawFile_Render        0x045540
  16. #endif /* !DrawFile_Render */
  17.  
  18.  
  19. /*
  20.  * iGetFiletype
  21.  * This function will get the filetype of the given file.
  22.  * returns the filetype.
  23.  */
  24. int
  25. iGetFiletype(const char *szFilename)
  26. {
  27.     _kernel_swi_regs    regs;
  28.     _kernel_oserror        *e;
  29.  
  30.     fail(szFilename == NULL || szFilename[0] == '\0');
  31.  
  32.     (void)memset((void *)®s, 0, sizeof(regs));
  33.     regs.r[0] = 23;
  34.     regs.r[1] = (int)szFilename;
  35.     e = _kernel_swi(OS_File, ®s, ®s);
  36.     if (e == NULL) {
  37.         return regs.r[6];
  38.     }
  39.     werr(0, "Get Filetype error %d: %s", e->errnum, e->errmess);
  40.     return -1;
  41. } /* end of iGetFiletype */
  42.  
  43. /*
  44.  * vSetFiletype
  45.  * This procedure will set the filetype of the given file to the given
  46.  * type.
  47.  */
  48. void
  49. vSetFiletype(const char *szFilename, int iFiletype)
  50. {
  51.     _kernel_swi_regs    regs;
  52.     _kernel_oserror        *e;
  53.  
  54.     fail(szFilename == NULL || szFilename[0] == '\0');
  55.  
  56.     if (iFiletype < 0x000 || iFiletype > 0xfff) {
  57.         return;
  58.     }
  59.     (void)memset((void *)®s, 0, sizeof(regs));
  60.     regs.r[0] = 18;
  61.     regs.r[1] = (int)szFilename;
  62.     regs.r[2] = iFiletype;
  63.     e = _kernel_swi(OS_File, ®s, ®s);
  64.     if (e != NULL) {
  65.         switch (e->errnum) {
  66.         case 0x000113:    /* ROM */
  67.         case 0x0104e1:    /* Read-only floppy DOSFS */
  68.         case 0x0108c9:    /* Read-only floppy ADFS */
  69.         case 0x013803:    /* Read-only ArcFS */
  70.         case 0x80344a:    /* CD-ROM */
  71.             break;
  72.         default:
  73.             werr(0, "Set Filetype error %d: %s",
  74.                 e->errnum, e->errmess);
  75.             break;
  76.         }
  77.     }
  78. } /* end of vSetFileType */
  79.  
  80. /*
  81.  * Check if the directory part of the given file exists, make the directory
  82.  * if it does not exist yet.
  83.  * Returns TRUE in case of success, otherwise FALSE.
  84.  */
  85. BOOL
  86. bMakeDirectory(const char *szFilename)
  87. {
  88.     _kernel_swi_regs    regs;
  89.     _kernel_oserror        *e;
  90.     char    *pcLastDot;
  91.     char    szDirectory[PATH_MAX+1];
  92.  
  93.     DBG_MSG("bMakeDirectory");
  94.     fail(szFilename == NULL || szFilename[0] == '\0');
  95.     DBG_MSG(szFilename);
  96.  
  97.     if (strlen(szFilename) >= sizeof(szDirectory)) {
  98.         DBG_DEC(strlen(szFilename));
  99.         return FALSE;
  100.     }
  101.     strcpy(szDirectory, szFilename);
  102.     pcLastDot = strrchr(szDirectory, '.');
  103.     if (pcLastDot == NULL) {
  104.         /* No directory equals current directory */
  105.         DBG_MSG("No directory part given");
  106.         return TRUE;
  107.     }
  108.     *pcLastDot = '\0';
  109.     DBG_MSG(szDirectory);
  110.     /* Check if the name exists */
  111.     (void)memset((void *)®s, 0, sizeof(regs));
  112.     regs.r[0] = 17;
  113.     regs.r[1] = (int)szDirectory;
  114.     e = _kernel_swi(OS_File, ®s, ®s);
  115.     if (e != NULL) {
  116.         werr(0, "Directory check %d: %s", e->errnum, e->errmess);
  117.         return FALSE;
  118.     }
  119.     if (regs.r[0] == 2) {
  120.         /* The name exists and it is a directory */
  121.         DBG_MSG("The directory already exists");
  122.         return TRUE;
  123.     }
  124.     if (regs.r[0] != 0) {
  125.         /* The name exists and it is not a directory */
  126.         DBG_DEC(regs.r[0]);
  127.         return FALSE;
  128.     }
  129.     /* The name does not exist, make the directory */
  130.     (void)memset((void *)®s, 0, sizeof(regs));
  131.     regs.r[0] = 8;
  132.     regs.r[1] = (int)szDirectory;
  133.     regs.r[4] = 0;
  134.     e = _kernel_swi(OS_File, ®s, ®s);
  135.     if (e != NULL) {
  136.         werr(0, "I can't make a directory %d: %s",
  137.             e->errnum, e->errmess);
  138.         return FALSE;
  139.     }
  140.     return TRUE;
  141. } /* end of bMakeDirectory */
  142.  
  143. /*
  144.  * bISO_8859_1_IsCurrent
  145.  * This function checks whether ISO_8859_1 (aka Latin1) is the current
  146.  * character set.
  147.  */
  148. BOOL
  149. bISO_8859_1_IsCurrent(void)
  150. {
  151.     _kernel_swi_regs    regs;
  152.     _kernel_oserror        *e;
  153.  
  154.     (void)memset((void *)®s, 0, sizeof(regs));
  155.     regs.r[0] = 71;
  156.     regs.r[1] = 127;
  157.     e = _kernel_swi(OS_Byte, ®s, ®s);
  158.     if (e == NULL) {
  159.         return regs.r[1] == 101;
  160.     }
  161.     werr(0, "Read alphabet error %d: %s", e->errnum, e->errmess);
  162.     return FALSE;
  163. } /* end of bISO_8859_1_IsCurrent */
  164.  
  165. /*
  166.  * iGetRiscOsVersion - get the RISC OS version number
  167.  *
  168.  * returns the RISC OS version * 100
  169.  */
  170. int
  171. iGetRiscOsVersion(void)
  172. {
  173.     _kernel_swi_regs    regs;
  174.     _kernel_oserror        *e;
  175.  
  176.     (void)memset((void *)®s, 0, sizeof(regs));
  177.     regs.r[0] = 129;
  178.     regs.r[1] = 0;
  179.     regs.r[2] = 0xff;
  180.     e = _kernel_swi(OS_Byte, ®s, ®s);
  181.     if (e != NULL) {
  182.         werr(0, "Read RISC OS version error %d: %s",
  183.             e->errnum, e->errmess);
  184.         return 0;
  185.     }
  186.     switch (regs.r[1]) {
  187.     case 0xa0:    /* Arthur 1.20 */
  188.         return 120;
  189.     case 0xa1:    /* RISC OS 2.00 */
  190.         return 200;
  191.     case 0xa2:    /* RISC OS 2.01 */
  192.         return 201;
  193.     case 0xa3:    /* RISC OS 3.00 */
  194.         return 300;
  195.     case 0xa4:    /* RISC OS 3.1x */
  196.         return 310;
  197.     case 0xa5:    /* RISC OS 3.50 */
  198.         return 350;
  199.     case 0xa6:    /* RISC OS 3.60 */
  200.         return 360;
  201.     case 0xa7:    /* RISC OS 3.7x */
  202.         return 370;
  203.     default:
  204.         if (regs.r[1] >= 0xa8 && regs.r[1] <= 0xaf) {
  205.             /* RISC OS 4.00 and up */
  206.             return 400;
  207.         }
  208.         /* Unknow version */
  209.         return 0;
  210.     }
  211. } /* end of iGetRiscOsVersion */
  212.  
  213. /*
  214.  * Replaces the draw_render_diag function from RISC_OSLib when using
  215.  * RISC OS version 3.60 or higher
  216.  * This function calls a SWI that does not exist in earlier versions
  217.  */
  218. BOOL
  219. bDrawRenderDiag360(draw_diag *pInfo,
  220.     draw_redrawstr *pRedraw, double dScale, draw_error *pError)
  221. {
  222.     _kernel_swi_regs    regs;
  223.     _kernel_oserror        *e;
  224.     int    aiTransform[6];
  225.  
  226.     fail(pInfo == NULL);
  227.     fail(pRedraw == NULL);
  228.     fail(dScale < 0.01);
  229.     fail(pError == NULL);
  230.     fail(iGetRiscOsVersion() < 360);
  231.  
  232.     aiTransform[0] = (int)(dScale * 0x10000);
  233.     aiTransform[1] = 0;
  234.     aiTransform[2] = 0;
  235.     aiTransform[3] = (int)(dScale * 0x10000);
  236.     aiTransform[4] = (pRedraw->box.x0 - pRedraw->scx) * 256;
  237.     aiTransform[5] = (pRedraw->box.y1 - pRedraw->scy) * 256;
  238.  
  239.     (void)memset((void *)®s, 0, sizeof(regs));
  240.     regs.r[0] = 0;
  241.     regs.r[1] = (int)pInfo->data;
  242.     regs.r[2] = pInfo->length;
  243.     regs.r[3] = (int)aiTransform;
  244.     regs.r[4] = (int)&pRedraw->box;
  245.     regs.r[5] = 0;
  246.     e = _kernel_swi(DrawFile_Render, ®s, ®s);
  247.     if (e == NULL) {
  248.         return TRUE;
  249.     }
  250.     werr(0, "DrawFile render error %d: %s", e->errnum, e->errmess);
  251.     pError->type = DrawOSError;
  252.     pError->err.os.errnum = e->errnum;
  253.     strncpy(pError->err.os.errmess,
  254.         e->errmess,
  255.         sizeof(pError->err.os.errmess) - 1);
  256.     pError->err.os.errmess[sizeof(pError->err.os.errmess) - 1] = '\0';
  257.     return FALSE;
  258. } /* end of bDrawRenderDiag360 */
  259.