/* This is the main function to call */ int getInt() { std::string strInt; bool validInt = false; do{ std::cout <<"Enter an integer: "; std::getline(std::cin, strInt); if (isValidInt(strInt)) { //std::cout << "Valid int: " << strInt << std::endl; validInt = true; } else { std::cout << "Invalid input\n"; } }while(!validInt); return toInt(strInt); };
void scanValue() { Value *v = valueBuff + nValueBuff; char buff[1024]; gets(buff); if (isValidInt(buff)) { v->type = INTEGER; nValueExtraBuff += sizeof(int); *((int*)v->extra) = (int)atoi(buff); } else if (isValidFloat(buff)) { v->type = FLOAT; nValueExtraBuff += sizeof(float); *((float*)v->extra) = (float)atoi(buff); } else { v->type = STRING; v->extra = valueExtraBuff + nValueExtraBuff; nValueExtraBuff += sprintf((char*)v->extra, "%s", buff) + 1; } }
//CHAR 类型的长度限制在interpreter部分实现 bool APIManager:: checkInsertType(string tableName, vector<string> insert) { int tableIndex = catalogmanager.findTable(tableName); int attrType; int arrNum = catalogmanager.Vtable[tableIndex].attriNum; for(int i = 0; i< arrNum; ++i ) { attrType = catalogmanager.Vtable[tableIndex].attributes[i].type; switch (attrType) { case INT: // int if (!isValidInt(insert[i])) { return false; } break; case FLOAT: // float if (!isValidFloat(insert[i])) { return false; } default: break; } } return true; }
int main(int argc, char * argv[]) { // gets the hash table size from the command line int hashSize = Table::HASH_SIZE; Table * grades; // Table is dynamically allocated below, so we can call // different constructors depending on input from the user. if (argc > 1) { hashSize = atoi(argv[1]); // atoi converts c-string to int if (hashSize < 1) { cout << "Command line argument (hashSize) must be a positive number" << endl; return 1; } grades = new Table(hashSize); } else { // no command line args given -- use default table size grades = new Table(); } grades->hashStats(cout); // add more code here // Reminder: use -> when calling Table methods, since grades is type Table* printCmdSummary();//print out command lines //initialize some data char c; string name; int score; bool isValid=true; //while isvalid it will keep going ask client to enter untill quit do{ cout<<"\n Please enter the command shows above [i , c , l , r , p , s , h , t , q]: "; cin>>c; if(cin.fail()){//if cin fail promt fail info and quit cout<<"exit"<<endl; cout<<"ERROR: invalid command!"<<endl; isValid=false; } else{ switch (c){//switch method provide client to choose case 'i': cout<<"You are now in the insert method: "<<endl; cout<<"here is the example: "<<endl; cout<<"---Name: James ---"<<endl; cout<<"---Score: 86 ---"<<endl;; do{ cout<<"Please enter name: "<<endl; cin>>name; }while(isValidString(name)==false);//check valid or not do{ cout<<"Please enter score: "<<endl; cin>>score; }while(isValidInt(score)==false);//check valid or not if(grades->insert(name,score)==true){//if no same key existed then record entry cout<<"Entry recorded!"<<endl; }else{ cout<<"Entry has already existed, Nothing changes!"<<endl; } break; case 'c': cout<<"You are now in the change method: "<<endl; cout<<"here is the example: "<<endl; cout<<"---Name: James ---"<<endl; cout<<"---Score: 86 ---"<<endl;; do{ cout<<"Please enter name: "<<endl; cin>>name; }while(isValidString(name)==false); do{ cout<<"Please enter new score: "<<endl; cin>>score; }while(isValidInt(score)==false); if(grades->lookup(name)!=NULL){//if found grades->remove(name);//remove grades->insert(name,score);//then insert again cout<<"Record changed!"<<endl; }else{ cout<<"No such entry"<<endl; } break; case 'l': cout<<"You are now in the lookup method: "<<endl; cout<<"here is the example: "<<endl; cout<<"---Name: James ---"<<endl; do{ cout<<"Please enter name you want to lookup: "<<endl; cin>>name; }while(isValidString(name)==false); if(grades->lookup(name)!=NULL){//if found int* address=grades->lookup(name);//assign address to it cout<<"Record found!"<<endl; cout<<"the address is :"<<address<<endl; }else{ cout<<"No such entry"<<endl; } break; case 'r': cout<<"You are now in the remove method: "<<endl; cout<<"here is the example: "<<endl; cout<<"---Name: James ---"<<endl; do{ cout<<"Please enter name: "<<endl; cin>>name; }while(isValidString(name)==false); if(grades->remove(name)==true){//if found cout<<"Entry removed!"<<endl;//target removed }else{ cout<<"No such entry!"<<endl; } break; case 'p': cout<<"You are now in the print method: "<<endl; cout<<"here is the table: "<<endl; if(grades->numEntries()==0){ cout<<"<empty>"<<endl; }else{ grades->printAll();//print out all the entries } break; case 's': cout<<"You are now in the size method: "<<endl; cout<<"here is the total number of entries: "<<endl; cout<<grades->numEntries();//print out entries numbers break; case 'h': cout<<"You are now in the help method: "<<endl; cout<<"here is the comands: "<<endl; printCmdSummary();//print summary break; case 't': cout<<"You are now in the stats method: "<<endl; cout<<"here is the stats of this table: "<<endl; grades->hashStats(cout);//print out hashstats break; case 'q': cout<<"quitting this program"<<endl; isValid=false;//end the loop and quit break; default: cout<<"Invalid Command,Please try again."<<endl; break; } } }while(isValid); return 0; }
int main(int argc, char * argv[]){ assert(sizeof(rec_t) == 100); //arguments char * inFile; char * outFile; long highValue = -1; long lowValue = -1; rec_t input; int omitCount = 0; int withinRangeCount = 0; //check for the proper amount of parameters if(argc != 9){ usage(argv[0]); } // input params int c; opterr = 0; while ((c = getopt(argc, argv, "i:o:l:h:")) != -1) { switch (c) { case 'i': inFile = strdup(optarg); break; case 'o': outFile = strdup(optarg); break; case 'l': if(isValidInt(optarg)){ lowValue = atol(optarg); } break; case 'h': if(isValidInt(optarg)){ highValue = atol(optarg); } break; default: usage(argv[0]); } } //printf("Low: %lu \n", lowValue); //printf("High: %lu \n", highValue); //printf("Max: %u \n", UINT_MAX); //TODO //validate parameters if(lowValue > highValue || lowValue < 0 || highValue < 0 || highValue > UINT_MAX || lowValue > UINT_MAX){ fprintf(stderr, "Error: Invalid range value\n"); exit(1); } //open input file int fd = open(inFile, O_RDONLY); if(fd < 0 ){ fprintf(stderr, "Error: Cannot open file %s\n" ,inFile); exit(1); } //get input file size struct stat buf; fstat(fd, &buf); int sizeInFile = buf.st_size; rec_t *withinRange = malloc(sizeInFile); while(1){ int rc = read(fd, &input, sizeof(rec_t)); //check for EOF if(rc == 0) break; //Error Occured if(rc <0){ fprintf(stderr,"Error: Something happened while reading the file"); exit(1); } //check that key falls in the right range if(input.key < lowValue || input.key > highValue){ omitCount++; } else{ //printf("Key: %d\n", input.key); withinRange[withinRangeCount] = input; withinRangeCount++; } } //printf("Ommitted: %d\n", omitCount); //printf("Within Range: %d\n", withinRangeCount); //close input file close(fd); //sort the array - not fully tested, but it is sorting the array properly qsort(&withinRange[0], (long)withinRangeCount, sizeof(rec_t), compRec); //open output file fd = open(outFile, O_WRONLY|O_CREAT|O_TRUNC, S_IRWXU); if(fd < 0){ fprintf(stderr, "Error: Cannot open file %s\n" ,outFile); exit(1); } //write to output file int i; for(i = 0; i < withinRangeCount; i++){ // printf("Key[%d]: %d\n", i, withinRange[i].key); int rc = write(fd, &withinRange[i], sizeof(rec_t)); if(rc != sizeof(rec_t)){ fprintf(stderr, "Error: Cannot write to file %s\n" ,outFile); exit(1); } } //close output file //close(fd); printf("closed file, exiting with 0"); return 0; }
int PutOriginData ( FILE *ptr , DBlist tree ) { char HeaderLine1[] = " Date Time Latitude Longitude Depth Ndef Nsta " "Gap Mag1 N Mag2 N Mag3 N Author ID\n"; char HeaderLine2[] = " rms OT_Error Smajor Sminor Az Err mdist" " Mdist Err Err Err Quality\n"; char time[ 25 ] , fixf = ' ' , lat[ 9 ] , lon[ 10 ] , depth[ 6 ] , ndef[ 5 ] , nsta[ ] = " " , gap[ ] = " " , magtype1[ 3 ] , mag1[ 5 ] , magtype2[ 3 ] , mag2[ 5 ] , magtype3[ 3 ] , mag3[ 5 ] , N[] = " " , author[ 9 ] , id[ 9 ] , line2[] = "\n +- +- +- +- +- \n\n" ; struct CSStree *Tree = (struct CSStree *) tree ; struct originList *orig = Tree->orHead ; if(!orig) return -1; printf("Writing origin data "); fprintf(ptr,"DATA_TYPE ORIGIN GSE2.0\n"); fprintf(ptr, "%s", HeaderLine1); fprintf(ptr, "%s\n", HeaderLine2); while(orig){ putchar('.'); fflush(stdout); /* time */ if ( isValidFloat ( dbl_LIST_ORIGIN, dbl_ORIGI_TIME, orig->element->time ) ) { sprintf ( time , "%-21.21s" , tmListEpochTime ( orig->element->time , 14 ) ) ; time[ 10 ] = ' ' ; } else strcpy ( time , " " ) ; /* 21 spaces */ /* lat */ if ( isValidFloat ( dbl_LIST_ORIGIN, dbl_ORIGI_LAT, orig->element->lat ) ) sprintf ( lat , "%8.4f" , orig->element->lat ) ; else strcpy ( lat , " " ) ; /* 8 spaces */ /* lon */ if ( isValidFloat ( dbl_LIST_ORIGIN, dbl_ORIGI_LON, orig->element->lon ) ) sprintf ( lon , "%9.4f" , orig->element->lon ) ; else strcpy ( lon , " " ) ; /* 9 spaces */ /* depth */ if ( isValidFloat ( dbl_LIST_ORIGIN, dbl_ORIGI_DEPTH, orig->element->depth ) ) sprintf ( depth , "%5.1f" , orig->element->depth ) ; else strcpy ( depth , " " ) ; /* 5 spaces */ /* ndef */ if ( isValidInt ( dbl_LIST_ORIGIN, dbl_ORIGI_NDEF, orig->element->ndef ) ) sprintf ( ndef , "%4d" , orig->element->ndef ) ; else strcpy ( ndef , " " ) ; /* 4 spaces */ /* magnitudes */ if ( isValidFloat ( dbl_LIST_ORIGIN, dbl_ORIGI_MB, orig->element->mb ) ) { sprintf ( mag1 , "%4.1f" , orig->element->mb ) ; strcpy ( magtype1 , "mb" ) ; } else { strcpy ( mag1 , " " ) ; /* 4 spaces */ strcpy ( magtype1 , " " ) ; /* 2 spaces */ } if ( isValidFloat ( dbl_LIST_ORIGIN, dbl_ORIGI_MB, orig->element->ms ) ) { sprintf ( mag2 , "%4.1f" , orig->element->ms ) ; strcpy ( magtype2 , "ms" ) ; } else { strcpy ( mag2 , " " ) ; /* 4 spaces */ strcpy ( magtype2 , " " ) ; /* 2 spaces */ } if ( isValidFloat ( dbl_LIST_ORIGIN, dbl_ORIGI_MB, orig->element->ml ) ) { sprintf ( mag3 , "%4.1f" , orig->element->ml ) ; strcpy ( magtype3 , "ml" ) ; } else { strcpy ( mag3 , " " ) ; /* 4 spaces */ strcpy ( magtype3 , " " ) ; /* 2 spaces */ } /* author */ if ( isValidString ( dbl_LIST_ORIGIN, dbl_ORIGI_AUTH, orig->element->auth ) && strcmp( orig->element->auth , "- " ) ) { if ( strlen ( orig->element->auth ) <= 8 ) sprintf ( author , "%-8.8s" , orig->element->auth ) ; else { strncpy ( author , orig->element->auth , 8 ) ; author[ 8 ] = '\0' ; } } else strcpy ( author , " " ) ; /* 8 spaces */ /* id */ if ( isValidInt ( dbl_LIST_ORIGIN, dbl_ORIGI_ORID, orig->element->orid ) ) sprintf ( id , "%8d" , orig->element->orid ) ; else strcpy ( id , " " ) ; /* 8 spaces */ /* out put the strings */ fprintf ( ptr , "%s %c %s %s %c %s %c %s %s %s %s%s %s %s%s %s %s%s %s %s %s%s", time, fixf, lat, lon, fixf, depth, fixf, ndef, nsta, gap, magtype1, mag1, N, magtype2, mag2, N, magtype3, mag3, N, author, id , line2 ) ; orig = orig->next; } fprintf(ptr,"\n"); printf("\n"); return 0 ; } /* end PutOriginData */
int PutArrivalData ( FILE *ptr , DBlist tree ) { char sta[ 6 ] , dist[ 7 ] , evaz[ 6 ] , picktype = ' ' , direction , detchar , phase[ 8 ] , time[ 25 ] , tres[] = " " , azim[ 6 ] , azres[ 7 ] , slow[ 6 ] , sres[ 6 ] , tdef , adef , sdef , snr[ 6 ] , amp[ 10 ] , per[ 6 ] , mdef1[ 3 ] , mag1[ 5 ] , mdef2[ 3 ] , mag2[ 5 ] , id [ 9 ] ; struct CSStree *Tree = (struct CSStree *) tree ; struct arrivalList *ar = Tree->arHead ; int dots = 0; char HeaderLine[] = "Sta Dist EvAz Phase Date Time TRes Azim" " AzRes Slow SRes Def SNR Amp Per Mag1 Mag2 ID"; if(!ar) return -1 ; printf("Writing arrival data "); fprintf(ptr,"DATA_TYPE ARRIVAL GSE2.0\n"); fprintf(ptr, "%s\n", HeaderLine); while(ar){ struct assocList *as = Tree->asHead ; struct originList *orig = Tree->orHead ; struct siteList *si = Tree->slHead ; putchar('.'); dots++; if(dots == 80){ putchar('\n'); dots = 0; } fflush(stdout); /* sta */ if ( isValidString ( dbl_LIST_ARRIVAL, dbl_ARRIV_STA , ar->element->sta ) && strcmp( ar->element->sta , "- " ) ) sprintf ( sta , "%-5.5s" , ar->element->sta ) ; else strcpy ( sta , " " ) ; /* 5 spaces */ /* dist & evaz */ /* find assoc in order to find origin, then find sitechan */ while ( as ) { if ( as->element->arid == ar->element->arid ) break ; as = as->next ; } if ( as ) while ( orig ) { if ( as->element->orid == orig->element->orid ) break ; orig = orig->next ; } /* end while ( orig ) */ while ( si ) { if ( !strcmp ( ar->element->sta , si->element->sta ) && si->element->ondate <= ar->element->jdate && (si->element->offdate >= ar->element->jdate || si->element->offdate == -1 ) ) break ; si = si->next ; } /* end while ( si ) */ if ( orig && si && isValidFloat ( dbl_LIST_ORIGIN, dbl_ORIGI_LAT, orig->element->lat ) && isValidFloat ( dbl_LIST_ORIGIN, dbl_ORIGI_LON, orig->element->lon ) && isValidFloat ( dbl_LIST_SITE, dbl_SITE_LAT, si->element->lat ) && isValidFloat ( dbl_LIST_SITE, dbl_SITE_LON, si->element->lon ) ) { float slat = si->element->lat , slon = si->element->lon , elat = orig->element->lat , elon = orig->element->lon , delt, fDist, fAzim, bazim ; dbDelaz(&slat, &slon, &elat, &elon, &delt, &fDist, &fAzim, &bazim); if ( fDist < 1000 ) sprintf ( dist , "%6.2f" , fDist ) ; else sprintf ( dist , "%6.1f" , fDist ) ; sprintf ( evaz , "%5.1f" , fAzim ) ; } else { strcpy ( dist , " " ) ; /* 6 spaces */ strcpy ( evaz , " " ) ; /* 5 spaces */ } /* direction */ if ( isValidString ( dbl_LIST_ARRIVAL, dbl_ARRIV_FM , ar->element->fm ) ) { char *fm = ar->element->fm ; if ( fm[ 0 ] == 'c' || fm[ 1 ] == 'c' ) direction = 'c' ; else if ( fm[ 0 ] == 'd' || fm[ 1 ] == 'd' ) direction = 'd' ; else if ( fm[ 0 ] == 'u' || fm[ 1 ] == 'u' ) direction = 'c' ; else if ( fm[ 0 ] == 'r' || fm[ 1 ] == 'r' ) direction = 'd' ; else direction = ' ' ; } else direction = ' ' ; /* detchar */ if ( isValidString ( dbl_LIST_ARRIVAL, dbl_ARRIV_QUAL , ar->element->qual ) ) { if ( ar->element->qual[ 0 ] == 'i' || ar->element->qual[ 0 ] == 'e' ) detchar = ar->element->qual[ 0 ] ; else if ( ar->element->qual[ 0 ] == 'q' || ar->element->qual[ 0 ] == 'w' ) detchar = 'q' ; else detchar = ' ' ; } else detchar = ' ' ; /* phase */ if ( isValidString ( dbl_LIST_ARRIVAL, dbl_ARRIV_IPHASE , ar->element->iphase ) && strcmp( ar->element->iphase , "- " ) ) sprintf ( phase , "%-7.7s" , ar->element->iphase ) ; else strcpy ( phase , " " ) ; /* 7 spaces */ /* time */ if ( isValidFloat ( dbl_LIST_ARRIVAL, dbl_ARRIV_TIME, ar->element->time ) ) { sprintf ( time , "%-21.21s" , tmListEpochTime ( ar->element->time , 14 ) ) ; time[ 10 ] = ' ' ; } else strcpy ( time , " " ) ; /* 21 spaces */ /* azim */ if ( isValidFloat ( dbl_LIST_ARRIVAL, dbl_ARRIV_AZIMUTH , ar->element->azimuth ) ) sprintf ( azim , "%5.1f" , ar->element->azimuth ) ; else strcpy ( azim , " " ) ; /* 5 spaces */ /* azres */ if ( isValidFloat ( dbl_LIST_ARRIVAL, dbl_ARRIV_DELAZ , ar->element->delaz ) ) sprintf ( azres , "%6.1f" , ar->element->delaz ) ; else strcpy ( azres , " " ) ; /* 6 spaces */ /* slow */ if ( isValidFloat ( dbl_LIST_ARRIVAL, dbl_ARRIV_SLOW , ar->element->slow ) ) sprintf ( slow , "%5.1f" , ar->element->slow ) ; else strcpy ( slow , " " ) ; /* 5 spaces */ /* sres */ if ( isValidFloat ( dbl_LIST_ARRIVAL, dbl_ARRIV_DELSLO , ar->element->delslo ) ) sprintf ( sres , "%5.1f" , ar->element->delslo ) ; else strcpy ( sres , " " ) ; /* 5 spaces */ /* tdef, adef, & sdef */ if ( as && as->element->timedef[0] == 'd' ) tdef = 'T' ; else tdef = ' ' ; if ( as && as->element->azdef[0] == 'd' ) adef = 'A' ; else adef = ' ' ; if ( as && as->element->slodef[0] == 'd' ) sdef = 'S' ; else sdef = ' ' ; /* snr */ if ( isValidFloat ( dbl_LIST_ARRIVAL, dbl_ARRIV_SNR , ar->element->snr ) ) sprintf ( snr , "%5.1f" , ar->element->snr ) ; else strcpy ( snr , " " ) ; /* 5 spaces */ /* amp */ if ( isValidFloat ( dbl_LIST_ARRIVAL, dbl_ARRIV_AMP , ar->element->amp ) ) sprintf ( amp , "%9.1f" , ar->element->amp ) ; else strcpy ( amp , " " ) ; /* 9 spaces */ /* per */ if ( isValidFloat ( dbl_LIST_ARRIVAL, dbl_ARRIV_PER , ar->element->per ) ) sprintf ( per , "%5.2f" , ar->element->per ) ; else strcpy ( per , " " ) ; /* 5 spaces */ /* mdef1, mag1, mdef2, & mag2 */ if ( orig ) { if ( isValidFloat ( dbl_LIST_ORIGIN , dbl_ORIGI_MB , orig->element->mb ) && isValidFloat ( dbl_LIST_ORIGIN , dbl_ORIGI_MS , orig->element->ms ) ) { strcpy ( mdef1 , "mb" ) ; sprintf( mag1 , "%4.1f" , orig->element->mb ) ; strcpy ( mdef2 , "ms" ) ; sprintf( mag2 , "%4.1f" , orig->element->ms ) ; } else if ( isValidFloat ( dbl_LIST_ORIGIN , dbl_ORIGI_MB , orig->element->mb ) && isValidFloat ( dbl_LIST_ORIGIN , dbl_ORIGI_ML , orig->element->ml ) ) { strcpy ( mdef1 , "mb" ) ; sprintf( mag1 , "%4.1f" , orig->element->mb ) ; strcpy ( mdef2 , "ml" ) ; sprintf( mag2 , "%4.1f" , orig->element->ml ) ; } else if ( isValidFloat ( dbl_LIST_ORIGIN , dbl_ORIGI_MS , orig->element->ms ) && isValidFloat ( dbl_LIST_ORIGIN , dbl_ORIGI_ML , orig->element->ml ) ) { strcpy ( mdef1 , "ms" ) ; sprintf( mag1 , "%4.1f" , orig->element->ms ) ; strcpy ( mdef2 , "ml" ) ; sprintf( mag2 , "%4.1f" , orig->element->ml ) ; } else { strcpy ( mdef2 , " " ) ; strcpy ( mag2 , " " ) ; if ( isValidFloat ( dbl_LIST_ORIGIN , dbl_ORIGI_MB , orig->element->mb ) ) { strcpy ( mdef1 , "mb" ) ; sprintf( mag1 , "%4.1f" , orig->element->mb ) ; } else if ( isValidFloat ( dbl_LIST_ORIGIN , dbl_ORIGI_MS , orig->element->ms ) ) { strcpy ( mdef1 , "ms" ) ; sprintf( mag1 , "%4.1f" , orig->element->ms ) ; } else if ( isValidFloat ( dbl_LIST_ORIGIN , dbl_ORIGI_ML , orig->element->ml ) ) { strcpy ( mdef1 , "ml" ) ; sprintf( mag1 , "%4.1f" , orig->element->ml ) ; } else { strcpy ( mdef1 , " " ) ; strcpy ( mag1 , " " ) ; } } } else { strcpy ( mdef1 , " " ) ; strcpy ( mag1 , " " ) ; strcpy ( mdef2 , " " ) ; strcpy ( mag2 , " " ) ; } /* id */ if ( isValidInt ( dbl_LIST_ARRIVAL, dbl_ARRIV_ARID , ar->element->arid ) ) sprintf ( id , "%8d" , ar->element->arid ) ; else strcpy ( id , " " ) ; /* 8 spaces */ fprintf ( ptr , "%s %s %s %c%c%c %s %s %s %s %s %s %s %c%c%c %s %s %s %s%s %s%s %s", sta, dist, evaz, picktype, direction, detchar, phase, time, tres, azim, azres, slow, sres, tdef, adef, sdef, snr, amp, per, mdef1, mag1, mdef2, mag2, id ) ; fprintf(ptr, "\n"); ar = ar->next; } fprintf(ptr,"\n"); printf("\n"); return 0 ; } /* end PutArrivalData */
int PutChannelData ( FILE *ptr , DBlist tree ) { char sta[6], chan[4], auxid[ 5 ] , lat[ 10 ] , lon[ 11 ] , elev[ 8 ] , depth[ 7 ] , hang[ 7 ], vang[ 6 ], samprat[ 12 ], inst[8], ondate[25], offdate[25]; int dots = 0; float fLat , fLon , fElev ; struct CSStree *Tree = (struct CSStree *) tree ; struct sitechanList *sc = Tree->scHead ; if(!sc) return -1 ; printf("Writing channel data "); fprintf(ptr,"DATA_TYPE CHANNEL\n"); fprintf(ptr, "Sta Chan Aux Latitude Longitude Elev Depth Hang Vang Sample_Rate Inst On Date Off Date\n"); while(sc){ struct siteList *si = Tree->slHead ; struct wfdiscList *wf = Tree->wfHead ; putchar('.'); dots++; if(dots == 80){ putchar('\n'); dots = 0; } fflush(stdout); /* sta */ if ( isValidString ( dbl_LIST_SITECHAN, dbl_SITEC_STA , sc->element->sta ) && strcmp( sc->element->sta , "- " ) ) sprintf ( sta , "%-5.5s" , sc->element->sta ) ; else strcpy ( sta , " " ) ; /* 5 spaces */ /* chan */ if ( isValidString ( dbl_LIST_SITECHAN, dbl_SITEC_CHAN , sc->element->chan ) && strcmp( sc->element->chan , "- " ) ) sprintf ( chan , "%-3.3s" , sc->element->chan ) ; else strcpy ( chan , " " ) ; /* 3 spaces */ /* auxid */ strcpy ( auxid , " " ) ; /* 4 spaces */ /* lat, lon, & elev */ GetSiteInfo ( si , sc->element->sta , sc->element->ondate , sc->element->offdate , &fLat , &fLon , &fElev ) ; if ( isValidFloat ( dbl_LIST_SITE, dbl_SITE_LAT , fLat ) ) sprintf ( lat , "%9.5f" , fLat ) ; else strcpy ( lat , " " ) ; /* 9 spaces */ if ( isValidFloat ( dbl_LIST_SITE, dbl_SITE_LON , fLon ) ) sprintf ( lon , "%10.5f" , fLon ) ; else strcpy ( lon , " " ) ; /* 10 spaces */ if ( isValidFloat ( dbl_LIST_SITE, dbl_SITE_ELEV , fElev ) ) sprintf ( elev , "%7.3f" , fElev ) ; else strcpy ( elev , " " ) ; /* 7 spaces */ /* depth */ if ( isValidFloat ( dbl_LIST_SITECHAN, dbl_SITEC_EDEPTH , sc->element->edepth ) ) sprintf ( depth , "%6.3f" , sc->element->edepth ) ; else strcpy ( depth , " " ) ; /* 6 spaces */ /* hang & vang */ if ( isValidFloat ( dbl_LIST_SITECHAN, dbl_SITEC_HANG , sc->element->hang ) ) sprintf ( hang , "%6.1f" , sc->element->hang ) ; else strcpy ( hang , " " ) ; /* 6 spaces */ if ( isValidFloat ( dbl_LIST_SITECHAN, dbl_SITEC_VANG , sc->element->vang ) ) sprintf ( vang , "%5.1f" , sc->element->vang ) ; else strcpy ( vang , " " ) ; /* 5 spaces */ /* samprat */ strcpy ( samprat , " " ) ; /* 11 spaces */ /* inst */ GetWfdiscInfo ( wf, sta , chan , sc->element->ondate , sc->element->offdate , inst ) ; if ( isValidString ( dbl_LIST_WFDISC, dbl_WFDIS_INSTYPE , inst ) && strcmp( inst , "- " ) ) { if ( strlen ( inst ) < 7 ) { int kdx ; for ( kdx = strlen ( inst ) ; kdx < 7 ; kdx++ ) { inst[ kdx ] = ' ' ; } inst[ 7 ] = '\0' ; } } else sprintf ( inst , " " ) ; /* 7 spaces */ /* ondate */ if ( isValidInt ( dbl_LIST_SITECHAN, dbl_SITEC_ONDATE , sc->element->ondate ) ) { int doy, mm, id, iyyy , jdate = sc->element->ondate ; iyyy = jdate / 1000 ; doy = jdate - ( iyyy * 1000 ) ; mnday ( doy , isleap ( iyyy ) , &mm , &id ) ; sprintf(ondate, "%04d/%02d/%02d" , iyyy, mm, id ); } else strcpy ( ondate , " " ) ; /* 10 spaces */ /* offdate */ if ( isValidInt ( dbl_LIST_SITECHAN, dbl_SITEC_OFFDATE , sc->element->offdate ) ) { int doy, mm, id, iyyy , jdate = sc->element->offdate ; iyyy = jdate / 1000 ; doy = jdate - ( iyyy * 1000 ) ; mnday ( doy , isleap ( iyyy ) , &mm , &id ) ; sprintf(offdate, "%04d/%02d/%02d" , iyyy, mm, id ); } else strcpy ( offdate , " " ) ; /* 10 spaces */ fprintf(ptr,"%s %s %s %s %s %s %s %s %s %s %s %s %s\n", sta, chan, auxid, lat, lon, elev, depth, hang, vang, samprat, inst, ondate, offdate ); sc = sc->next; } fprintf(ptr,"\n"); printf("\n"); return 0 ; } /* end PutChannelData */
int PutStationData ( FILE *ptr , DBlist tree ) { char sta [ 6 ] , statype[ 5 ] , lat [ 10 ] , lon [ 11 ] , elev [ 8 ] , ondate [ 25 ] , offdate[ 25 ] ; int dots = 0; struct CSStree *Tree = (struct CSStree *) tree ; struct siteList *si = Tree->slHead ; if(!si) return -1 ; printf("Writing station data "); fprintf(ptr,"DATA_TYPE STATION\n"); fprintf(ptr, "Sta Type Latitude Longitude Elev On Date Off Date\n"); while(si){ putchar('.'); dots++; if(dots == 80){ putchar('\n'); dots = 0; } fflush(stdout); /* sta */ if ( isValidString ( dbl_LIST_SITE, dbl_SITE_STA , si->element->sta ) && strcmp( si->element->sta , "- " ) ) sprintf ( sta , "%-5.5s" , si->element->sta ) ; else strcpy ( sta , " " ) ; /* 5 spaces */ /* statype */ if ( isValidString ( dbl_LIST_SITE, dbl_SITE_STATYPE , si->element->statype ) && strcmp( si->element->statype , "- " ) ) sprintf ( statype , "%-4.4s" , si->element->statype ) ; else strcpy ( statype , " " ) ; /* 4 spaces */ /* lat */ if ( isValidFloat ( dbl_LIST_SITE, dbl_SITE_LAT , si->element->lat ) ) sprintf ( lat , "%9.5f" , si->element->lat ) ; else strcpy ( lat , " " ) ; /* 9 spaces */ /* lon */ if ( isValidFloat ( dbl_LIST_SITE, dbl_SITE_LON , si->element->lon ) ) sprintf ( lon , "%10.5f" , si->element->lon ) ; else strcpy ( lon , " " ) ; /* 10 spaces */ /* elev */ if ( isValidFloat ( dbl_LIST_SITE, dbl_SITE_ELEV , si->element->elev ) ) sprintf ( elev , "%7.3f" , si->element->elev ) ; else strcpy ( elev , " " ) ; /* 7 spaces */ /* ondate */ if ( isValidInt ( dbl_LIST_SITE, dbl_SITE_ONDATE , si->element->ondate ) ) { int doy, mm, id, iyyy , jdate = si->element->ondate ; iyyy = jdate / 1000 ; doy = jdate - ( iyyy * 1000 ) ; mnday ( doy , isleap ( iyyy ) , &mm , &id ) ; sprintf(ondate, "%04d/%02d/%02d" , iyyy, mm, id ); } else strcpy ( ondate , " " ) ; /* 10 spaces */ /* offdate */ if ( isValidInt ( dbl_LIST_SITE, dbl_SITE_OFFDATE , si->element->offdate ) ) { int doy, mm, id, iyyy , jdate = si->element->offdate ; iyyy = jdate / 1000 ; doy = jdate - ( iyyy * 1000 ) ; mnday ( doy , isleap ( iyyy ) , &mm , &id ) ; sprintf(offdate, "%04d/%02d/%02d" , iyyy, mm, id ); } else strcpy ( offdate , " " ) ; /* 10 spaces */ fprintf(ptr,"%s %s %s %s %s %s %s\n" , sta, statype, lat, lon, elev , ondate, offdate ); si = si->next; } fprintf(ptr,"\n"); printf("\n"); return 0 ; } /* end PutStationData */
int PutWaveformData ( FILE *ptr , DBlist tree , int cm6 ) { char id [ 5 ] , time [ 24 ] , station [ 6 ] , channel [ 4 ] , auxid [ 5 ] , datatype[ 4 ] , samps [ 9 ] , samprat [ 12 ] , calib [ 11 ] , calper [ 9 ] ; char instype[ ] = " ", hang [ 6 ] , vang [ 5 ] ; double dHang , dVang ; int Ntraces = 0 ; int dots = 0 ; int jdx ; int column ; int MaxColumns = 15 ; struct CSStree *Tree = (struct CSStree *) tree ; struct wfdiscList *wfL ; printf ( "Converting waveforms " ) ; for ( wfL = Tree->wfHead ; wfL ; wfL = wfL->next , Ntraces++ ) { int *iData ; struct sitechanList *sc = Tree->scHead ; /* display dots for the users sake */ putchar('.'); dots++; if(dots == 80){ putchar('\n'); dots = 0; } fflush(stdout); /* data_type record */ fprintf(ptr,"DATA_TYPE WAVEFORM\n"); /* BEGIN WID2 RECORD */ /* id */ strcpy ( id , "WID2" ) ; /* date and time */ if(isValidFloat( dbl_LIST_WFDISC, dbl_WFDIS_TIME, wfL->element->time )){ sprintf ( time , "%-23.23s" , tmListEpochTime ( wfL->element->time , 14 ) ) ; time[ 10 ] = ' ' ; } else strcpy ( time , " " ) ; /* 23 spaces */ /* station */ if ( isValidString ( dbl_LIST_WFDISC, dbl_WFDIS_STA , wfL->element->sta ) && strcmp( wfL->element->sta , "- " ) ) sprintf ( station , "%-5.5s" , wfL->element->sta ) ; else strcpy ( station , " " ) ; /* 5 spaces */ /* channel */ if ( isValidString ( dbl_LIST_WFDISC, dbl_WFDIS_CHAN , wfL->element->chan ) && strcmp( wfL->element->chan , "- " ) ) sprintf ( channel , "%-3.3s" , wfL->element->chan ) ; else strcpy ( channel , " " ) ; /* 3 spaces */ /* auxid */ strcpy ( auxid , " " ) ; /* 4 spaces */ /* datatype */ if( cm6 ) strcpy ( datatype , "CM6" ) ; /* 3 spaces */ else strcpy ( datatype , "INT" ) ; /* 3 spaces */ /* samps */ if ( isValidInt ( dbl_LIST_WFDISC, dbl_WFDIS_NSAMP , wfL->element->nsamp ) ) sprintf ( samps , "%8d" , wfL->element->nsamp ) ; else { printf ( "Warning: file %d had no value for samps\n" , Ntraces + 1 ) ; continue ; } /* samprat */ if ( isValidFloat ( dbl_LIST_WFDISC, dbl_WFDIS_SAMPRATE , wfL->element->samprate ) ) sprintf ( samprat , "%11.6f" , wfL->element->samprate ) ; else strcpy ( samprat , " " ) ;/* 11 spaces */ /* calib */ if ( isValidFloat ( dbl_LIST_WFDISC, dbl_WFDIS_CALIB , wfL->element->calib ) ) sprintf ( calib , "%10.2e" , wfL->element->calib ) ; else strcpy ( calib , " " ) ; /* 10 spaces */ /* calper */ if ( isValidFloat ( dbl_LIST_WFDISC, dbl_WFDIS_CALPER , wfL->element->calper ) ) sprintf ( calper , "%7.3f" , wfL->element->calper ) ; else sprintf ( calper , "%s", " " ) ; /* 8 spaces */ /* instype */ if ( isValidString ( dbl_LIST_WFDISC, dbl_WFDIS_INSTYPE , wfL->element->instype ) && strcmp( wfL->element->instype , "- " ) ) sprintf ( instype , "%-6.6s" , wfL->element->instype ) ; else strcpy ( instype , " " ) ; /* 6 spaces */ /* hang and vang */ GetChannelInfo(sc, station, channel, &dHang, &dVang); if ( isValidFloat ( dbl_LIST_SITECHAN, dbl_SITEC_HANG , dHang ) ) sprintf ( hang , "%5.1f" , dHang ) ; else strcpy ( hang , " " ) ; /* 5 spaces */ if ( isValidFloat ( dbl_LIST_SITECHAN, dbl_SITEC_VANG , dVang ) ) sprintf ( vang , "%4.1f" , dVang ) ; else strcpy ( vang , " " ) ; /* 4 spaces */ fprintf(ptr, "%s %s %s %s %s %s %s %s %s %s %s %s %s\n", id, time, station, channel, auxid, datatype, samps, samprat, calib, calper, instype, hang, vang); /* END WID2 RECORD */ /* dat2 record */ fprintf(ptr,"DAT2\n"); column = 0 ; if( !wfL->seis ) { printf ( "ERROR: Data not found!\n " ) ; return ( -1 ) ; } /* waveform */ iData = (int *) malloc ( wfL->element->nsamp * sizeof( int ) ) ; if ( !iData ) { printf ( "Error: insufficient memory, PutWavefomrData\n" ) ; return 0 ; } for ( jdx = 0 ; jdx < wfL->element->nsamp ; jdx++ ) { double point = wfL->seis->i[ jdx ] ; point = point >= 0 ? point + 0.5 : point - 0.5 ; iData[ jdx ] = (int) point ; if( !cm6 ){ /* INT format, one point at a time. */ fprintf ( ptr , "%d " , (int) point ) ; if ( ++column == MaxColumns ) { fprintf ( ptr , "\n" ) ; column = 0 ; } } } if( cm6 ){ char *cOut ; /* data compressed into CM6 format. */ takeDiff( iData , wfL->element->nsamp ) ; /* call cmprs6 to get string of chars */ if( cmprs6( wfL->element->nsamp , iData , &cOut ) ) { printf ( "Error: insufficient memory, PutWavefomrData\n" ) ; free( cOut ); return 0 ; } /* write it out (it already has newlines every 80 chars.) */ fprintf ( ptr , "%s", cOut ) ; fprintf ( ptr , "\n" ) ; free( cOut ) ; remdif1( iData , wfL->element->nsamp ) ; remdif1( iData , wfL->element->nsamp ) ; } else if ( column ) fprintf ( ptr , "\n" ) ; fprintf(ptr,"CHK2 %d\n\n", CheckSumFromIntArray(iData, wfL->element->nsamp) ); free ( iData ) ; } /* end for */ printf ( "\n" ) ; return Ntraces ; } /* end PutWaveformData */