/* //- // ========================================================================== // Copyright (C) 1995 - 2006 Autodesk, Inc. and/or its licensors. All // rights reserved. // // The coded instructions, statements, computer programs, and/or related // material (collectively the "Data") in these files contain unpublished // information proprietary to Autodesk, Inc. ("Autodesk") and/or its // licensors, which is protected by U.S. and Canadian federal copyright // law and by international treaties. // // The Data is provided for use exclusively by You. You have the right // to use, modify, and incorporate this Data into other products for // purposes authorized by the Autodesk software license agreement, // without fee. // // The copyright notices in the Software and this entire statement, // including the above license grant, this restriction and the // following disclaimer, must be included in all copies of the // Software, in whole or in part, and all derivative works of // the Software, unless such copies or derivative works are solely // in the form of machine-executable object code generated by a // source language processor. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND. // AUTODESK DOES NOT MAKE AND HEREBY DISCLAIMS ANY EXPRESS OR IMPLIED // WARRANTIES INCLUDING, BUT NOT LIMITED TO, THE WARRANTIES OF // NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR // PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE, OR // TRADE PRACTICE. IN NO EVENT WILL AUTODESK AND/OR ITS LICENSORS // BE LIABLE FOR ANY LOST REVENUES, DATA, OR PROFITS, OR SPECIAL, // DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES, EVEN IF AUTODESK // AND/OR ITS LICENSORS HAS BEEN ADVISED OF THE POSSIBILITY // OR PROBABILITY OF SUCH DAMAGES. // // ========================================================================== //+ */ #include #ifndef _WIN32 #include #include #else #include #include #include #endif #include #include #include #ifdef LINUX #define GETOPTHUH '?' #endif /* LINUX */ #ifdef _WIN32 #define PATH_MAX _MAX_PATH char * optarg = NULL; int optind = 1; int opterr = 0; int optlast = 0; #define GETOPTHUH 0 int getopt(int argc, char **argv, char *pargs) { if (optind >= argc) return EOF; if (optarg==NULL || optlast==':') { optarg = argv[optind]; if (*optarg!='-' && *optarg!='/') return EOF; } if (*optarg=='-' || *optarg=='/') optarg++; pargs = strchr(pargs, *optarg); if (*optarg) optarg++; if (*optarg=='\0') { optind++; optarg = NULL; } if (pargs == NULL) return 0; //error if (*(pargs+1)==':') { if (optarg==NULL) { if (optind >= argc) return EOF; // we want a second paramter optarg = argv[optind]; } optind++; } optlast = *(pargs+1); return *pargs; } int gettimeofday(struct timeval* tp) { unsigned int dw = timeGetTime(); tp->tv_sec = dw/1000; tp->tv_usec = dw%1000; return 0; } #endif size_t gBufferSize = 4096; float gPlayFreq = 30; int gShowUsage = 0; char gProgramName[PATH_MAX] = { 0 }; char gDataPath[PATH_MAX] = { 0 }; FILE *gDataFile = NULL; char *gBuffer; double toDouble(const struct timeval t) { return (double)(t.tv_sec) + (double)(t.tv_usec) / 1000000.0; } struct timeval toTimeval(double sec) { struct timeval tim; tim.tv_sec = sec; tim.tv_usec = (sec - tim.tv_sec) * 1000000; return tim; } void printUsage() { fprintf(stderr, "Usage:\n"); fprintf(stderr, " %s [-H] [-b size] -f file\n", gProgramName); fprintf(stderr, "\n"); fprintf(stderr, " -f file file to read\n"); fprintf(stderr, " -b size max line length for input file\n"); fprintf(stderr, " -h Print this help message\n"); fprintf(stderr, " -H default record frequency in Hz\n"); fprintf(stderr, "\n"); exit(1); } void parseArgs(int argc, char **argv) { int opt; char *cptr; float optFloat; /* * Grab a copy of the program name */ #ifdef _WIN32 _splitpath (argv[0], NULL, NULL, gProgramName, NULL); #else cptr = strrchr(argv[0], '/'); if (cptr) { strcpy(gProgramName, (cptr + 1)); } else { strcpy(gProgramName, argv[0]); } #endif /* * Set default values */ gBufferSize = 4096; gPlayFreq = 30; gShowUsage = 0; while ((opt = getopt(argc, argv, "b:H:hf:")) != -1) { switch (opt) { case 'f' : strcpy(gDataPath, optarg); break; case 'h': gShowUsage++; break; case 'b' : gBufferSize = atoi(optarg); break; case 'H': if ( 1 == sscanf( optarg ,"%f", &optFloat) ) { gPlayFreq = optFloat; } else { gShowUsage++; } break; case GETOPTHUH: default : gShowUsage++; } } if ( gPlayFreq <= 0. ) gShowUsage++; if ( gBufferSize < 1 ) gShowUsage++; if ( !gDataPath[0] ) gShowUsage++; if (gShowUsage) printUsage(); } main( int argc, char *argv[] ) { double playNow = 0; double playNext = 0; double playPeriod = 1./30.; FILE *dataFile = NULL; parseArgs( argc, argv); playPeriod = 1./ gPlayFreq; dataFile = fopen(gDataPath, "r"); if ( NULL == dataFile ) { fprintf(stderr, "%s: could not open file %s", gProgramName, gDataPath); exit(1); } gBuffer = malloc(gBufferSize); if ( NULL == gBuffer ) { fprintf(stderr, "%s: out of memory, -b %d failed", gProgramName, gBufferSize); exit(1); } strcpy(gBuffer,""); while (1) { struct timeval timeout, now; #ifdef LINUX gettimeofday(&now,NULL); #else gettimeofday(&now); #endif playNow = toDouble(now); if ( playNext == 0.0 ) /* The first record */ playNext = playNow; while ( playNext <= playNow ) { if ( NULL == fgets(gBuffer, gBufferSize, dataFile ) ) { rewind( dataFile); if ( NULL == fgets(gBuffer, gBufferSize, dataFile ) ) { fprintf(stderr, "%s: file read failed", gProgramName); exit(1); } } printf( "%s", gBuffer ); playNext = playNext + playPeriod; } fflush(stdout); /* * wait for commands or a timeout */ timeout = toTimeval( playNext - playNow ); select(FD_SETSIZE, NULL, NULL, NULL, &timeout); } }