/* Returns NULL on failure */ hash_t* SMBCALL smb_hashstr(ulong msgnum, uint32_t t, unsigned source, unsigned flags ,const char* str) { char* p=NULL; hash_t* hash; if(flags&SMB_HASH_PROC_MASK) { /* string pre-processing */ if((p=strdup(str))==NULL) return(NULL); if(flags&SMB_HASH_STRIP_CTRL_A) strip_ctrla((uchar *)p,(uchar *)p); if(flags&SMB_HASH_STRIP_WSP) strip_chars((uchar *)p,(uchar *)p,(uchar *)" \t\r\n"); if(flags&SMB_HASH_LOWERCASE) strlwr(p); } if(p!=NULL) { hash=smb_hash(msgnum, t, source, flags, p, strlen(p)); free(p); } else hash=smb_hash(msgnum, t, source, flags, str, strlen(str)); return(hash); }
/* Returns NULL on failure */ hash_t* SMBCALL smb_hashstr(ulong msgnum, ulong t, unsigned source, unsigned flags ,const char* str) { char* p=(char *)str; hash_t* hash; if(flags&SMB_HASH_PROC_MASK) { /* string pre-processing */ if((p=strdup(str))==NULL) return(NULL); if(flags&SMB_HASH_STRIP_WSP) strip_chars(p,str," \t\r\n"); if(flags&SMB_HASH_LOWERCASE) strlwr(p); } hash=smb_hash(msgnum, t, source, flags, p, strlen(p)); if(p!=str) /* duped string */ free(p); return(hash); }
/***********************************************************************//** * @brief Load CSV table * * @param[in] filename Filename. * @param[in] sep Column separator (default is whitespace). * * @exception GException::file_not_found * CSV table file not found. * @exception GException::csv_bad_columns * Inconsistent columns encountered in CSV table file. * * Load CSV table from ASCII file. * Any environment variable present in the filename will be expanded. **************************************************************************/ void GCsv::load(const std::string& filename, std::string sep) { // Clear instance clear(); // If no seperator is given then assume a whitespace if (sep.length() == 0) { sep = " "; } // Allocate line buffer const int n = 10000; char line[n]; // Expand environment variables std::string fname = expand_env(filename); // Open CSV table (read-only FILE* fptr = std::fopen(fname.c_str(), "r"); if (fptr == NULL) { throw GException::file_not_found(G_LOAD, fname); } // Read lines int iline = 0; while (std::fgets(line, n, fptr) != NULL) { // Increment line counter iline++; // Get line with leading and trailing whitespace removed std::string sline = strip_chars(strip_whitespace(std::string(line)),"\n"); // Skip line if empty if (sline.length() == 0) continue; // Split line in elements std::vector<std::string> elements = split(sline, sep); for (int i = 0; i < elements.size(); ++i) { elements[i] = strip_whitespace(elements[i]); } // If this is the first valid line then simply store the elements if (m_data.empty()) { m_data.push_back(elements); m_cols = elements.size(); } // ... otherwise check table consistency and add elements else { // Check table consistency if (m_cols != elements.size()) { throw GException::csv_bad_columns(G_LOAD, fname, iline, m_cols, elements.size()); } // Append elements m_data.push_back(elements); } // Increment number of rows m_rows++; } // endwhile: looped over lines // Close file std::fclose(fptr); // Return return; }
/***********************************************************************//** * @brief Open Table * * @param[in] vptr FITS file pointer. * * @exception GException::fits_hdu_not_found * Specified HDU not found in FITS file. * @exception GException::fits_error * A CFITSIO error occured during loading the table. * @exception GException::fits_unknown_coltype * FITS column of unsupported type has been found in the FITS file. * @exception GException::fits_inconsistent_tdim * The TDIM information provided in the header is inconsistent * with the size of the column. * * This method loads a description of the table into memory. Column data are * not loaded at this point to save memory. They will be loaded on request * later. ***************************************************************************/ void GFitsTable::data_open(void* vptr) { // Move to HDU int status = 0; status = __ffmahd(FPTR(vptr), (FPTR(vptr)->HDUposition)+1, NULL, &status); if (status != 0) throw GException::fits_hdu_not_found(G_DATA_OPEN, (FPTR(vptr)->HDUposition)+1, status); // Save FITS file pointer FPTR_COPY(m_fitsfile, vptr); // Determine number of rows in table long nrows = 0; status = __ffgnrw(FPTR(m_fitsfile), &nrows, &status); if (status != 0) throw GException::fits_error(G_DATA_OPEN, status); else m_rows = (int)nrows; // Determine number of columns in table status = __ffgncl(FPTR(m_fitsfile), &m_cols, &status); if (status != 0) throw GException::fits_error(G_DATA_OPEN, status); // Allocate and initialise memory for column pointers. Note that this // initialisation is needed to allow for a clean free_members() call // in case of any exception. if (m_columns != NULL) delete [] m_columns; m_columns = new GFitsTableCol*[m_cols]; for (int i = 0; i < m_cols; ++i) m_columns[i] = NULL; // Get table column information int typecode = 0; long repeat = 0; long width = 0; for (int i = 0; i < m_cols; ++i) { // Get column name char keyname[10]; char value[80]; sprintf(keyname, "TTYPE%d", i+1); status = __ffgkey(FPTR(m_fitsfile), keyname, value, NULL, &status); if (status != 0) throw GException::fits_error(G_DATA_OPEN, status); value[strlen(value)-1] = '\0'; // Get column definition status = __ffgtcl(FPTR(m_fitsfile), i+1, &typecode, &repeat, &width, &status); if (status != 0) throw GException::fits_error(G_DATA_OPEN, status); // Check for unsigned columns unsigned long offset = 0; sprintf(keyname, "TZERO%d", i+1); status = __ffgky(FPTR(m_fitsfile), __TULONG, keyname, &offset, NULL, &status); if (status == 0) { if (typecode == __TSHORT && offset == 32768u) typecode = __TUSHORT; else if (typecode == __TLONG && offset == 2147483648u) typecode = __TULONG; else if (typecode == __TINT && offset == 2147483648u) typecode = __TUINT; else { std::ostringstream message; message << ", but column " << value << " has typecode " << typecode << " and unexpected associated TZERO=" << offset << "."; throw GException::fits_error(G_DATA_OPEN, 0, message.str()); } } else status = 0; // Get column unit (optional, leave blank if not found) char unit[80]; sprintf(keyname, "TUNIT%d", i+1); status = __ffgkey(FPTR(m_fitsfile), keyname, unit, NULL, &status); if (status != 0) { status = 0; unit[0] = '\0'; unit[1] = '\0'; } else unit[strlen(unit)-1] = '\0'; // Get column dimension (optional, leave blank if not found) char dim[80]; sprintf(keyname, "TDIM%d", i+1); status = __ffgkey(FPTR(m_fitsfile), keyname, dim, NULL, &status); if (status != 0) { status = 0; dim[0] = '\0'; dim[1] = '\0'; } else { dim[strlen(dim)-1] = '\0'; } // If found, extract column dimension into vector array of integers std::vector<int> vdim; std::string sdim = strip_chars(strip_whitespace(&(dim[1])),"()"); if (sdim.length() > 0) { std::vector<std::string> elements = split(sdim, ","); for (int k = 0; k < elements.size(); ++k) { vdim.push_back(toint(elements[k])); } } // Allocate column m_columns[i] = alloc_column(typecode); if (m_columns[i] == NULL) { std::ostringstream colname; colname << value; throw GException::fits_unknown_coltype(G_DATA_OPEN, colname.str(), typecode); } // Store column definition m_columns[i]->name(strip_whitespace(&(value[1]))); m_columns[i]->unit(strip_whitespace(&(unit[1]))); m_columns[i]->dim(vdim); m_columns[i]->m_colnum = i+1; m_columns[i]->m_type = typecode; m_columns[i]->m_repeat = repeat; m_columns[i]->m_width = width; m_columns[i]->m_length = m_rows; m_columns[i]->connect(FPTR(m_fitsfile)); // Extract column vector size if (m_columns[i]->m_repeat == 1) { // ASCII tables m_columns[i]->m_number = 1; } else { // Binary tables if (typecode == __TSTRING) { m_columns[i]->m_number = m_columns[i]->m_repeat / m_columns[i]->m_width; } else { m_columns[i]->m_number = m_columns[i]->m_repeat; } } // If TDIM information was set then check its consistency if (!vdim.empty()) { // Compute expectation int num = vdim[0]; for (int k = 1; k < vdim.size(); ++k) { num *= vdim[k]; } // Compare with real size if (num != m_columns[i]->m_number) { throw GException::fits_inconsistent_tdim(G_DATA_OPEN, vdim, m_columns[i]->m_number); } } // endif: Valid TDIM information was found } // endfor: looped over all columns // Return return; }