• Main Page
  • Related Pages
  • Data Structures
  • Files
  • File List
  • Globals

flatline.c

Go to the documentation of this file.
00001 /** @file flatline.c ad-hoc test file looking for an error condition.
00002  * test files for flatline condition
00003  * input files are channelized shorts
00004  */
00005 
00006 
00007 #include <stdio.h>
00008 #include <errno.h>
00009 #include <fcntl.h>
00010 #include <stdlib.h>
00011 #include <unistd.h>
00012 
00013 
00014 #define NFLATS 32
00015 #define MAXBUF 0x10000
00016 
00017 unsigned short last_value;
00018 int repeat_count;
00019 
00020 
00021 int error_count;
00022 short* buffer;
00023 const char* fname;
00024 
00025 void find_flatline(int fd)
00026 {
00027 #define FLATFMT "FLATLINE %s %5d 0x%04x %6s at %d\n"
00028 #define HEXIBLE(v) ((v)&0x0000ffff)
00029         int totread = 0;
00030         int nread;
00031         int nflats;
00032 
00033         for( ; (nread = read(fd, buffer, MAXBUF)) > 0; totread += nread){
00034                 int ibuf;
00035 
00036                 for (ibuf = 0, nread /= 2; ibuf != nread; ++ibuf){
00037                         switch(last_value){
00038                         case 0xdead:
00039                         case 0xbeef:
00040                                 nflats = 2; break;
00041                         default:
00042                                 nflats = NFLATS;
00043                         }
00044                         if (buffer[ibuf] == last_value){
00045                                 if (++repeat_count == nflats){
00046                                         fprintf(stderr, FLATFMT,
00047                                                 fname,
00048                                                 last_value, 
00049                                                 HEXIBLE(last_value),
00050                                                 "starts",
00051                                                 totread + ibuf - nflats);
00052                                 }
00053                         }else{
00054                                 if (repeat_count >= nflats){
00055                                         fprintf(stderr, FLATFMT,
00056                                                 fname,
00057                                                 last_value, 
00058                                                 HEXIBLE(last_value),
00059                                                 "ends",
00060                                                 totread + ibuf);
00061                                 }
00062                                 last_value = buffer[ibuf];
00063                                 repeat_count = 0;
00064                         }
00065                 }
00066         }
00067 }
00068 
00069 int main(int argc, const char* argv[])
00070 {
00071         int iarg;
00072 
00073         buffer = malloc(MAXBUF);
00074 
00075         if (argc <= 1){
00076                 fname = "stdin";
00077                 find_flatline(0);
00078         }else{
00079                 for (iarg = 1; iarg < argc; ++iarg){
00080                         int fd = open(fname = argv[iarg], O_RDONLY);
00081 
00082                         if (fd < 0){
00083                                 perror("failed to open file");
00084                                 exit(errno);
00085                         }else{
00086                                 find_flatline(fd);
00087                         }
00088                 }
00089         }
00090 
00091         exit(error_count != 0);
00092 }

Generated on Wed Jan 5 2011 for llcontrol by  doxygen 1.7.1