home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / CPROG / CGAZV5N3.ZIP / SVC.C < prev    next >
C/C++ Source or Header  |  1991-02-25  |  5KB  |  138 lines

  1. /************  Listing 2  ********************** SVC.C ****************
  2. *  svc.c --  Object Explorer, Service routines
  3. *  Author:   Thomas E. Siering, 1991. See Listing 1 for copyright.
  4. *  Usage:    These routines are general-purpose, not strictly related
  5. *            to the Object Explorer software and reusable.                 
  6. *  Compiler: Microsoft C 6.0, Turbo C++ 1.0
  7. *  Compile time switches: none
  8. *  Links with: ox.c and apps.c
  9. ***********************************************************************/
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <stdarg.h>
  13. #include <string.h>
  14. #include "ox.h"       /* see Listing 4 */
  15.  
  16. /*-------------------------------------------------------------------
  17. *  InstList  -- Instantiate a linked list.  Setting the InfoP pointer 
  18. *      to NULL will identify this node as the header.  The list data
  19. *      can be any type desired by client.
  20. *------------------------------------------------------------------*/
  21. PUBLIC void InstList(LNODE *ListHeader)
  22. {
  23.     ListHeader->InfoP = NULL;
  24.     ListHeader->NextP = NULL;
  25. }
  26.  
  27. /*----------------------------------------------------------------
  28. *  AddListNode  --  Add a new node to existing linked list.
  29. *      This linked list uses a little trick: the header's InfoP                
  30. *      pointer field, since not otherwise utilized will point to               
  31. *      the terminating list member thus avoiding a linear search.              
  32. *      NOTE: This function will allocate space for the list node               
  33. *      but NOT for the data put into list (since the data's type               
  34. *      is of no concern to list manager).  If the client pollutes              
  35. *      this memory (free()s it or clobbers it) disaster strikes!               
  36. *----------------------------------------------------------------*/
  37. PUBLIC LNODE AddListNode(LNODE ListHeader, void *NewData)
  38. {
  39.     LNODE *NewNode;
  40.  
  41.     if ((NewNode = (LNODE *) malloc(sizeof(LNODE))) == NULL) 
  42.     {
  43.         /* let the client worry about the consequences of this */
  44.         ListHeader.InfoP = NULL;
  45.         return (ListHeader);
  46.     }
  47.     NewNode->InfoP = NewData;
  48.     NewNode->NextP = NULL;
  49.  
  50.     if (ListHeader.InfoP != NULL)
  51.     /* link to last previous list element */
  52.         ((LNODE *) ListHeader.InfoP)->NextP = NewNode;
  53.     else
  54.         ListHeader.NextP = NewNode;
  55.     ListHeader.InfoP = NewNode;
  56.     return (ListHeader);
  57. }
  58.  
  59. /*-------------------------------------------------------------------
  60. *   GetListNode  -- Retrieve a node from the linked list
  61. *         NOTE: List node 0 is header and shouldn't need retrieval
  62. *-------------------------------------------------------------------*/
  63. PUBLIC LNODE *GetListNode(LNODE ListHeader, unsigned int NodeIdx)
  64. {
  65.     LNODE *NodeP = ListHeader.NextP;
  66.     unsigned int   i;
  67.  
  68.     if (NodeIdx < 1)                /* nonsensical request */
  69.         return (NULL);
  70.  
  71.     for (i = 1; i < NodeIdx; i++) 
  72.     {
  73.         NodeP = NodeP->NextP;
  74.         if (NodeP == NULL)         /* erroneous index > last element */
  75.             break;
  76.     }
  77.     return (NodeP);
  78. }
  79.  
  80. /*----------------------------------------------------------------------
  81. *   MakeASCIIZ  --  Convert string in Intel format (1 byte string length    
  82. *        followed by actual string with no terminating 0) to ASCIIZ.
  83. *----------------------------------------------------------------------*/
  84. PUBLIC void *MakeASCIIZ(char *data)
  85. {
  86.     char *ASCIIZString;
  87.     unsigned char StringLength;
  88.  
  89.     StringLength = *data++;
  90.  
  91.     if ((ASCIIZString = malloc((int) StringLength + 1)) == NULL)
  92.         return (NULL);
  93.  
  94.     ASCIIZString[StringLength] = '\0';
  95.     strncpy(ASCIIZString, data, StringLength);
  96.     return (ASCIIZString);
  97. }
  98.  
  99. /*-----------------------------------------------------------------
  100. *   Output  --  Write to the output stream. This function adds an 
  101. *     exception-handling layer to disk I/O. In addition, it handles
  102. *     abnormal program termination, and warnings to both stderr and         
  103. *     output. Three types of message can be handled:
  104. *        Message - simply printed to a file                                    
  105. *        Warning - print to file AND stderr                                    
  106. *        Error - same as Warning, but terminate with abnormal exit code        
  107. *-----------------------------------------------------------------*/
  108. PUBLIC void Output(MESSAGETYPE MsgType, FILE *Stream, 
  109.                     char *OutputFormat, ...)
  110. {
  111.     char OutputBuffer[133];
  112.     va_list VarArgP;
  113.  
  114.     va_start(VarArgP, OutputFormat);
  115.     vsprintf(OutputBuffer, OutputFormat, VarArgP);
  116.  
  117.     /* If this is (non-fatal) warning or (fatal) error, 
  118.        then also send it to stderr */
  119.     if (MsgType != Message)
  120.         fprintf(stderr, "\a%s", OutputBuffer);
  121.  
  122.     /* In any case: attempt to print message to output file.  
  123.        Exception check. */
  124.     if ((size_t) fprintf(Stream, OutputBuffer) != strlen(OutputBuffer)) 
  125.     {
  126.         fprintf(stderr, "\aDisk Write Failure!\n");
  127.         exit(0xFF);
  128.     }
  129.  
  130.     /* If this was (fatal) error message, abort on the spot */
  131.     if (MsgType == Error)
  132.         exit(0xFF);
  133.  
  134.     va_end(VarArgP);
  135. }