Changeset 521 for trunk/src/ddraw/misc.c


Ignore:
Timestamp:
Aug 16, 1999, 10:52:22 PM (26 years ago)
Author:
hugh
Message:

updated misc with FS wrappers

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/ddraw/misc.c

    r97 r521  
    1 /* $Id: misc.c,v 1.3 1999-06-10 17:10:57 phaller Exp $ */
    2 
    3 /*
    4  * Logging procedures
    5  *
    6  * Copyright 1998 Sander van Leeuwen (sandervl@xs4all.nl)
    7  * Copyright 1998 Joel Troster
    8  * Copyright 1998 Peter FitzSimmons
    9  *
    10  *
    11  * Project Odin Software License can be found in LICENSE.TXT
    12  *
    13  */
    141#define INCL_BASE
    152#define INCL_WIN
    163#define INCL_WINERRORS
    174#define INCL_DOSFILEMGR
    18 #include <os2.h>
     5#include <os2wrap.h>
    196#include <stdio.h>
    207#include <stdlib.h>
     
    229#include <stdarg.h>
    2310#include "misc.h"
     11#include <odincrt.h>
    2412
    2513
     
    3826#include <time.h>                  /* .. */
    3927
    40 extern ULONG flAllocMem;    /*Tue 03.03.1998: knut */
     28extern ULONG flAllocMem;  /*Tue 03.03.1998: knut */
    4129
    4230/* ----- Local defines ----- */
     
    4533
    4634/* ----- Per-thread output buffer and current indices into line ---- */
    47 struct perthread {
     35struct perthread
     36{
    4837  LONG   lineindex;                /* where next char */
    4938  LONG   tidemark;                 /* rightmost char */
    5039  int    bell;                     /* TRUE if line has bell */
    5140  UCHAR  line[PRINTFMAXBUF];       /* accumulator */
    52   };
     41};
    5342
    5443/* ----- Local static variables ----- */
     
    6655/* ----------------------------------------------------------------- */
    6756int SYSTEM EXPORT WriteLog(char *f, ...)
    68   {
     57{
    6958  TIB    *ptib;                    /* process/thread id structures */
    7059  PIB    *ppib;                    /* .. */
     
    7463  ULONG  urc;                      /* returncode */
    7564
    76   urc=DosOpenQueue(&servepid, &qhandle, PRINTFQNAME);  /* Open the Q */
     65  urc=DosOpenQueue( &servepid,
     66                    &qhandle,
     67                    PRINTFQNAME);  /* Open the Q */
    7768  /* Non-0 RC means Q does not exist or cannot be opened */
    78   if (urc==343) return 0;          /* queue does not exist, so quit */
    79   if (urc!=0)   return -1;         /* report any other error */
     69  if (urc==343)
     70    return 0;          /* queue does not exist, so quit */
     71  if (urc!=0)
     72    return -1;         /* report any other error */
    8073
    8174  /* First determine our thread ID (and hence get access to the      */
     
    8881    return 0;                      /* .. so quit, quietly */
    8982  tp=tps[ourtid];                  /* copy to local pointer */
    90   if (tp==NULL) {                  /* uninitialized (NULL=0) */
     83  if (tp==NULL)
     84  {
     85    /* uninitialized (NULL=0) */
    9186    /* allocate a per-thread structure */
    9287    tp=(struct perthread *)malloc(sizeof(struct perthread));
    93     if (tp==NULL) return -1;       /* out of memory -- return error */
     88    if (tp==NULL)
     89      return -1;       /* out of memory -- return error */
    9490    tps[ourtid]=tp;                /* save for future calls */
    9591    strcpy(tp->line,PRINTFID);     /* initialize: line.. */
     
    9793    tp->tidemark =PRINTFIDSIZE-2;  /* ..rightmost char */
    9894    tp->bell=FALSE;                /* ..if line has bell */
    99     if (ourpid==0) ourpid=ppib->pib_ulpid;   /* save PID for all to use */
    100     }
     95    if (ourpid==0)
     96      ourpid=ppib->pib_ulpid;   /* save PID for all to use */
     97  }
    10198
    10299  { /* Block for declarations -- only needed if queue exists, etc. */
     
    111108    va_end(argptr);                /* done with variable arguments */
    112109
    113     if (count<0) return count-1000;/* bad start */
    114 
    115     if (count>PRINTFMAXLEN) {
     110    if (count<0)
     111      return count-1000;/* bad start */
     112
     113    if (count>PRINTFMAXLEN)
     114    {
    116115      /* Disaster -- we are probably "dead", but just in case we */
    117116      /* are not, carry on with truncated data. */
    118117      count=PRINTFMAXLEN;
    119       }
     118    }
    120119    buffer[count]='\0';            /* ensure terminated */
    121120    /* OK, ready to go with the data now in BUFFER                    */
     
    124123    /* the queue whenever we see a LF control, or when the line       */
    125124    /* fills (causing a forced break).                                */
    126     for (i=0; ; i++) {
     125    for (i=0; ; i++)
     126    {
    127127      ch=buffer[i]; if (!ch) break;
    128       switch(ch) {
     128      switch(ch)
     129      {
    129130        case '\r':                 /* carriage return */
    130131          tp->lineindex=PRINTFIDSIZE-1; /* back to start of line */
     
    138139          newind=tp->lineindex-PRINTFIDSIZE+1;   /* offset into data */
    139140          newind=tp->lineindex+5-newind%5;    /* new index requested */
    140           if (newind>=PRINTFMAXBUF) newind=PRINTFMAXBUF;    /* clamp */
    141           for (; tp->lineindex<newind; tp->lineindex++) {
    142             if (tp->lineindex>tp->tidemark) {  /* beyond current end */
     141          if (newind>=PRINTFMAXBUF)
     142            newind=PRINTFMAXBUF;    /* clamp */
     143          for (; tp->lineindex<newind; tp->lineindex++)
     144          {
     145            if (tp->lineindex>tp->tidemark)
     146            {  /* beyond current end */
    143147              tp->line[tp->lineindex]=' ';              /* add space */
    144148              tp->tidemark=tp->lineindex;
    145               }
    146149            }
     150          }
    147151          break;
    148152        case '\v':                 /* vertical tab */
     
    161165          tp->lineindex++;                 /* step for next */
    162166        } /* switch */
    163       if (tp->lineindex>=PRINTFMAXBUF) {
     167      if (tp->lineindex>=PRINTFMAXBUF)
     168      {
    164169        rc=printf_(tp);            /* print a line */
    165         if (rc!=0) return rc;      /* error */
    166         }
    167 
    168       } /* copy loop */
     170        if (rc!=0)
     171          return rc;      /* error */
     172      }
     173
     174    } /* copy loop */
    169175    return count;                  /* all formatted data processed */
    170     } /* block */
    171   } /* printf */
     176  } /* block */
     177} /* printf */
    172178
    173179/* ----- printf_(tp) -- Local subroutine to send a line ------------ */
    174180/* A line has been completed (or overflowed): write it to the queue. */
    175181int printf_(struct perthread *tp)  /* pointer to per-thread data */
    176   {
     182{
    177183  ULONG   urc;                     /* unsigned returncode */
    178184  PSZ     pszTo, pszFrom;          /* character pointers */
     
    185191
    186192  /* Get some shared memory that can be given away */
    187   urc=DosAllocSharedMem(&addr, NULL, (unsigned)size,
    188     OBJ_GIVEABLE|PAG_WRITE|PAG_COMMIT|flAllocMem);
     193  urc=DosAllocSharedMem( &addr,
     194                         NULL,
     195                         (unsigned)size,
     196                         OBJ_GIVEABLE|PAG_WRITE|PAG_COMMIT|flAllocMem);
    189197                                   /*knut: added flAllocMem */
    190198
    191   if (urc!=0) return -2;           /* error */
     199  if (urc!=0)
     200    return -2;                     /* error */
    192201
    193202  pszTo=addr;                      /* copy for clarity */
     
    195204  strcpy(pszTo,pszFrom);           /* copy the string to shared memory */
    196205
    197   if (ourpid!=servepid) {          /* (no giveaway needed if to self) */
     206  if (ourpid!=servepid)
     207  {          /* (no giveaway needed if to self) */
    198208    urc=DosGiveSharedMem(addr, servepid, PAG_READ); /* give access */
    199     if (urc!=0) return -3;}        /* error */
     209    if (urc!=0)
     210      return -3; /* error */
     211  }
    200212
    201213  /* Write the selector, size, and timestamp to the queue */
    202214  if (tp->bell) size=-size;        /* BELL passed by negation */
    203215  time(&timenow);                  /* optional - else use 0 */
    204   urc=DosWriteQueue(qhandle,       /* handle */
    205          (unsigned)timenow,        /* 'request' (timestamp) */
    206          (unsigned)size,           /* 'length'  (length/bell) */
    207                    addr,           /* 'address' (address) */
    208                    0);             /* priority (FIFO if enabled) */
    209   if (urc!=0) return -4;           /* error */
    210   if (ourpid!=servepid) {          /* if given away.. */
     216  urc=DosWriteQueue( qhandle,           /* handle */
     217                     (unsigned)timenow, /* 'request' (timestamp) */
     218                     (unsigned)size,    /* 'length'  (length/bell) */
     219                               addr,    /* 'address' (address) */
     220                               0);       /* priority (FIFO if enabled) */
     221  if (urc!=0)
     222    return -4;           /* error */
     223  if (ourpid!=servepid)
     224  {          /* if given away.. */
    211225    urc=DosFreeMem(addr);          /* .. *we* are done with it */
    212     if (urc!=0) return -5;}        /* error */
     226    if (urc!=0)
     227      return -5; /* error */
     228  }
    213229  /* Reset the line buffer and indices */
    214230  tp->lineindex=PRINTFIDSIZE-1;    /* where next char */
     
    216232  tp->bell     =FALSE;             /* true if line has bell */
    217233  return 0;                        /* success! */
    218   } /* printf_ */
     234} /* printf_ */
    219235#else
    220236
     
    226242int SYSTEM EXPORT WriteLog(char *tekst, ...)
    227243{
    228     va_list argptr;
    229     if(!init){
    230         init = TRUE;
    231         if(!getenv("NOWIN32LOG"))
    232             flog = fopen("directx.log", "w");
    233     }
    234 
    235     if(flog){
    236         va_start(argptr, tekst);
    237         vfprintf(flog, tekst, argptr);
    238         va_end(argptr);
    239     if(tekst[strlen(tekst)-1] != '\n')
    240         fprintf(flog, "\n");
    241     }
    242     return 1;
     244  ODIN_FS_BEGIN
     245
     246  va_list argptr;
     247  if(!init)
     248  {
     249    init = TRUE;
     250    if(!getenv("NODDRAWLOG"))
     251      flog = fopen("DDRAW.log", "w");
     252  }
     253
     254  if(flog)
     255  {
     256     va_start(argptr, tekst);
     257     vfprintf(flog, tekst, argptr);
     258     va_end(argptr);
     259     fflush(flog);
     260  }
     261  ODIN_FS_END
     262
     263  return 1;
    243264}
    244265#else   /*PLF Mon  97-09-08 20:04:26*/
     
    249270int SYSTEM EXPORT WriteLog(char *tekst, ...)
    250271{
    251 ULONG Action, Wrote;
    252 HFILE log;
    253 APIRET rc;
    254 char message[4096];
    255 va_list argptr;
    256 ULONG openFlags = OPEN_ACTION_CREATE_IF_NEW;
    257 
    258  if(fLog == FALSE)
     272  ULONG Action, Wrote;
     273  HFILE log;
     274  APIRET rc;
     275  char message[4096];
     276  va_list argptr;
     277  ULONG openFlags = OPEN_ACTION_CREATE_IF_NEW;
     278
     279  if(fLog == FALSE)
    259280    return(FALSE);
    260281
    261  if(!init) {
    262     init       = TRUE;
     282  if(!init)
     283  {
     284    init = TRUE;
    263285    openFlags |= OPEN_ACTION_REPLACE_IF_EXISTS;
    264         if(getenv("NOWIN32LOG"))
    265             fLog = FALSE;
    266  }
    267  else   openFlags |= OPEN_ACTION_OPEN_IF_EXISTS;
    268 
    269  rc = DosOpen(
    270               "directx.log",
    271               &log,           /* file handle returned */
    272               &Action,
    273               0L,
    274               FILE_NORMAL,
    275               openFlags,
    276               OPEN_ACCESS_READWRITE | OPEN_SHARE_DENYWRITE,
    277               (PEAOP2)NULL);
    278 
    279  rc = DosSetFilePtr(log, 0, FILE_END, &Wrote);
    280  va_start(argptr, tekst);
    281  vsprintf(message, tekst, argptr);
    282  va_end(argptr);
    283 
    284  rc = DosWrite(log, message, strlen(message), &Wrote);
    285 
    286  DosClose(log);   /*PLF Mon  97-09-08 20:01:43*/
    287  return(TRUE);
     286    if(getenv("NOWIN32LOG"))
     287      fLog = FALSE;
     288  }
     289  else
     290    openFlags |= OPEN_ACTION_OPEN_IF_EXISTS;
     291
     292  rc = DosOpen( "win32os2.log",
     293                &log,           /* file handle returned */
     294                &Action,
     295                0L,
     296                FILE_NORMAL,
     297                openFlags,
     298                OPEN_ACCESS_READWRITE | OPEN_SHARE_DENYWRITE,
     299                (PEAOP2)NULL);
     300
     301   rc = DosSetFilePtr(log, 0, FILE_END, &Wrote);
     302   va_start(argptr, tekst);
     303   vsprintf(message, tekst, argptr);
     304   va_end(argptr);
     305
     306   rc = DosWrite(log, message, strlen(message), &Wrote);
     307
     308   DosClose(log);   /*PLF Mon  97-09-08 20:01:43*/
     309   return(TRUE);
    288310}
    289311#endif  /*PLF Mon  97-09-08 20:04:23*/
Note: See TracChangeset for help on using the changeset viewer.