home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Misc / DC-POS24.LZX / pOS / pOS_RKRM.lzx / pOS_RKRM / pDos / DosSynIO.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-03-18  |  6.4 KB  |  235 lines

  1.  
  2. /*******************************************************************
  3.  $CRT 02 Jun 1996 : hb
  4.  
  5.  $AUT Holger Burkarth
  6.  $DAT >>DosSynIO.c<<   19 Nov 1996    11:54:36 - (C) ProDAD
  7. *******************************************************************/
  8.  
  9. //##ex mcpp:cppc -gs -o pos:pos/Ex/DosSynIO p:pLib/StartCode.o p:/pOS_RKRM/pDos/DosSynIO.c p:pLib/StdIO.o -l pOSStub -l pOS
  10.  
  11. /***********************************************************
  12.   pOS programing example - Copyright (C) 1995-97 proDAD
  13.  
  14.   This code was written as an easy to understand example,
  15.   how to program pOS features. It is provided 'as-is',
  16.   without any express or implied warranty.
  17.  
  18.   Permission is hereby granted to use, copy and modify
  19.   this source code for any purpose, without fee, subject
  20.   to the following conditions:
  21.  
  22.     (1) This notice may not be removed or altered from any
  23.         source distribution.
  24.  
  25.     (2) Altered source versions must be plainly marked as
  26.         such, and must not be misrepresented as being
  27.         the original source code.
  28.  
  29.     (3) If only executable code is distributed, then the
  30.         accompanying documentation have to state that
  31.         "this software is based in part on examples of
  32.         the pOS developer packet".
  33.  
  34.     (4) Permission for use of this code is granted only
  35.         if the user accepts full responsibility for any
  36.         undesirable consequences. proDAD accept NO LIABILITY
  37.         for damages of any kind.
  38.  
  39.   ©proDAD
  40. ***********************************************************/
  41.  
  42. /*\
  43. *** Example:
  44. *** struct pOS_FHManage
  45. *** struct pOS_DosIOReq
  46. *** struct pOS_DosArgs
  47. *** struct pOS_FileHandle
  48. ***
  49. *** pOS_DoIO
  50. *** pOS_ReadDosArgs
  51. *** pOS_OpenFile
  52. *** pOS_AllocMem
  53. *** pOS_ReadFile
  54. *** pOS_CheckBreakSignal
  55. *** pOS_WriteFile
  56. *** pOS_PrintDosErr
  57. *** pOS_CloseFile
  58. *** pOS_DeleteDosArgs
  59. ***
  60. \*/
  61.  
  62.  
  63. #define __COMPUTER_AMIGA 1
  64. #define NOMYDEBUG
  65.  
  66. #include "p:pExec/Types.h"
  67. #include "p:pExec/Memory.h"
  68. #include "p:pDOS/ArgTags.h"
  69. #include "p:pDOS/DosSig.h"
  70. #include "p:pDOS/DosErrors.h"
  71. #include "p:pDOS/DosDev.h"
  72. #include "p:pDOS/Files.h"
  73. #include "p:proto/pLibExt.h"
  74. #include "p:proto/pExec2.h"
  75. #include "p:proto/pDOS2.h"
  76.  
  77. #ifdef _____ME_____
  78.   #include "grund/inc_string.h"
  79.   #include "grund/inc_stdio.h"
  80. #else
  81.  #ifdef __cplusplus
  82.  extern "C" {
  83.  #endif
  84.   #include <string.h>
  85.   #include <stdio.h>
  86.  #ifdef __cplusplus
  87.  }
  88.  #endif
  89. #endif
  90.  
  91.  
  92. const CHAR *HelpText=
  93. ""
  94. ;
  95.  
  96. const CHAR *PrgHeader=
  97. "Dos-Device - Lister";
  98.  
  99. const CHAR *PrgVerText=
  100. "$VER: 1.0 ("__DATE2__") (Copyright 1996 by proDAD) (Created by Holger Burkarth)";
  101.  
  102.  
  103.  
  104. struct ManData
  105. {
  106.   pOS_FHManage FMM;
  107. };
  108.  
  109.  
  110. /*----------------------------------
  111. -----------------------------------*/
  112. VOID Crypt_Read_func(_R_LB struct pOS_DosBase* dos,_R_A0 struct pOS_DosIOReq* io)
  113. {
  114. /*  ManData *Data=(ManData*)io->dr_U.dr_ReadFH.drrd_FH->fh_Manage; */
  115.   UBYTE *Adr=(UBYTE*)io->dr_U.dr_ReadFH.drrd_Adr; /* Adresse vor DoIO() merken */
  116.  
  117.   pOS_DoIO((struct pOS_IORequest*)io); /* Daten vom Handler lesen */
  118.  
  119.   if(io->dr_Error2==0) {
  120.     dossize_t i;
  121.     for(i=io->dr_U.dr_ReadFH.drrd_ResSize; i>0; --i,++Adr) *Adr += 0xa5;
  122.   }
  123. }
  124.  
  125. /*----------------------------------
  126. -----------------------------------*/
  127. VOID Crypt_Write_func(_R_LB struct pOS_DosBase* dos,_R_A0 struct pOS_DosIOReq* io)
  128. {
  129. //  ManData *Data=(ManData*)io->dr_U.dr_ReadFH.drrd_FH->fh_Manage;
  130.   UBYTE *Adr=(UBYTE*)io->dr_U.dr_WriteFH.drwd_Adr; /* ACHTUNG, Daten sind normalerweise 'const' */
  131.  
  132.   {
  133.     dossize_t i;
  134.     for(i=io->dr_U.dr_WriteFH.drwd_Size; i>0; --i,++Adr) *Adr -= 0xa5;
  135.   }
  136.  
  137.   pOS_DoIO((struct pOS_IORequest*)io); /* Daten vom Handler schreiben lassen */
  138. }
  139.  
  140.  
  141.  
  142. /*----------------------------------
  143. -----------------------------------*/
  144. #ifdef __cplusplus
  145. extern "C"
  146. #endif
  147.  
  148. VOID main()
  149. {
  150.   struct pOS_DosArgs* Args;
  151.   UWORD Err=0;
  152.   ULONG Ops[5]={0,0,1024,0,0};
  153.  
  154.   Args=pOS_ReadDosArgs(
  155. // 0                     1              2          3          4
  156. "FROM=SOURCE/A, TO=DESTINATION/A, BUF=BUFFERS/N, ENCODE/S, DECODE",
  157. Ops,sizeof(Ops)/sizeof(ULONG),
  158.  
  159.     ARGTAG_PrgHeaderText, (ULONG)PrgHeader,    /* kurze Programm-Beschreibung */
  160.     ARGTAG_HelpText,      (ULONG)HelpText,     /* Help-Texte */
  161.     ARGTAG_PrgVerText,    (ULONG)PrgVerText,   /* VER-String */
  162.     TAG_END);
  163.  
  164.   if(Args) {
  165.     struct pOS_FileHandle *SFH,*DFH;
  166.     struct ManData Data;
  167.  
  168. /*\
  169. *** Datei öffnen, CurrDir=NULL bedeutet, daß das pr_CurrentDir verwendet wird.
  170. \*/
  171.     if(SFH=pOS_OpenFile(NULL,(CHAR*)Ops[0],FILEHDMOD_Read)) {
  172.       Data.FMM=*SFH->fh_Manage; /* Standard-Daten kopieren */
  173.  
  174.       printf("ReadFile: Size=%ld\n",SFH->fh_Size); /* nach FILEHDMOD_Read ist fh_Size immer gesetzt */
  175.  
  176.       if(DFH=pOS_OpenFile(NULL,(CHAR*)Ops[1],FILEHDMOD_Write)) {
  177.         UBYTE *Buffer;
  178.         const size_t BufSize=*((ULONG*)Ops[2]);
  179.  
  180.         if(SFH->fh_Count==1 && DFH->fh_Count==1) {
  181.           SFH->fh_Manage=&Data.FMM; DFH->fh_Manage=&Data.FMM;
  182.  
  183.           if(Ops[3]) {
  184.             printf("Encode-Mode\n");
  185.             Data.FMM.fhm_Write_func=Crypt_Write_func;  /* unsere Funktion setzen */
  186.           }
  187.           else if(Ops[4]) {
  188.             printf("Decode-Mode\n");
  189.             Data.FMM.fhm_Read_func =Crypt_Read_func; /* unsere Funktion setzen */
  190.           }
  191.  
  192.           if(Buffer=(UBYTE*)pOS_AllocMem(BufSize,MEMF_PUBLIC)) {
  193.             dossize_t RSize,WSize;
  194.  
  195.             while(RSize=pOS_ReadFile(SFH,Buffer,BufSize)) {
  196.               if(pOS_CheckBreakSignal()) break;  /* Ctrl-C als Abbruch */
  197.  
  198.               printf("read %ld bytes ...",RSize);
  199.               WSize=pOS_WriteFile(DFH,Buffer,RSize);
  200.               printf(" write %ld bytes\n",WSize);
  201.               if(RSize!=WSize) {
  202.                 /** SLONG Error=pOS_GetIoErr(); **/
  203.                 pOS_PrintDosErr(NULL,"RSize!=WSize",0);
  204.                 break;
  205.               }
  206.             }
  207.             pOS_FreeMem(Buffer,BufSize);
  208.           }
  209.           else printf("Not enougth memory\n");
  210.         }
  211.         else {
  212. /*\
  213. *** Steht der 'fh_Count'-Wert nach pOS_FileOpen() nicht auf '1', so wird
  214. *** das File mehrfach verwendet und ein Verändern der shared-datas (FH)
  215. *** kann zum Chaos führen.
  216. \*/
  217.           if(SFH->fh_Count>0) printf("Cannot install read-handler\n");
  218.           if(DFH->fh_Count>0) printf("Cannot install write-handler\n");
  219.         }
  220.         pOS_CloseFile(DFH);
  221.       }
  222.       else printf("Cannot open <%s>\n",Ops[1]);
  223.       pOS_CloseFile(SFH);
  224.     }
  225.     else printf("Cannot open <%s>\n",Ops[0]);
  226.  
  227.     pOS_DeleteDosArgs(Args);  /* Args freigeben */
  228.   }
  229.   else Err=DOSFAIL_FAIL;  /* vollkommen fehlgeschlagen */
  230.  
  231.   pOS_SetShellFail(Err);
  232. }
  233.  
  234.  
  235.