#include "isf_bul.h" /* To check whether the current line type is of an ISF type consistent with the type of the previous line written. The exception is line_type 'data_type' where previous line type is assumed to be undefined. Checks and resets the global variable isf_prev_line_type. Returns 0 if the current line type is expected to follow the previous one. Returns 1 if this line type should not follow the previous line type. */ int check_prev_line_type(char *line_type) { char **allowed; int i,n; allowed = (char **)calloc(20,ISF_LINE_LEN); i=0; if (strcmp(line_type,"data_type") == 0){ strcpy(isf_prev_line_type,line_type); return 0; } else if (strcmp(line_type,"event_id") == 0){ allowed[i++] = "data_type"; allowed[i++] = "phase"; allowed[i++] = "phase_com"; allowed[i++] = "phase_info"; } else if (strcmp(line_type,"origin_head") == 0){ allowed[i++] = "event_id"; } else if (strcmp(line_type,"origin") == 0){ allowed[i++] = "origin_head"; allowed[i++] = "origin"; allowed[i++] = "origin_com"; } else if (strcmp(line_type,"origin_com") == 0){ allowed[i++] = "origin"; allowed[i++] = "origin_com"; allowed[i++] = "axes"; allowed[i++] = "axes_err"; allowed[i++] = "fault_plane"; allowed[i++] = "momten"; allowed[i++] = "netmag"; } else if (strcmp(line_type,"momten_head") == 0){ allowed[i++] = "origin"; allowed[i++] = "origin_com"; allowed[i++] = "axes"; allowed[i++] = "axes_err"; allowed[i++] = "fault_plane"; } else if (strcmp(line_type,"momten") == 0){ allowed[i++] = "momten"; allowed[i++] = "momten_head"; allowed[i++] = "origin_com"; } else if (strcmp(line_type,"fault_plane_head") == 0){ allowed[i++] = "origin"; allowed[i++] = "origin_com"; allowed[i++] = "axes"; allowed[i++] = "axes_err"; allowed[i++] = "momten"; } else if (strcmp(line_type,"fault_plane") == 0){ allowed[i++] = "fault_plane_head"; allowed[i++] = "fault_plane"; allowed[i++] = "origin_com"; } else if (strcmp(line_type,"axes_head") == 0){ allowed[i++] = "origin"; allowed[i++] = "origin_com"; allowed[i++] = "fault_plane"; allowed[i++] = "momten"; } else if (strcmp(line_type,"axes_err_head") == 0){ allowed[i++] = "axes_head"; } else if (strcmp(line_type,"axes") == 0){ allowed[i++] = "axes_head"; allowed[i++] = "axes_err_head"; allowed[i++] = "origin_com"; } else if (strcmp(line_type,"axes_err") == 0){ allowed[i++] = "axes"; } else if (strcmp(line_type,"netmag_head") == 0){ allowed[i++] = "origin"; allowed[i++] = "origin_com"; allowed[i++] = "momten"; allowed[i++] = "axes"; allowed[i++] = "axes_err"; allowed[i++] = "fault_plane"; } else if (strcmp(line_type,"netmag") == 0){ allowed[i++] = "netmag_head"; allowed[i++] = "netmag"; allowed[i++] = "netmag_com"; } else if (strcmp(line_type,"netmag_com") == 0){ allowed[i++] = "netmag"; allowed[i++] = "netmag_com"; } else if (strcmp(line_type,"effects_head") == 0){ allowed[i++] = "origin"; allowed[i++] = "origin_com"; allowed[i++] = "momten"; allowed[i++] = "axes"; allowed[i++] = "axes_err"; allowed[i++] = "fault_plane"; allowed[i++] = "netmag"; allowed[i++] = "netmag_com"; } else if (strcmp(line_type,"effects") == 0){ allowed[i++] = "effects_head"; allowed[i++] = "effects"; allowed[i++] = "effects_com"; } else if (strcmp(line_type,"effects_com") == 0){ allowed[i++] = "effects"; allowed[i++] = "effects_com"; } else if (strcmp(line_type,"phase_head") == 0){ allowed[i++] = "origin"; allowed[i++] = "origin_com"; allowed[i++] = "momten"; allowed[i++] = "axes"; allowed[i++] = "axes_err"; allowed[i++] = "fault_plane"; allowed[i++] = "netmag"; allowed[i++] = "netmag_com"; allowed[i++] = "effects"; allowed[i++] = "effects_com"; } else if (strcmp(line_type,"phase_origid") == 0){ allowed[i++] = "phase_head"; allowed[i++] = "phase_info_head"; } else if (strcmp(line_type,"phase") == 0){ allowed[i++] = "phase_origid"; allowed[i++] = "phase_head"; allowed[i++] = "phase"; allowed[i++] = "phase_com"; } else if (strcmp(line_type,"phase_com") == 0){ allowed[i++] = "phase"; allowed[i++] = "phase_com"; } else if (strcmp(line_type,"phase_info_head") == 0){ allowed[i++] = "phase"; allowed[i++] = "phase_com"; } else if (strcmp(line_type,"phase_info") == 0){ allowed[i++] = "phase_origid"; allowed[i++] = "phase_info"; allowed[i++] = "phase_info_com"; allowed[i++] = "phase_info_head"; } else if (strcmp(line_type,"phase_info_com") == 0){ allowed[i++] = "phase_info"; allowed[i++] = "phase_info_com"; } else if (strcmp(line_type,"stop") == 0){ allowed[i++] = "phase"; allowed[i++] = "phase_com"; allowed[i++] = "phase_info"; allowed[i++] = "phase_info_com"; } n=i; for (i=0; i 99.99 but 999.9999 => 999.9. */ void print_float(FILE *fp, float x, int width, int max_prec) { int prec,spare; if (x<0) { spare = width - 2.5 - (int)log10(fabs(x)); } else if (x>0) { spare = width - 1.5 - (int)log10(fabs(x)); } else { spare = max_prec; } if (spare > max_prec){ prec = max_prec; } else if (spare < 0){ prec = 0; } else { prec = spare; } fprintf (fp,"%*.*f",width,prec,x); return; } /* Get a substring, removing leading and trailing white space. Expects a string, an offset from the start of the string, and a maximum length for the resulting substring. If this length is 0 it will take up to the end of the input string. Need to allow for ')' to come after the required field at the end of a comment. Discard ')' at end of a string as long as there's no '(' before it anywhere. Returns the length of the resulting substring. */ int partline(char *part, char *line, int offset, int numchars) { int i,j,len; int bracket=0; len = strlen(line); if (len < offset) return 0; if (numchars == 0) numchars = len-offset; for (i=offset, j=0; i < offset+numchars; i++){ if ( j == 0 && (line[i] == ' ' || line[i] == '\t')) continue; if ( line[i] == '\0' || line[i] == '\n') break; part[j++] = line[i]; if ( line[i] == '(' ) bracket=1; } if (!bracket){ while (--j != -1 && (part[j]==' ' || part[j]=='\t' || part[j]==')' )); part[++j]='\0'; } else if (j){ while (part[--j] == ' ' || part[j] == '\t'); part[++j]='\0'; } return j; } /* To check that a string has no spaces in it. Returns 0 if there are no spaces or 1 if there is a space. */ int check_whole(char *substr) { int i; for (i=0; i < strlen(substr); i++){ if (substr[i] == ' ' || substr[i] == '\t') return 1; } return 0; } /* Check if a string is composed entirely of white space or not. Returns 1 if it is, 0 if it isn't. */ int all_blank(char *s){ int i; for ( i=0; s[i] == ' ' || s[i] == '\t'; i++); if (s[i] == '\n' || s[i] == '\0') return 1; return 0; } /* Check whether a float or int is null or not. Returns 1 if it is, 0 if it isn't. */ int is_null(int i){ if (i == ISF_NULL){ return 1; } return 0; } /* To check that a string contains only sign/number characters and so is suitable for atoi - atoi itself does no checking. Returns 0 if OK, 1 if not. */ int check_int(char *substr) { int i; for (i=0; i < strlen(substr); i++){ if (isdigit(substr[i])) continue; if (i==0) if (substr[i] == '+' || substr[i] == '-') continue; return 1; } return 0; } /* To check that a string contains only sign/number/point characters and so is suitable for atof - atof itself does no checking. Returns 0 if OK, 1 if not. */ int check_float(char *substr) { int i; for (i=0; i < strlen(substr); i++){ if (isdigit(substr[i]) || substr[i] == '.') continue; if (i==0) if (substr[i] == '+' || substr[i] == '-') continue; return 1; } return 0; }