/* Return the nb of samples of sacfiles at segm1/??/segm2 */ int get_length(char *segm1, char *segm2) { char *sac_GF; int npts; int fd; sac_GF = char_alloc(FSIZE); strncpy(sac_GF, segm1, FSIZE); strcat(sac_GF, "/RR/"); strcat(sac_GF, segm2); strcat(sac_GF, "Z.SAC"); fd = open(sac_GF, O_RDONLY, "r"); if(fd == -1) { fflush(stdout); fprintf(stderr, "\n*** ERROR (get_length): sac_GF file %s not accessible\n", sac_GF); fprintf(stderr, "*** ... Exiting the program ... ***\n"); fflush(stderr); exit(1); } lseek(fd, 316, SEEK_SET); read(fd, (void *)(&npts), sizeof(int)); close(fd); free((void *)sac_GF); return(npts); }
char *substring(char *name, int from, int to) { char *ret; ret = char_alloc(); name += from; strncpy(ret, name, to); return ret; }
void geterror(const char *format, ...) { char *error; va_list err; error = char_alloc(); va_start(err, format); vsprintf(error, format, err); va_end(err); perror(error); }
/* Count nb of lines in a file */ int count_lines(FILE *f) { char *line; int nl = 0; line = char_alloc(LSIZE); while( fgets(line,LSIZE,f) != NULL ) ++nl; rewind(f); free(line); return nl; }
void create_index_spec(index_spec_t* dest, int nridx, ...) { int i; va_list ap; va_start(ap,nridx); dest->ndims = nridx; dest->dim_size = size_alloc(0,nridx); dest->index = index_alloc(0,nridx); dest->index_type = char_alloc(0,nridx); for (i = 0; i < nridx; ++i) { dest->dim_size[i] = va_arg(ap, _index_t); dest->index[i] = va_arg(ap, _index_t*); dest->index_type[i] = (char) va_arg(ap,_index_t); /* char is cast to int by va_arg.*/ } va_end(ap); }
/* -> ext : extention */ char *get_gf_filename(char *dir, char *stnm, char *netwk, char *cmpnm, char *loc, char *ext) { int n,m ; char *sac_filename ; sac_filename = char_alloc(FSIZE); n = strlen(dir); m = nbchar(stnm); if (n!=0) { if (dir[n-1] != '/') { strcpy(sac_filename, dir); strcat(sac_filename, "/"); } else strcpy(sac_filename, dir); strncat(sac_filename, stnm,m); } else { strcpy(sac_filename, dir); strncat(sac_filename, stnm,m); } // If loc is empty (more precisely " "; it is set to "--") if ( strncmp(loc, " ", 2 ) == 0 ) strncpy(loc, "--", 2); strcat(sac_filename, "."); n = nbchar(netwk); strncat(sac_filename, netwk,n); strcat(sac_filename, "."); n = nbchar(cmpnm); strncat(sac_filename, cmpnm,n); strcat(sac_filename, "."); n = nbchar(loc); strncat(sac_filename, loc,2); strcat(sac_filename, "."); n = nbchar(ext); if (ext[0] == '.') m=1; else m=0; strncat(sac_filename, &ext[m],n); strcat(sac_filename, ""); return sac_filename ; }
/* save displacement and header in sac file */ void save_sac(char *stnm, char *netwk, char *chan, float *lat, float *lon, sachdr *hdr, double* depval) { int i,nbc; char *sac_filename; sac_filename = char_alloc(FSIZE); strcpy(sac_filename, stnm); strcat(sac_filename, "."); strcat(sac_filename, netwk); strcat(sac_filename, "."); strcat(sac_filename, chan); strcat(sac_filename, ".SAC"); nbc = strlen(stnm); strncpy(hdr->kstnm, stnm, nbc); for (i=nbc; i<8; i++) hdr->kstnm[i] = ' '; hdr->kstnm[8] = '\0'; nbc = strlen(netwk); strncpy(hdr->knetwk,netwk,nbc); for (i=nbc; i<8; i++) hdr->knetwk[i] = ' '; hdr->knetwk[8] = '\0'; nbc = strlen(chan); strncpy(hdr->kcmpnm,chan, nbc); for (i=nbc; i<8; i++) hdr->kcmpnm[i] = ' '; hdr->kcmpnm[8] = '\0'; hdr->stla = *lat; hdr->stlo = *lon; wsac(sac_filename, hdr, depval); free((void *)sac_filename); }
bool file_shm_handler<MAPPED_FILE>::initial_file_shm(std::vector<MAPPED_FILE *> *files_map) { //char allocator char_allocator char_alloc(file_shm->get_segment_manager()); //string allocator string_allocator str_alloc(file_shm->get_segment_manager()); //map allocator map_str_shm_allocator mapstr_shm_alloc(file_shm->get_segment_manager()); //Initial binary size support file memory. binary_string_shm *binarystr_shm[files_map->size()]; //initial file-shm if(shm_name.empty()) { logger->write_info("file_shm_handler<MAPPED_FILE>::initial_file_shm,Name not support shm."); return false; } map_str_shm_ptr = file_shm->construct<map_str_shm>(shm_name.c_str())(std::less<uint64_t>(), mapstr_shm_alloc); typename std::vector<MAPPED_FILE *>::iterator iter_files; for(iter_files = files_map->begin(); iter_files != files_map->end(); ++iter_files) { //index get from map size_t index_file = std::distance(files_map->begin(), iter_files); //file struct got from iterator MAPPED_FILE *mf = *iter_files; //MD5 variable mallocs supported mf->data converts from MD5Hash. //details_file_hex = (char*)malloc(sizeof(char) * mf->size); //printf("File name : %c ", mf->file_name); //If internal message is not value if(mf->msg_type == utils::internal_msg) { char *hex_ptr = utils::convert::byte2hexstr(mf->data, mf->size); addr_df_hex_vec.push_back(hex_ptr); logger->write_info("file_shm_handler<MAPPED_FILE>::initial_file_shm, Interanl message"); } else { //External message addr_df_hex_vec.push_back((char*)mf->data); logger->write_info("file_shm_handler<MAPPED_FILE>::initial_file_shm, External message"); } //get detail_file_hex address from vector to binary_string_shm //test: 7115022752065567031 binarystr_shm[index_file] = new binary_string_shm(char_alloc); *binarystr_shm[index_file] = addr_df_hex_vec[index_file]; //encode with MD5 with mf->file_name ( filename inculded path of file) //Plan-00004 : const uint64_t file_name_md5 = utils::convert::MD5Hash(mf->data, mf->size); boost::hash<char *> hash_file_name; const uint64_t file_name_md5 = hash_file_name(const_cast<char *>(mf->file_name.c_str())); file_name_md5_vec.push_back(file_name_md5); //Key : MD5 from detail insides file. //Value : insert vector contains detail of hex of file to value. /*map_shm_ptr->insert(std::pair<const uint64_t, binary_string_shm_vec>( file_name_md5, *binarystr_shm_vec[index_file])); */ //support, value type is char //Key : MD5 from detail insides file. //Value : insert vector contains detail of hex of file to value. map_str_shm_ptr->insert(std::pair<const uint64_t, binary_string_shm>( file_name_md5, *binarystr_shm[index_file])); //insert filename and file size for calculate map_file_size.insert(std::make_pair(file_name_md5, mf->size)); } }
int read_snap_graph(const char * const filename, vtx_t * const r_nvtxs, adj_t ** const r_xadj, vtx_t ** const r_adjncy, wgt_t ** const r_vwgt, wgt_t ** const r_adjwgt) { file_t * file; int rv, do_adjwgt; vtx_t nvtxs, i, j, u, v, maxv; adj_t nedges; ssize_t ll; size_t bufsize; char * line = NULL,*eptr, * sptr; wgt_t w; vtx_t * labels = NULL; adj_t * xadj = NULL; vtx_t * adjncy = NULL; wgt_t * adjwgt = NULL; bufsize = BUFFERSIZE; if ((rv = __open_file(filename,"r",&file)) != BOWSTRING_SUCCESS) { goto ERROR; } line = char_alloc(bufsize); do_adjwgt = 0; nedges = 0; /* first pass to count edges and vertices */ maxv = 0; while((ll = dl_get_next_line(file,&line,&bufsize)) >= 0) { if (ll == 0) { /* ignore blank lines */ continue; } else { if (COMMENT_CHARS[(unsigned int)line[0]]) { /* skip comments */ continue; } } sptr = line; i = (vtx_t)strtoull(sptr,&eptr,10); sptr = eptr; j = (vtx_t)strtoull(sptr,&eptr,10); sptr = eptr; w = (wgt_t)strtod(sptr,&eptr); if (eptr != sptr) { /* if anyone is missing weight, we'll assign 1 */ printf("Parsing edge weight from SNAP graph\n"); do_adjwgt = 1; } if (i > maxv) { maxv = i; } if (j > maxv) { maxv = j; } /* count edges in both directions */ nedges+=2; } nvtxs = maxv+1; labels = vtx_init_alloc(NULL_VTX,nvtxs); xadj = adj_calloc(nvtxs+1); adjncy = vtx_alloc(nedges); if (do_adjwgt) { adjwgt = wgt_alloc(nedges); } dl_reset_file(file); /* second pass to populate xadj */ nvtxs = 0; while((ll = dl_get_next_line(file,&line,&bufsize)) >= 0) { if (ll == 0) { /* ignore blank lines */ continue; } else { if (COMMENT_CHARS[(unsigned int)line[0]]) { /* skip comments */ continue; } } sptr = line; i = strtoull(sptr,&eptr,10); if (labels[i] == NULL_VTX) { u = labels[i] = nvtxs++; } else { u = labels[i]; } sptr = eptr; j = strtoull(sptr,&eptr,10); if (labels[j] == NULL_VTX) { v = labels[j] = nvtxs++; } else { v = labels[j]; } ++xadj[u+1]; ++xadj[v+1]; } xadj[0] = 0; adj_prefixsum_exc(xadj+1,nvtxs); dl_reset_file(file); DL_ASSERT(xadj[1] == 0,"Broken xadj for loading\n"); /* final pass */ w = 0; /* stops uninitialized complaints */ while((ll = dl_get_next_line(file,&line,&bufsize)) >= 0) { if (ll == 0) { /* ignore blank lines */ continue; } else { if (COMMENT_CHARS[(unsigned int)line[0]]) { /* skip comments */ continue; } } sptr = line; u = labels[strtoull(sptr,&eptr,10)]; sptr = eptr; v = labels[strtoull(sptr,&eptr,10)]; sptr = eptr; if (do_adjwgt) { w = (wgt_t)strtod(sptr,&eptr); if (eptr == sptr) { /* if anyone is missing weight, we'll assign 1 */ w = 1; } } adjncy[xadj[u+1]] = v; adjncy[xadj[v+1]] = u; if (do_adjwgt) { adjwgt[xadj[u+1]] = w; adjwgt[xadj[v+1]] = w; } ++xadj[u+1]; ++xadj[v+1]; } dl_free(labels); dl_free(line); dl_close_file(file); /* sort the edges */ if (do_adjwgt) { for (i=0;i<nvtxs;++i) { __quicksort_edges(adjncy+xadj[i],adjwgt+xadj[i],xadj[i+1]-xadj[i]); } } else { for (i=0;i<nvtxs;++i) { vtx_quicksort(adjncy+xadj[i],xadj[i+1]-xadj[i]); } } /* remove duplicates */ nedges = 0; for (i=0;i<nvtxs;++i) { j=xadj[i]; xadj[i] = nedges; if (j<xadj[i+1]) { adjncy[nedges] = adjncy[j]; if (do_adjwgt) { adjwgt[nedges] = adjwgt[j]; } ++nedges; ++j; } for (;j<xadj[i+1];++j) { if (adjncy[j] != adjncy[j-1]) { adjncy[nedges] = adjncy[j]; if (do_adjwgt) { adjwgt[nedges] = adjwgt[j]; } ++nedges; } } } xadj[nvtxs] = nedges; if (r_nvtxs) { *r_nvtxs = nvtxs; } if (r_xadj) { *r_xadj = xadj; } else if (xadj) { dl_free(xadj); } if (r_adjncy) { *r_adjncy = adjncy; } else if (adjncy) { dl_free(adjncy); } /* doesn't support vwgt */ if (r_vwgt) { *r_vwgt = NULL; } if (r_adjwgt) { *r_adjwgt = adjwgt; } else if (adjwgt) { dl_free(adjwgt); } return BOWSTRING_SUCCESS; ERROR: if (line) { dl_free(line); } if (labels) { dl_free(labels); } if (xadj) { dl_free(xadj); } if (adjncy) { dl_free(adjncy); } if (adjwgt) { dl_free(adjwgt); } return rv; }
int read_stp_graph( char const * const filename, vtx_t * const r_nvtxs, adj_t ** const r_xadj, vtx_t ** const r_adjncy, wgt_t ** const r_vwgt, wgt_t ** const r_adjwgt) { int rv; vtx_t i, k, nvtxs; adj_t nedges, j, l; wgt_t w; ssize_t ll; char * line = NULL; size_t bufsize = BUFFERSIZE; file_t * ifile = NULL; vtx_t * x = NULL; vtx_t * y = NULL; wgt_t * z = NULL; adj_t * xadj = NULL; vtx_t * adjncy = NULL; wgt_t * adjwgt = NULL; wgt_t * vwgt = NULL; if ((rv = __open_file(filename,"r",&ifile)) != BOWSTRING_SUCCESS) { goto END; } line = char_alloc(bufsize); /* find the graph section */ if (!__find_section(ifile,"GRAPH",&bufsize)) { eprintf("Could not find graph section\n"); rv = BOWSTRING_ERROR_INVALIDINPUT; goto END; } /* read the number of vertices/nodes */ if ((ll = dl_get_next_line(ifile,&line,&bufsize)) < 0 || sscanf(line,"Nodes%*[ \t]"RF_VTX_T,&nvtxs) != 1) { eprintf("Could not read number of nodes from graph section\n"); rv = BOWSTRING_ERROR_INVALIDINPUT; goto END; } /* read the number of edges */ if ((ll = dl_get_next_line(ifile,&line,&bufsize)) < 0 || sscanf(line,"Edges%*[ \t]"RF_ADJ_T,&nedges) != 1) { eprintf("Could not read number of edges from graph section\n"); rv = BOWSTRING_ERROR_INVALIDINPUT; goto END; } nedges *=2; /* allocate graph */ xadj = adj_calloc(nvtxs+1); adjncy = vtx_alloc(nedges); adjwgt = wgt_alloc(nedges); /* allocate graph buffers */ x = vtx_alloc(nedges/2); y = vtx_alloc(nedges/2); z = wgt_alloc(nedges/2); /* read in edges */ for (j=0;j<nedges/2;++j) { while ((ll = dl_get_next_line(ifile,&line,&bufsize)) == 0) { /* blank line */ } if (ll < 0) { eprintf("Could not find edge #"PF_ADJ_T": '%s'\n",j,line); rv = BOWSTRING_ERROR_INVALIDINPUT; goto END; } else { if (dl_strncmp_nocase(line,"END",3) == 0) { wprintf("Only half the number of edges listed :"PF_ADJ_T"/"PF_ADJ_T \ ".\n",j,nedges/2); nedges = j*2; goto NODEWEIGHTS; } else if (line[0] == 'E') { if (sscanf(line,"E%*[ \t]"RF_VTX_T"%*[ \t]"RF_VTX_T"%*[ \t]"RF_WGT_T, \ &i,&k,&w) != 3) { eprintf("Could not read edge #"PF_ADJ_T": '%s'\n",j,line); rv = BOWSTRING_ERROR_INVALIDINPUT; goto END; } } else if (line[0] == 'A') { /* handle broken files with arcs listed instead of edges */ if (sscanf(line,"A%*[ \t]"RF_VTX_T"%*[ \t]"RF_VTX_T"%*[ \t]"RF_WGT_T, \ &i,&k,&w) != 3) { eprintf("Could not read arc #"PF_ADJ_T": '%s'\n",j,line); rv = BOWSTRING_ERROR_INVALIDINPUT; goto END; } } else { eprintf("Bad label for edge #"PF_ADJ_T": '%s'\n",j,line); rv = BOWSTRING_ERROR_INVALIDINPUT; goto END; } ++xadj[i]; ++xadj[k]; --i; --k; x[j] = i; y[j] = k; z[j] = w; } } /* skip blank lines */ while ((ll = dl_get_next_line(ifile,&line,&bufsize)) == 0) { /* blank line */ } /* check the end of the edge list */ if (ll < 0 || dl_strncmp_nocase(line,"END",3) != 0) { eprintf("Invalid line at end of section: '%s'\n",line); rv = BOWSTRING_ERROR_INVALIDINPUT; goto END; } NODEWEIGHTS: /* find the node weight section if it exists */ if (r_vwgt && __find_section(ifile,"NODEWEIGHTS",&bufsize)) { vwgt = wgt_alloc(nvtxs); for (i=0;i<nvtxs;++i) { while ((ll = dl_get_next_line(ifile,&line,&bufsize)) == 0) { /* blank line */ } if (ll < 0 || sscanf(line,"NW%*[ \t]"RF_WGT_T,&w) != 1) { eprintf("Could not read vertex weight for vertex "PF_VTX_T"\n",i); rv = BOWSTRING_ERROR_INVALIDINPUT; goto END; } else { vwgt[i] = w; } } /* skip blank lines */ while ((ll = dl_get_next_line(ifile,&line,&bufsize)) == 0) { /* blank line */ } /* check the end of the vertex weight list */ if (ll < 0 || dl_strncmp_nocase(line,"END",3) != 0) { eprintf("Invalid line at end of section: '%s'\n",line); rv = BOWSTRING_ERROR_INVALIDINPUT; goto END; } } /* close the file */ dl_close_file(ifile); ifile = NULL; /* build edges */ adj_prefixsum_exc(xadj+1,nvtxs); for (j=0;j<nedges/2;++j) { i = x[j]; k = y[j]; w = z[j]; /* vertex a */ l = xadj[i+1]++; adjncy[l] = k; adjwgt[l] = w; /* vertex b */ l = xadj[k+1]++; adjncy[l] = i; adjwgt[l] = w; } *r_nvtxs = nvtxs; *r_xadj = xadj; xadj = NULL; *r_adjncy = adjncy; adjncy = NULL; if (r_adjwgt) { *r_adjwgt = adjwgt; } else { dl_free(adjwgt); } adjwgt = NULL; if (r_vwgt) { *r_vwgt = vwgt; } vwgt = NULL; END: if (line) { dl_free(line); } if (ifile) { dl_close_file(ifile); } if (xadj) { dl_free(xadj); } if (adjncy) { dl_free(adjncy); } if (adjwgt) { dl_free(adjwgt); } if (vwgt) { dl_free(vwgt); } if (x) { dl_free(x); } if (y) { dl_free(y); } if (z) { dl_free(z); } return rv; }