00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include <stdlib.h>
00012 #include <stdio.h>
00013 #include "bytestreamfe.h"
00014
00015 #define BURST_LENGTH 1024
00016
00017 byteStreamFE::byteStreamFE (
00018 const Id& n,
00019 In<VYAfileName>& fileNameF,
00020 In<int>& nrBytesF,
00021 Out<unsigned char>& byteStreamF)
00022 :
00023 VYAprocess(n),
00024 fileNameP(id("fileNameP"),fileNameF),
00025 nrBytesP(id("nrBytesP"),nrBytesF),
00026 byteStreamP(id("byteStreamP"),byteStreamF)
00027 {}
00028
00029 const char* byteStreamFE::type() const
00030 {
00031 return "byteStreamFE";
00032 }
00033
00034 void byteStreamFE::main()
00035 {
00036 while(true)
00037 {
00038 FILE *fptr;
00039 unsigned char burst[BURST_LENGTH];
00040 int nrBytesRead = 0;
00041 int currentNrBytes;
00042
00043 read(fileNameP, fileName);
00044 fptr=fopen(fileName, "rb");
00045 if (!fptr) {
00046 printf("\n%s: error opening input file \"%s\"\n", fullName(),(const char*)fileName);
00047 exit(1);
00048 }
00049 #ifdef VERBOSE
00050 else
00051 printf("\n%s: reading data from %s\n", fullName(),(const char*)fileName);
00052 #endif
00053
00054 read(nrBytesP, nrBytes);
00055 if (nrBytes < 0) {
00056 printf("%s: error: negative number of bytes requested (%i)\n", fullName(), nrBytes);
00057 exit(1);
00058 }
00059
00060 while (!feof(fptr) && ((nrBytesRead!=nrBytes) || (nrBytes==0)))
00061 {
00062 currentNrBytes = fread(burst, sizeof(unsigned char), BURST_LENGTH, fptr);
00063 if ((currentNrBytes == 0) && !feof(fptr)) {
00064 printf("%s: error reading input file %s\n", fullName(),(const char*)fileName);
00065 exit(1);
00066 }
00067 else
00068 {
00069 if ((nrBytesRead+currentNrBytes > nrBytes) && (nrBytes!=0)) {
00070 currentNrBytes = nrBytes - nrBytesRead;
00071 nrBytesRead = nrBytes;
00072 }
00073 else
00074 {
00075 nrBytesRead += currentNrBytes;
00076 }
00077 write(byteStreamP, burst, currentNrBytes);
00078 }
00079 }
00080
00081 #ifdef VERBOSE
00082 if (nrBytesRead != 1)
00083 printf("%s: finished; read %i bytes from input file\n", fullName(), nrBytesRead);
00084 else
00085 printf("%s: finished; read 1 byte from input file\n", fullName());
00086 #endif
00087 fclose(fptr);
00088 }
00089 }