home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / diverses / text_cla / fio.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-05-12  |  6.5 KB  |  346 lines

  1. /******************************************************************************
  2. *+
  3. ** Module Name:   MOS.C
  4. ** 
  5. ** Description:   Multi-tasking
  6. **                Operating
  7. **                System
  8. **                Simulation
  9. **
  10. **                Moss is a simulation of a multi-tasking operating system
  11. **                which uses many of the C standard algorithms.
  12. **
  13. ** Written by:  John Tal
  14. **
  15. **
  16. **
  17. ** Modification History:
  18. **
  19. ** Date          Programmer      Mod#     Modification
  20. ** ---------------------------------------------------------------------------
  21. ** 04-JUL-1991   J. Tal          V1.0-000 New
  22. ** 01-DEC-1991   J. Tal          V2.0-000 New Version, enhanced semaphores,
  23. **                                        messaging, swapping control
  24. **
  25. *-
  26. */
  27.  
  28. #ifdef C_ANSI
  29. #include <string.h>
  30. #include <stdlib.h>
  31. #endif
  32.  
  33. #include <stdio.h>
  34.  
  35. #include <fio.h>
  36.  
  37.  
  38.  
  39. /*
  40. **  FioGetFile
  41. **
  42. **  Is called by a reader to check for existance of a file.  
  43. **  SeedPathName = directory + filename + extension.
  44. **  The extension increments starting from .000 
  45. */
  46.  
  47. #ifdef C_ANSI
  48. SHORT APIENTRY
  49. FioGetFile(PCHAR  pcSeedPathName)
  50. #else
  51. SHORT APIENTRY 
  52. FioGetFile(pcSeedPathName)
  53. PCHAR  pcSeedPathName;
  54. #endif
  55. {
  56.    C_DEF_MODULE("FioGetFile")
  57.  
  58.    struct stat stStatMain;
  59.  
  60.  
  61.    /* check if file exists */
  62.  
  63.    C_STATUS = stat(pcSeedPathName,&stStatMain);    
  64.    if(C_STATUS)
  65.       C_SET_STATUS(FIO_NO_FILE)
  66.    else
  67.       C_SET_STATUS(FIO_OK);
  68.  
  69.    C_RETURN
  70. }
  71.  
  72.  
  73.  
  74. /*
  75. **  FioRemoveFile
  76. **
  77. **  Delete a file.  To be called by a READER only
  78. */
  79.  
  80. #ifdef C_ANSI
  81. SHORT APIENTRY
  82. FioRemoveFile(PCHAR  pcSeedPathName)
  83. #else
  84. SHORT APIENTRY
  85. FioRemoveFile(pcSeedPathName)
  86. PCHAR  pcSeedPathName;
  87. #endif
  88. {
  89.    C_DEF_MODULE("FioRemoveFile")
  90.  
  91.    C_STATUS = unlink(pcSeedPathName);
  92.  
  93.    C_RETURN
  94. }
  95.  
  96.  
  97. /*
  98. **  FioIncSeedPathName
  99. **
  100. **  Increment the extension of the rolling seed name.
  101. **  Wraps to 000 if 999
  102. **  Called by writer to next filename of next file to write.
  103. **  Called by reader to get next filename to read.
  104. **  NOTE:  Beware of situations where files a reader is looking
  105. **         for do not have their response files created in the
  106. **         exact same order (asynchronus process on one side
  107. **         of the connection).  The reader may have to maintain
  108. **         a list of all filenames it is waiting for and be
  109. **         able to accept them out of order.  This is only necessary
  110. **         if there is asynchronus processing on one side AND the
  111. **         queue/pipeline of communication files is greater than one
  112. **         at a time.
  113. */
  114.  
  115. #ifdef C_ANSI
  116. SHORT APIENTRY
  117. FioIncSeedPathName(PCHAR  pcSeedPathName)
  118. #else
  119. SHORT APIENTRY
  120. FioIncSeedPathName(pcSeedPathName)
  121. PCHAR  pcSeedPathName;
  122. #endif
  123. {
  124.    C_DEF_MODULE("FioIncSeedPathName")
  125.  
  126.    SHORT iPos;
  127.    CHAR  szExtension[FIO_EXTENSION_LEN + 2];
  128.    SHORT iExtension;
  129.    CHAR  szWorkStr[128];
  130.  
  131.    iPos = FioFindChar(pcSeedPathName,FIO_PERIOD);
  132.  
  133.    if(iPos == FIO_NO_CHAR)
  134.            return(iPos);
  135.  
  136.    strcpy(szExtension,&pcSeedPathName[iPos + 1]);
  137.    iExtension = atoi(szExtension);
  138.    iExtension++;
  139.    if(iExtension > FIO_MAX_EXTENSION)
  140.            iExtension = 0;
  141.  
  142.    sprintf(szExtension,"%d",iExtension);
  143.    while(strlen(szExtension) < FIO_EXTENSION_LEN)
  144.    {
  145.       strcpy(szWorkStr,"0");
  146.       strcat(szWorkStr,szExtension);
  147.       strcpy(szExtension,szWorkStr);
  148.    }
  149.  
  150.    strcpy(&pcSeedPathName[iPos + 1],szExtension);
  151.  
  152.    C_RETURN
  153. }
  154.  
  155.  
  156.  
  157. /*
  158. **  FioFindChar
  159. **
  160. **  Look for a character in a string.
  161. **  Returns a 0-based offset
  162. **
  163. */
  164.  
  165. #ifdef C_ANSI
  166. SHORT APIENTRY
  167. FioFindChar(PCHAR  pcString, CHAR cChar)
  168. #else
  169. SHORT APIENTRY
  170. FioFindChar(pcString,cChar)
  171. PCHAR  pcString;
  172. CHAR cChar;
  173. #endif
  174. {
  175.    C_DEF_MODULE("FioFindChar")
  176.  
  177.    SHORT iPos = -1;
  178.    SHORT i;
  179.    SHORT iLen;
  180.  
  181.    iLen = strlen(pcString);
  182.    for(i = 0; i < iLen; i++)
  183.      if(pcString[i] == cChar)
  184.         break;
  185.  
  186.    if(i < iLen)
  187.      C_SET_STATUS(i)
  188.    else
  189.      C_SET_STATUS(FIO_NO_CHAR)
  190.  
  191.    C_RETURN
  192.  
  193.  
  194. /*
  195. **  FioReplaceExt
  196. **
  197. **  Replace the default .xxx extension
  198. **  Usefull if restarting one side of the communcation session
  199. */
  200.  
  201. #ifdef C_ANSI
  202. SHORT APIENTRY
  203. FioReplaceExt(PCHAR  pcSeedPathName, PCHAR  pcNewExt)
  204. #else
  205. SHORT APIENTRY
  206. FioReplaceExt(pcSeedPathName,pcNewExt)
  207. PCHAR  pcSeedPathName;
  208. PCHAR  pcNewExt;
  209. #endif
  210. {
  211.    C_DEF_MODULE("FioReplaceExt")
  212.  
  213.    SHORT iPos;
  214.  
  215.    iPos = FioFindChar(pcSeedPathName,'.');
  216.  
  217.    if(iPos != FIO_NO_CHAR)
  218.    {
  219.        strcpy(&pcSeedPathName[iPos +1],pcNewExt);
  220.    } 
  221.  
  222.    C_RETURN
  223. }
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230. #ifdef TEST
  231.  
  232. /* *******************************************************************
  233.                       T E S T  C O D E
  234.    ******************************************************************* */
  235.  
  236. #include <memlib.h>
  237.  
  238. #include <mos.h>
  239.  
  240.  
  241. CHAR  szReaderSeedPathName[] = "Testing.000";
  242. CHAR  szWriterSeedPathName[] = "Testing.000";
  243.  
  244.  
  245.  
  246. /* Reader Proc */
  247.  
  248. #ifdef C_ANSI
  249. SHORT 
  250. FioProc1(PROC_P pstProc)
  251. #else
  252. SHORT
  253. FioProc1(pstProc)
  254. PROC_P pstProc;
  255. #endif
  256. {
  257.     C_DEF_MODULE("FioProc1")
  258.  
  259.     C_STATUS = FioGetFile(szReaderSeedPathName);
  260.  
  261.     if(!C_STATUS)
  262.     {
  263.        printf("Reader: Working on file %s\n",szReaderSeedPathName);
  264.        FioRemoveFile(szReaderSeedPathName);
  265.        FioIncSeedPathName(szReaderSeedPathName);
  266.     }
  267.     else
  268.     {
  269.        pstProc -> lWakeTime = MosCurTime()+0;
  270.   
  271.        C_STATUS = MosSleep(pstProc);
  272.     }
  273.  
  274.     C_RETURN
  275.  
  276. }
  277.  
  278.  
  279.  
  280. #define TEMP_FILE_NAME  "Temp.Dat"
  281.  
  282.  
  283. /* Writer Proc */
  284.  
  285. #ifdef C_ANSI
  286. SHORT
  287. FioProc2(PROC_P  pstProc)
  288. #else
  289. SHORT
  290. FioProc2(pstProc)
  291. PROC_P  pstProc;
  292. #endif
  293. {
  294.    C_DEF_MODULE("FioProc2")
  295.  
  296.    FILE *fp;
  297.  
  298.    fp = fopen(TEMP_FILE_NAME,"w");
  299.    fclose(fp);
  300.  
  301.  
  302.    /* It is highly recommended that you write out the file as a temporary file
  303.       and rename it after it is closed.  This will prevent the reader from
  304.       dealing with situations where the data has not been flushed out to
  305.       disk by the operating system (i.e. Unix).
  306.    */
  307.  
  308.    rename(TEMP_FILE_NAME,szWriterSeedPathName);
  309.  
  310.    printf("Writer: Created file %s\n",szWriterSeedPathName);
  311.    FioIncSeedPathName(szWriterSeedPathName);
  312.  
  313.    pstProc -> lWakeTime = MosCurTime()+0;  /* yield cpu */
  314.  
  315.    C_STATUS = MosSleep(pstProc);
  316.  
  317.    C_RETURN
  318. }
  319.  
  320.  
  321. #ifdef C_ANSI
  322. main(VOID)
  323. #else
  324. main()
  325. #endif
  326. {
  327.     C_DEF_MODULE("FioTestMain")
  328.  
  329.     C_STATUS = MosInit();
  330.  
  331.     C_STATUS = MosProcCreate(FioProc1,"FIO_PROC_1",10);
  332.     C_STATUS = MosProcCreate(FioProc2,"FIO_PROC_2",10);
  333.  
  334.     C_STATUS = MosScheduler();
  335.  
  336. }
  337.  
  338.  
  339.  
  340. #endif
  341.  
  342.  
  343.  
  344.  
  345.