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

  1. //////////////////////////////////////////////////////////////////////// 
  2. //+ 
  3. //  Module Name:  fio.cpp 
  4. // 
  5. //  Description:  FIO specific routines 
  6. // 
  7. //  Include Modules Referenced:  fio.h 
  8. // 
  9. //  Written by:  John Tal 
  10. // 
  11. // 
  12. //  Modification history: 
  13. // 
  14. //  Date         Engineer     Mod #          Modification Description 
  15. // 
  16. //  12-Feb-1992  Tal          v 1.0-001      Initial conversion to C++ 
  17. //- 
  18. //////////////////////////////////////////////////////////////////////// 
  19.  
  20.  
  21.  
  22.  
  23. #include <fio.h> 
  24.   
  25. //////////////////////////////////////////////////////////////////////// 
  26. //+ 
  27. // Function Name:   GetFile 
  28. // 
  29. // Class:           FIO_C 
  30. // 
  31. // Security:        Public 
  32. // 
  33. // Description:     Checks for existance of a file 
  34. // 
  35. // Parameters 
  36. //    In:           pcSeedPathName = File being looked for 
  37. // 
  38. // Return Codes:    SHORT = FIO_OK = file exists 
  39. //                        = FIO_NO_FILE = file does not exist 
  40. // 
  41. // Written by:      John Tal 
  42. // 
  43. // 
  44. // Modification History: 
  45. // 
  46. //  Date         Engineer     Mod #          Modification Description 
  47. // 
  48. //  12-Feb-1992  Tal          v 1.0-001      Initial conversion to C++ 
  49. // 
  50. //- 
  51. //////////////////////////////////////////////////////////////////////// 
  52.  
  53.  
  54. SHORT FIO_C::GetFile(PCHAR pcSeedPathName)
  55.     C_DEF_MODULE("FIO_C::GetFile") 
  56.  
  57.     struct stat stStatMain; 
  58.  
  59.     /* check if file exists */ 
  60.  
  61.     C_STATUS = stat(pcSeedPathName,&stStatMain);     
  62.   
  63.     if(C_STATUS) 
  64.        C_SET_STATUS(FIO_NO_FILE) 
  65.     else 
  66.        C_SET_STATUS(FIO_OK) 
  67.  
  68.     C_RETURN 
  69.  
  70.   
  71. //////////////////////////////////////////////////////////////////////// 
  72. //+ 
  73. // Function Name:   RemoveFile 
  74. // 
  75. // Class:           FIO_C 
  76. // 
  77. // Security:        Public 
  78. // 
  79. // Description:     Remove a file, to be called by reader only 
  80. // 
  81. // Parameters 
  82. //    In:           pcSeedPathName = File to be removed 
  83. // 
  84. // Return Codes:    SHORT = result from system unlink call 
  85. // 
  86. // Written by:      John Tal 
  87. // 
  88. // 
  89. // Modification History: 
  90. // 
  91. //  Date         Engineer     Mod #          Modification Description 
  92. // 
  93. //  12-Feb-1992  Tal          v 1.0-001      Initial conversion to C++ 
  94. // 
  95. //- 
  96. //////////////////////////////////////////////////////////////////////// 
  97.   
  98. SHORT FIO_C::RemoveFile(PCHAR pcSeedPathName)
  99.    C_DEF_MODULE("FIO_C::RemoveFile") 
  100.  
  101.    C_STATUS = unlink(pcSeedPathName); 
  102.    
  103.    C_RETURN 
  104.  
  105.   
  106. //////////////////////////////////////////////////////////////////////// 
  107. //+ 
  108. // Function Name:   IncSeedPathName 
  109. // 
  110. // Class:           FIO_C 
  111. // 
  112. // Security:        Public 
  113. // 
  114. // Description:     Increment the extension of a filename 
  115. // 
  116. // Parameters 
  117. //    In:           pcSeedPathName = Filename to increment 
  118. // 
  119. // Return Codes:    SHORT = C_OK = Got an item 
  120. //                        = C_NOTOK = Heap empty 
  121. // 
  122. // Written by:      John Tal 
  123. // 
  124. // 
  125. // Modification History: 
  126. // 
  127. //  Date         Engineer     Mod #          Modification Description 
  128. // 
  129. //  12-Feb-1992  Tal          v 1.0-001      Initial conversion to C++ 
  130. // 
  131. //- 
  132. //////////////////////////////////////////////////////////////////////// 
  133.   
  134. SHORT FIO_C::IncSeedPathName(PCHAR pcSeedPathName)
  135. {
  136.     C_DEF_MODULE("FIO_C::IncSeedPathName")
  137.  
  138.     INT   iPos;
  139.     CHAR  szExtension[FIO_EXTENSION_LEN + 2];
  140.     INT   iExtension;
  141.     CHAR  szWorkStr[128];
  142.  
  143.     iPos = FindChar(pcSeedPathName,FIO_PERIOD); 
  144.  
  145.     if(iPos == FIO_NO_CHAR) 
  146.        C_LEAVE(iPos); 
  147.  
  148.     strcpy(szExtension,&pcSeedPathName[iPos + 1]); 
  149.     iExtension = atoi(szExtension); 
  150.     iExtension++; 
  151.     if(iExtension > FIO_MAX_EXTENSION) 
  152.        iExtension = 0; 
  153.  
  154.     sprintf(szExtension,"%d",iExtension); 
  155.     while(strlen(szExtension) < FIO_EXTENSION_LEN) 
  156.     { 
  157.        strcpy(szWorkStr,"0"); 
  158.        strcat(szWorkStr,szExtension); 
  159.        strcpy(szExtension,szWorkStr); 
  160.     } 
  161.  
  162.     strcpy(&pcSeedPathName[iPos + 1],szExtension); 
  163.  
  164. C_MODULE_EXIT: 
  165.  
  166.     C_RETURN 
  167.  
  168.   
  169. //////////////////////////////////////////////////////////////////////// 
  170. //+ 
  171. // Function Name:   FindChar 
  172. // 
  173. // Class:           FIO_C 
  174. // 
  175. // Security:        Private 
  176. // 
  177. // Description:     Looks for a character in a string [0] based 
  178. // 
  179. // Parameters 
  180. //    In:           pcString = string to check 
  181. //                  cChar = character to search for 
  182. // 
  183. // Return Codes:    SHORT = FIO_NOCHAR = Charcter not found 
  184. //                        = !FIO_NO_CHAR = Offset of charcter in string 
  185. // 
  186. // Written by:      John Tal 
  187. // 
  188. // 
  189. // Modification History: 
  190. // 
  191. //  Date         Engineer     Mod #          Modification Description 
  192. // 
  193. //  12-Feb-1992  Tal          v 1.0-001      Initial conversion to C++ 
  194. // 
  195. //- 
  196. //////////////////////////////////////////////////////////////////////// 
  197.   
  198.  
  199. SHORT FIO_C::FindChar(PCHAR pcString, CHAR cChar)
  200. {
  201.     C_DEF_MODULE("FIO_C::FindChar")
  202.  
  203.     INT i;
  204.     INT iLen;
  205.  
  206.     iLen = strlen(pcString);
  207.     for(i = 0; i < iLen; i++)
  208.       if(pcString[i] == cChar)
  209.      break;
  210.  
  211.     if(i < iLen)
  212.       C_SET_STATUS(i)
  213.     else
  214.       C_SET_STATUS(FIO_NO_CHAR)
  215.  
  216.     C_RETURN
  217. }
  218.  
  219.  
  220.  
  221. #define TEST
  222. #ifdef TEST
  223.  
  224.  
  225. #include <cmemlib.h>
  226.  
  227. #include "mos.h"
  228.  
  229.  
  230. char  szReaderSeedPathName[] = "Testing.000"; 
  231. char  szWriterSeedPathName[] = "Testing.000"; 
  232.  
  233.  
  234.  
  235. //
  236. //  Reader Proc 
  237. //
  238.  
  239. SHORT 
  240. Proc1(MOS_P pclMos, PVOID pvWorkArea)
  241. {
  242.     C_DEF_MODULE("Proc1")
  243.     FIO_C  cFio;
  244.      
  245.     C_STATUS = cFio.GetFile(szReaderSeedPathName);
  246.  
  247.     if(!C_STATUS) 
  248.     { 
  249.        printf("Reader: Working on file %s\n",szReaderSeedPathName); 
  250.        cFio.RemoveFile(szReaderSeedPathName); 
  251.        cFio.IncSeedPathName(szReaderSeedPathName); 
  252.     } 
  253.     else 
  254.     { 
  255.        C_STATUS = pclMos -> Sleep(1);
  256.     } 
  257.  
  258.     pvWorkArea = pvWorkArea;  // keep compiler quiet
  259.  
  260.     C_RETURN 
  261.  
  262.  
  263.  
  264.  
  265. #define TEMP_FILE_NAME  "Temp.Dat" 
  266.  
  267. // 
  268. // Writer Proc
  269. //
  270.  
  271. SHORT 
  272. Proc2(MOS_P pclMos,PVOID pvWorkArea) 
  273.    C_DEF_MODULE("Proc2") 
  274.    FIO_C  cFio; 
  275.  
  276.    FILE *fp; 
  277.  
  278.    fp = fopen(TEMP_FILE_NAME,"w"); 
  279.    fclose(fp); 
  280.  
  281.    /* It is highly recommended that you write out the file as a temporary file 
  282.       and rename it after it is closed.  This will prevent the reader from 
  283.       dealing with situations where the data has not been flushed out to 
  284.       disk by the operating system (i.e. Unix). 
  285.    */ 
  286.  
  287.    rename(TEMP_FILE_NAME,szWriterSeedPathName); 
  288.  
  289.    printf("Writer: Created file %s\n",szWriterSeedPathName); 
  290.    cFio.IncSeedPathName(szWriterSeedPathName); 
  291.  
  292.    // C_STATUS = pclMos -> Sleep(1);
  293.  
  294.    pvWorkArea = pvWorkArea;  // keep compiler quiet
  295.  
  296.    C_RETURN 
  297.  
  298.  
  299. main(VOID)
  300. {
  301.     C_DEF_MODULE("Fio Test Main") 
  302.  
  303.     char szSeedPathName[128 + 1]; 
  304.     int iMode; 
  305.     int iSts; 
  306.     FILE * fp; 
  307.  
  308.     MOS_C  clMos;
  309.  
  310.     C_STATUS = clMos.ProcCreate(Proc1,"FIO_PROC_1",10,NULL);
  311.     C_STATUS = clMos.ProcCreate(Proc2,"FIO_PROC_2",10,NULL); 
  312.  
  313.     C_STATUS = clMos.Scheduler(); 
  314.  
  315.     C_RETURN
  316.  
  317.  
  318.  
  319. #endif 
  320.  
  321.  
  322.