/* ** Copyright (C) 2001-2007 by Carnegie Mellon University. ** ** @OPENSOURCE_HEADER_START@ ** ** Use of the SILK system and related source code is subject to the terms ** of the following licenses: ** ** GNU Public License (GPL) Rights pursuant to Version 2, June 1991 ** Government Purpose License Rights (GPLR) pursuant to DFARS 252.225-7013 ** ** NO WARRANTY ** ** ANY INFORMATION, MATERIALS, SERVICES, INTELLECTUAL PROPERTY OR OTHER ** PROPERTY OR RIGHTS GRANTED OR PROVIDED BY CARNEGIE MELLON UNIVERSITY ** PURSUANT TO THIS LICENSE (HEREINAFTER THE "DELIVERABLES") ARE ON AN ** "AS-IS" BASIS. CARNEGIE MELLON UNIVERSITY MAKES NO WARRANTIES OF ANY ** KIND, EITHER EXPRESS OR IMPLIED AS TO ANY MATTER INCLUDING, BUT NOT ** LIMITED TO, WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE, ** MERCHANTABILITY, INFORMATIONAL CONTENT, NONINFRINGEMENT, OR ERROR-FREE ** OPERATION. CARNEGIE MELLON UNIVERSITY SHALL NOT BE LIABLE FOR INDIRECT, ** SPECIAL OR CONSEQUENTIAL DAMAGES, SUCH AS LOSS OF PROFITS OR INABILITY ** TO USE SAID INTELLECTUAL PROPERTY, UNDER THIS LICENSE, REGARDLESS OF ** WHETHER SUCH PARTY WAS AWARE OF THE POSSIBILITY OF SUCH DAMAGES. ** LICENSEE AGREES THAT IT WILL NOT MAKE ANY WARRANTY ON BEHALF OF ** CARNEGIE MELLON UNIVERSITY, EXPRESS OR IMPLIED, TO ANY PERSON ** CONCERNING THE APPLICATION OF OR THE RESULTS TO BE OBTAINED WITH THE ** DELIVERABLES UNDER THIS LICENSE. ** ** Licensee hereby agrees to defend, indemnify, and hold harmless Carnegie ** Mellon University, its trustees, officers, employees, and agents from ** all claims or demands made against them (and any related losses, ** expenses, or attorney's fees) arising out of, or relating to Licensee's ** and/or its sub licensees' negligent use or willful misuse of or ** negligent conduct or willful misconduct regarding the Software, ** facilities, or other rights or assistance granted by Carnegie Mellon ** University under this License, including, but not limited to, any ** claims of product liability, personal injury, death, damage to ** property, or violation of any laws or regulations. ** ** Carnegie Mellon University Software Engineering Institute authored ** documents are sponsored by the U.S. Department of Defense under ** Contract F19628-00-C-0003. Carnegie Mellon University retains ** copyrights in all material produced under this contract. The U.S. ** Government retains a non-exclusive, royalty-free license to publish or ** reproduce these documents, or allow others to do so, for U.S. ** Government purposes only pursuant to the copyright license under the ** contract clause at 252.227.7013. ** ** @OPENSOURCE_HEADER_END@ */ /* * * rwcountFileIO.c * * This is the library which defines countFile input and output. */ #include "silk.h" RCSIDENT("$SiLK: rwcountFileIO.c 8269 2007-08-03 18:54:48Z mthomas $"); #include "rwcount.h" /* * int dumpText(FILE *tgtFile, countFile *countData) * * DESCRIPTION * drops an ascii count file to screen. * * PARAMETERS * FILE *tgtFile - the output stream * countFile *countData - the data to print * * RETURNS: * 0 on success; -1 on failure. * */ int dumpText(FILE *tgtFile, countFile *countData) { const uint32_t binInc = 1; const char * const value_format[] = { "%19s%c%17.2f%c%21.2f%c%18.2f%c\n", /* with columns */ "%s%c%.2f%c%.2f%c%.2f%c\n" /* no columns */ }; const char * const header_format[] = { "%19s%c%17s%c%21s%c%18s%c\n", /* with columns */ "%s%c%s%c%s%c%s%c\n" /* no columns */ }; uint32_t i, binCount; uint32_t startI, endI; countFileHeader *cfh; char buffer[128]; struct timeval curTime; buffer[0] = '\0'; curTime.tv_sec = 0; curTime.tv_usec = 0; /* check input to the function */ if (countData == NULL || countData->fileHeader == NULL) { skAppPrintErr("Error printing count data, no data provided"); return(1); } cfh = countData->fileHeader; /* print the titles */ if ( !cfh->no_titles ) { fprintf(tgtFile, header_format[cfh->no_columns], "Date", cfh->delimiter, "Records", cfh->delimiter, "Bytes", cfh->delimiter, "Packets", cfh->delimiter); } /* Protect ourselves against no data. When this happens, print a * row of zeroes and return. */ if (cfh->binSize == 0 || cfh->totalBins == 0 || countData->bins == NULL) { sktimestamp_r(buffer, &curTime, 1); fprintf(tgtFile, value_format[cfh->no_columns], buffer, cfh->delimiter, 0.0, cfh->delimiter, 0.0, cfh->delimiter, 0.0, cfh->delimiter); return 0; } /* * Determine where to start the output based on the start_epoch * value. If given an explicit start_epoch, use that, otherwise * skip all initial bins that have no counts. */ if (cfh->start_epoch == RWCO_START_EPOCH_UNSET) { /* No start_epoch given; find first bin with non-zero record * count. */ for (i = 0; i < cfh->totalBins; i++) { if (countData->bins[ROW_BINS * i] > 0.0) { break; } } startI = i; } else if (cfh->start_epoch < cfh->initOffset) { /* Users wants to start the data before the first bin. If * skip-zeroes is not active, print zero's until we get to the * first bin. */ /* mthomas-2005/03/09: I don't believe this branch of the "if" * ever gets called now since we set the initOffset to the * start_epoch, but leave this here in case we ever decide to * change that behavior. */ startI = 0; if (cfh->skip_zeroes == 0) { binCount = 1 + (cfh->initOffset - cfh->start_epoch) / cfh->binSize; curTime.tv_sec = cfh->initOffset - (binCount * cfh->binSize); for (i = 0; i < binCount; i+=binInc, curTime.tv_sec+=cfh->binSize) { /* figure out the title */ switch(cfh->bin_label_flag) { case BIN_LABEL_INDEX: snprintf(buffer, sizeof(buffer), "-%u", (binCount - i)); break; case BIN_LABEL_EPOCH: snprintf(buffer, sizeof(buffer), ("%" PRId64), (int64_t)curTime.tv_sec); break; case BIN_LABEL_GMT: sktimestamp_r(buffer, &curTime, 1); break; } fprintf(tgtFile, value_format[cfh->no_columns], buffer, cfh->delimiter, 0.0, cfh->delimiter, 0.0, cfh->delimiter, 0.0, cfh->delimiter); } } } else if (cfh->start_epoch >= (cfh->initOffset + (cfh->binSize * cfh->totalBins))) { /* User's starting time is greater than the times for which we * have data. */ skAppPrintErr("Epoch start time > time on final record."); return 0; } else { startI = (cfh->start_epoch - cfh->initOffset) / cfh->binSize; } /* * Now find the final bin that has data. */ for (i = (cfh->totalBins - 1); i > startI; i--) { if (countData->bins[ROW_BINS * i] > 0.0) { break; } } endI = i; curTime.tv_sec = cfh->initOffset + (startI * cfh->binSize); for(i = startI, binCount = ROW_BINS * startI; i <= endI; i += binInc, binCount += (ROW_BINS), curTime.tv_sec += cfh->binSize) { if ((countData->bins[binCount] > 0) || (countData->bins[binCount + 1] > 0) || (countData->bins[binCount + 2] > 0) || (cfh->skip_zeroes == 0)) { /* figure out the title */ switch(cfh->bin_label_flag) { case BIN_LABEL_INDEX: snprintf(buffer, sizeof(buffer), "%u", i); break; case BIN_LABEL_EPOCH: snprintf(buffer, sizeof(buffer), ("%" PRId64), (int64_t)curTime.tv_sec); break; case BIN_LABEL_GMT: sktimestamp_r(buffer, &curTime, 1); break; } fprintf(tgtFile, value_format[cfh->no_columns], buffer, cfh->delimiter, countData->bins[binCount], cfh->delimiter, countData->bins[binCount + 1], cfh->delimiter, countData->bins[binCount + 2], cfh->delimiter); } } #if 0 { fprintf(stderr, "\tinitOffset: %10u (%s)\n", cfh->initOffset, timestamp(cfh->initOffset)); fprintf(stderr, "\tstart_epoch: %10u (%s)\n", cfh->start_epoch, timestamp(cfh->start_epoch)); fprintf(stderr, "\tend time (calc): %10u (%s)\n", (cfh->initOffset + (cfh->totalBins * cfh->binSize)), timestamp(cfh->initOffset + (cfh->totalBins * cfh->binSize))); fprintf(stderr, ("\ttotal bins: %10u\n" "\tstart bin: %10u\n" "\tend bin: %10u\n"), cfh->totalBins, startI, endI); } #endif /* fclose(tgtFile); */ return 0; } /* * int dumpBinary(FILE *tgtFile, countFile *countData) * * DESCRIPTION * drops an binary count file. * * PARAMETERS * FILE *tgtFile - the output stream * countFile *countData - the data to print * * RETURNS: * 0 on success; -1 on failure. * */ int dumpBinary(FILE *tgtFile, countFile *countData) { countFileHeader *cfh; /* check input */ if (countData == NULL || countData->fileHeader == NULL) { skAppPrintErr("Error printing count data, no data provided"); return(1); } cfh = countData->fileHeader; if (_writeCountFileHeader(tgtFile, cfh)) { skAppPrintErr("Error writing header, exiting"); return RWCO_ERR_FILE; } if ((ROW_BINS * cfh->totalBins) != fwrite(countData->bins,sizeof(double),ROW_BINS*cfh->totalBins,tgtFile)) { skAppPrintErr("Error writing data, exiting"); return RWCO_ERR_FILE; } return 0; } /* ** Local Variables: ** mode:c ** indent-tabs-mode:nil ** c-basic-offset:4 ** End: */