File indexing completed on 2025-01-26 04:13:20
0001 #include <stdio.h> 0002 #include <stdlib.h> 0003 #include <getdata.h> 0004 #include <sys/types.h> 0005 #include <sys/stat.h> 0006 #include <unistd.h> 0007 #include <errno.h> 0008 #include <string.h> 0009 #include <time.h> 0010 0011 0012 void usage() { 0013 fprintf(stderr,"usage: dirfile_replay [-r <framerate>] [-p <preplay>] <source> <dest>\n" 0014 " -r <framerate>: rate at which frames are written. Default 5Hz\n" 0015 " -p <preplay>: initial replay time before timer starts\n" 0016 " <source>: dirfile to copy from\n" 0017 " <dest>: dirfile to copy to (erases any existing file)\n"); 0018 exit (0); 0019 } 0020 0021 void *buffer = NULL; 0022 0023 void process_field_entry(int i_frame, gd_entry_t *entry, 0024 DIRFILE *sourcedf, DIRFILE *destdf) { 0025 int n_rw; 0026 n_rw = gd_getdata(sourcedf, entry->field, i_frame, 0 , 1, 0, 0027 entry->data_type, buffer); 0028 if (n_rw != entry->spf) { 0029 fprintf(stderr, "read wrong number of samples (%d != %d) in %s\n", 0030 n_rw, entry->spf, entry->field); 0031 exit(0); 0032 } 0033 0034 n_rw = gd_putdata(destdf, entry->field, i_frame, 0 , 1, 0, 0035 entry->data_type, buffer); 0036 if (n_rw != entry->spf) { 0037 fprintf(stderr, "wrote wrong number of samples (%d != %d) in %s\n%s", 0038 n_rw, entry->spf, entry->field, gd_error_string(destdf,NULL, 0)); 0039 exit(0); 0040 } 0041 } 0042 0043 int main(int argc, char *argv[]) { 0044 char *source = NULL; 0045 char *dest = NULL; 0046 double rate = 5.0; 0047 int i_c; 0048 int ok_to_write = 0; 0049 char instr[1024]; 0050 int i_field; 0051 int n_fields; 0052 int i_frame; 0053 int n_frames; 0054 int i_ref = 0; 0055 int max_spf = 0; 0056 gd_entry_t *entry_list; 0057 gd_entry_t entry; 0058 char *reference; 0059 time_t t0; 0060 time_t preplay = 0; 0061 double t; 0062 struct timeval tv; 0063 0064 0065 DIRFILE *sourcedf; 0066 DIRFILE *destdf; 0067 struct stat sb; 0068 0069 for (i_c = 1; i_c < argc; i_c++) { 0070 if (argv[i_c][0] == '-') { // flags 0071 if (argv[i_c][1] == 'r') { 0072 i_c++; 0073 if (i_c < argc) { 0074 rate = atof(argv[i_c]); 0075 } else { 0076 usage(); // no argument to -r 0077 } 0078 } else if (argv[i_c][1] == 'p') { 0079 i_c++; 0080 if (i_c < argc) { 0081 preplay = atoi(argv[i_c]); 0082 } else { 0083 usage(); // no argument to -p 0084 } 0085 } else { 0086 usage(); // unrecognised flag 0087 } 0088 } else { // files 0089 if (source == NULL) { 0090 source = argv[i_c]; 0091 } else if (dest == NULL) { 0092 dest = argv[i_c]; 0093 } else { 0094 usage(); // too many parameters 0095 } 0096 } 0097 } 0098 if ((dest == NULL) || (rate <=0.0)) { 0099 usage(); 0100 } 0101 0102 /* open source file */ 0103 sourcedf = gd_open(source, GD_RDONLY); 0104 if (gd_error(sourcedf) != GD_E_OK) { 0105 fprintf(stderr, "dirfile_replay: error opening source file %s\n" 0106 "%s\n", source, gd_error_string(sourcedf,NULL, 0)); 0107 exit(0); 0108 } else { 0109 printf("opened source file %s with %ld frames\n", source, gd_nframes(sourcedf)); 0110 } 0111 0112 /* open dest file */ 0113 /* see if the dest file already exists */ 0114 errno=0; 0115 ok_to_write = stat(dest, &sb); 0116 //stat(dest, buf); 0117 if (ok_to_write == 0) { 0118 printf("The destination %s already exists! \n" 0119 " Overwriting will irretrivably delete what is already there!\n" 0120 " Overwite it? (yes/no) ", dest); 0121 fgets(instr, 6, stdin); 0122 if (strncmp(instr,"yes\n",4)!=0) { 0123 printf("You didn't say 'yes', so we are aborting rather than deleting the data.\n"); 0124 exit(0); 0125 } 0126 } 0127 0128 destdf = gd_open(dest, GD_RDWR|GD_CREAT|GD_TRUNC); 0129 if (gd_error(destdf) != GD_E_OK) { 0130 fprintf(stderr, "dirfile_replay: error opening destination file %s\n" 0131 "%s\n", 0132 dest, gd_error_string(destdf,NULL, 0)); 0133 exit(0); 0134 } else { 0135 printf("replaying %s into %s at %gHz\n", source, dest, rate); 0136 } 0137 0138 /* create the format file */ 0139 n_fields = gd_nfields(sourcedf); 0140 //field_list = gd_field_list(sourcedf); 0141 for (i_field = 0; i_field<n_fields; i_field++) { 0142 int i_meta; 0143 int n_meta; 0144 char field[300]; 0145 strncpy(field, gd_field_list(sourcedf)[i_field], 299); 0146 gd_entry(sourcedf, field, &entry); 0147 if (gd_add(destdf, &entry)!=0) { 0148 fprintf(stderr, "dirfile_replay: error adding %s to %s\n" 0149 "%s\n", 0150 field, dest, gd_error_string(destdf,NULL, 0)); 0151 //exit(0); 0152 0153 } 0154 printf("%s %d\n", entry.field, entry.field_type); 0155 //n_meta = gd_nmfields(sourcedf, field); 0156 //for (i_meta=0; i_meta<n_meta; i_meta++) { 0157 //char mfield[300]; 0158 //int error; 0159 //strncpy(mfield, gd_mfield_list(sourcedf,field)[i_meta], 299); 0160 //error = gd_entry(sourcedf, mfield, &entry); 0161 //printf(" %s %d\n", mfield, error); 0162 //gd_madd(destdf, &entry, field); 0163 //} 0164 } 0165 gd_flush(destdf, NULL); 0166 0167 n_frames = gd_nframes(sourcedf); 0168 n_fields = gd_nfields_by_type(sourcedf,GD_RAW_ENTRY); 0169 0170 // Create the entry list; allocate buffers. 0171 entry_list = (gd_entry_t *)malloc(n_fields*sizeof(gd_entry_t)); 0172 for (i_field= 0; i_field<n_fields; i_field++) { 0173 char field[300]; 0174 strncpy(field, 0175 gd_field_list_by_type(sourcedf, GD_RAW_ENTRY)[i_field], 0176 299); 0177 gd_entry(sourcedf, field, entry_list + i_field); 0178 if (entry_list[i_field].spf>max_spf) { 0179 max_spf = entry_list[i_field].spf; 0180 } 0181 if (strncmp(gd_reference(sourcedf, NULL), field, 299)==0) { 0182 i_ref = i_field; 0183 } 0184 } 0185 printf("Reference field %d %s\n", i_ref, entry_list[i_ref].field); 0186 buffer = (void *)malloc(max_spf * sizeof(double)); 0187 0188 gettimeofday(&tv, NULL); 0189 t0 = tv.tv_sec-preplay; 0190 printf("started %s", ctime(&t0)); 0191 for (i_frame = 0; i_frame < n_frames; ) { 0192 gettimeofday(&tv, NULL); 0193 t = (double)(tv.tv_sec-t0) + (double)tv.tv_usec/1.0E6; 0194 if (t > (double)i_frame/rate) { // we are behind 0195 printf("frame %d (%.2fs)\r", i_frame, t); 0196 fflush(stdout); 0197 for (i_field = 0; i_field<n_fields; i_field++) { 0198 if (i_field != i_ref) { 0199 process_field_entry(i_frame, entry_list+i_field, sourcedf, destdf); 0200 } 0201 } 0202 if (i_frame == n_frames-1) { 0203 usleep(10000); 0204 } 0205 process_field_entry(i_frame, entry_list+i_ref, sourcedf, destdf); 0206 i_frame++; 0207 } else { // we are caught up 0208 usleep(10000); 0209 } 0210 } 0211 gd_flush(destdf, NULL); 0212 0213 return (0); 0214 }