examples/PIPS/antiword/src/riscos.c

00001 /*
00002  * riscos.c
00003  * Copyright (C) 2001,2002 A.J. van Os; Released under GPL
00004  *
00005  * Description:
00006  * RISC OS only functions
00007  */
00008 
00009 #include <string.h>
00010 #include <stdlib.h>
00011 #include <stdarg.h>
00012 #include "DeskLib:Error.h"
00013 #include "DeskLib:SWI.h"
00014 #include "antiword.h"
00015 
00016 #if !defined(DrawFile_Render)
00017 #define DrawFile_Render         0x045540
00018 #endif /* !DrawFile_Render */
00019 #if !defined(JPEG_Info)
00020 #define JPEG_Info               0x049980
00021 #endif /* !JPEG_Info */
00022 
00023 /*
00024  * werr - write an error message and exit if needed
00025  */
00026 void
00027 werr(int iFatal, const char *szFormat, ...)
00028 {
00029         va_list tArg;
00030 
00031         va_start(tArg, szFormat);
00032         Error_Report(iFatal, (char *)szFormat, tArg);
00033         va_end(tArg);
00034         switch (iFatal) {
00035         case 0:         /* The message is just a warning, so no exit */
00036                 return;
00037         case 1:         /* Fatal error with a standard exit */
00038                 exit(EXIT_FAILURE);
00039         default:        /* Fatal error with a non-standard exit */
00040                 exit(iFatal);
00041         }
00042 } /* end of werr */
00043 
00044 /*
00045  * iGetFiletype
00046  * This function will get the filetype of the given file.
00047  * returns the filetype.
00048  */
00049 int
00050 iGetFiletype(const char *szFilename)
00051 {
00052         os_error        *e;
00053         int             iType;
00054 
00055         fail(szFilename == NULL || szFilename[0] == '\0');
00056 
00057         e = SWI(2, 7, SWI_OS_File | XOS_Bit,
00058                 23, szFilename,
00059                 NULL, NULL, NULL, NULL, NULL, NULL, &iType);
00060         if (e == NULL) {
00061                 return iType;
00062         }
00063         werr(0, "Get Filetype error %d: %s", e->errnum, e->errmess);
00064         return -1;
00065 } /* end of iGetFiletype */
00066 
00067 /*
00068  * vSetFiletype
00069  * This procedure will set the filetype of the given file to the given
00070  * type.
00071  */
00072 void
00073 vSetFiletype(const char *szFilename, int iFiletype)
00074 {
00075         os_error        *e;
00076 
00077         fail(szFilename == NULL || szFilename[0] == '\0');
00078 
00079         if (iFiletype < 0x000 || iFiletype > 0xfff) {
00080                 return;
00081         }
00082         e = SWI(3, 0, SWI_OS_File | XOS_Bit,
00083                 18, szFilename, iFiletype);
00084         if (e != NULL) {
00085                 switch (e->errnum) {
00086                 case 0x000113:  /* ROM */
00087                 case 0x0104e1:  /* Read-only floppy DOSFS */
00088                 case 0x0108c9:  /* Read-only floppy ADFS */
00089                 case 0x013803:  /* Read-only ArcFS */
00090                 case 0x80344a:  /* CD-ROM */
00091                         break;
00092                 default:
00093                         werr(0, "Set Filetype error %d: %s",
00094                                 e->errnum, e->errmess);
00095                         break;
00096                 }
00097         }
00098 } /* end of vSetFileType */
00099 
00100 /*
00101  * Check if the directory part of the given file exists, make the directory
00102  * if it does not exist yet.
00103  * Returns TRUE in case of success, otherwise FALSE.
00104  */
00105 BOOL
00106 bMakeDirectory(const char *szFilename)
00107 {
00108         os_error        *e;
00109         char    *pcLastDot;
00110         int     iObjectType;
00111         char    szDirectory[PATH_MAX+1];
00112 
00113         DBG_MSG("bMakeDirectory");
00114         fail(szFilename == NULL || szFilename[0] == '\0');
00115         DBG_MSG(szFilename);
00116 
00117         if (strlen(szFilename) >= sizeof(szDirectory)) {
00118                 DBG_DEC(strlen(szFilename));
00119                 return FALSE;
00120         }
00121         strcpy(szDirectory, szFilename);
00122         pcLastDot = strrchr(szDirectory, '.');
00123         if (pcLastDot == NULL) {
00124                 /* No directory equals current directory */
00125                 DBG_MSG("No directory part given");
00126                 return TRUE;
00127         }
00128         *pcLastDot = '\0';
00129         DBG_MSG(szDirectory);
00130         /* Check if the name exists */
00131         e = SWI(2, 1, SWI_OS_File | XOS_Bit,
00132                 17, szDirectory,
00133                 &iObjectType);
00134         if (e != NULL) {
00135                 werr(0, "Directory check %d: %s", e->errnum, e->errmess);
00136                 return FALSE;
00137         }
00138         if (iObjectType == 2) {
00139                 /* The name exists and it is a directory */
00140                 DBG_MSG("The directory already exists");
00141                 return TRUE;
00142         }
00143         if (iObjectType != 0) {
00144                 /* The name exists and it is not a directory */
00145                 DBG_DEC(iObjectType);
00146                 return FALSE;
00147         }
00148         /* The name does not exist, make the directory */
00149         e = SWI(5, 0, SWI_OS_File | XOS_Bit,
00150                 8, szDirectory, 0, 0, 0);
00151         if (e != NULL) {
00152                 werr(0, "I can't make a directory %d: %s",
00153                         e->errnum, e->errmess);
00154                 return FALSE;
00155         }
00156         return TRUE;
00157 } /* end of bMakeDirectory */
00158 
00159 /*
00160  * iReadCurrentAlphabetNumber
00161  * This function reads the current Alphabet number.
00162  * Returns the current Alphabet number when successful, otherwise -1
00163  */
00164 int
00165 iReadCurrentAlphabetNumber(void)
00166 {
00167         os_error        *e;
00168         int             iAlphabetNumber;
00169 
00170         e = SWI(2, 2, SWI_OS_Byte | XOS_Bit,
00171                 71, 127,
00172                 NULL, &iAlphabetNumber);
00173         if (e == NULL) {
00174                 return iAlphabetNumber;
00175         }
00176         werr(0, "Read alphabet error %d: %s", e->errnum, e->errmess);
00177         return -1;
00178 } /* end of iReadCurrentAlphabetNumber */
00179 
00180 /*
00181  * iGetRiscOsVersion - get the RISC OS version number
00182  *
00183  * returns the RISC OS version * 100
00184  */
00185 int
00186 iGetRiscOsVersion(void)
00187 {
00188         os_error        *e;
00189         int             iVersion;
00190 
00191         e = SWI(3, 2, SWI_OS_Byte | XOS_Bit,
00192                 129, 0, 0xff,
00193                 NULL, &iVersion);
00194         if (e != NULL) {
00195                 werr(0, "Read RISC OS version error %d: %s",
00196                         e->errnum, e->errmess);
00197                 return 0;
00198         }
00199         switch (iVersion) {
00200         case 0xa0:      /* Arthur 1.20 */
00201                 return 120;
00202         case 0xa1:      /* RISC OS 2.00 */
00203                 return 200;
00204         case 0xa2:      /* RISC OS 2.01 */
00205                 return 201;
00206         case 0xa3:      /* RISC OS 3.00 */
00207                 return 300;
00208         case 0xa4:      /* RISC OS 3.1x */
00209                 return 310;
00210         case 0xa5:      /* RISC OS 3.50 */
00211                 return 350;
00212         case 0xa6:      /* RISC OS 3.60 */
00213                 return 360;
00214         case 0xa7:      /* RISC OS 3.7x */
00215                 return 370;
00216         case 0xa8:      /* RISC OS 4.0x */
00217                 return 400;
00218         default:
00219                 if (iVersion >= 0xa9 && iVersion <= 0xaf) {
00220                         /* RISC OS 4.10 and up */
00221                         return 410;
00222                 }
00223                 /* Unknown version */
00224                 return 0;
00225         }
00226 } /* end of iGetRiscOsVersion */
00227 
00228 #if defined(DEBUG)
00229 BOOL
00230 bGetJpegInfo(UCHAR *pucJpeg, size_t tJpegSize)
00231 {
00232         os_error        *e;
00233         int     iReg0, iReg4, iReg5;
00234 
00235         e = SWI(3, 6, JPEG_Info | XOS_Bit,
00236                 0x00, pucJpeg, tJpegSize,
00237                 &iReg0, NULL, NULL, NULL, &iReg4, &iReg5);
00238         if (e == NULL) {
00239                 if (iReg0 & BIT(2)) {
00240                         DBG_MSG("Pixel density is a simple ratio");
00241                 } else {
00242                         DBG_MSG("Pixel density is in dpi");
00243                 }
00244                 DBG_DEC(iReg4);
00245                 DBG_DEC(iReg5);
00246                 return TRUE;
00247         }
00248         werr(0, "JPEG Info error %d: %s", e->errnum, e->errmess);
00249         return FALSE;
00250 } /* end of bGetJpegInfo */
00251 #endif /* DEBUG */

Generated by  doxygen 1.6.2