📄 flags.c
字号:
/* * Copyright (c) 2001-2003 Falk Feddersen * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * *//* --- flags.c ---------- */#include <stdlib.h>#include <string.h>#include "flags.h"#include "output.h"#include "input.h"#include "floats.h"#include "breaking.h"//#define DEBUG/* ----------------------------------------------------------------------- *//* First group of functions allocates and destroys the outputflags memory *//* ----------------------------------------------------------------------- */oflags* oflags_new(){ oflags *F; F = (oflags * ) g_malloc(sizeof(oflags)); F->point_list = NULL; F->cross_list = NULL; F->along_list = NULL; F->snapshot_list = NULL; F->FO = NULL; return F;}void output_options_destroy(output_options_t *OP){ if (OP != NULL) { if (OP->fp != NULL) fclose(OP->fp); if (OP->fname != NULL) g_string_free(OP->fname,TRUE); g_free(OP); }}void point_destroy(point *P){ if (P!=NULL) { output_options_destroy(P->OP); g_free(P); }}void along_line_destroy(along_line *P){ if (P!=NULL) { output_options_destroy(P->OP); g_free(P); }}void cross_line_destroy(cross_line *P){ if (P!=NULL) { output_options_destroy(P->OP); g_free(P); }}void snapshot_destroy(snapshot_output *P){ if (P!=NULL) { output_options_destroy(P->OP); g_free(P); }}void oflags_destroy(oflags *F){ GSList *list; if (F!=NULL) { for (list=F->point_list;list;list=list->next) point_destroy(list->data); for (list=F->cross_list;list;list=list->next) cross_line_destroy(list->data); for (list=F->along_list;list;list=list->next) along_line_destroy(list->data); for (list=F->snapshot_list;list;list=list->next) snapshot_destroy(list->data); g_slist_free(F->point_list); g_slist_free(F->cross_list); g_slist_free(F->along_list); g_slist_free(F->snapshot_list); }}void outputflags_destroy(outputflags *A){ if (A != NULL) { oflags_destroy(A->F1); oflags_destroy(A->F2); oflags_destroy(A->F3); }}/* ------------------------------------------------------------- *//* --- Next group of functions does the low level parsing stuff *//* ------------------------------------------------------------- *//* ---- functions for checking if things are in bounds ---- */int check_yloc(int a){ if ((a<0)||(a>=ny)) funwaveC_error_message("Syntax Error in init file: Bad alongshore index!\n"); return a;}int check_xloc(int a, variable_t type){ int max; switch(type) { case U_M: max=nx-1; break; case V_M: max=nx; break; case ETA_M: max=nx-2; break; default: funwaveC_error_message("Error in type of tindex!\n"); exit(-1); } if ((a<0)||(a>max)) funwaveC_error_message("Syntax Error in init file:\nBad cross_shore index!\n"); return a;}double point_var(variable_t var, int i, int j){ switch (var) { case U_M: return DR2(UU,i,j); case V_M: return DR2(VV,i,j); case ETA_M: return DR2(ETA,i,j); case TRACER_M: return DR2(QQ,i,j); default: funwaveC_error_message("unknown switch in output_new.c:point_var()"); } return -1.0; // This should never happen!}variable_t variable_t_index(char *a){ if (!strcasecmp(a,"taubx")) { return TAUBX_M; } if (!strcasecmp(a,"tauby")) { return TAUBY_M; } if (!strcasecmp(a,"tracer")) { return TRACER_M; } if (!strcasecmp(a,"vorticity")) { return VORTICITY_M; } if (!strcasecmp(a,"vort")) { return VORTICITY_M; } if (!strcasecmp(a,"eta")) { return ETA_M; } if (!strcasecmp(a,"eddy")) { return EDDY_BR_M; } if (!strcasecmp(a,"depth")) { return DEPTH_M; } switch(a[0]) { case 'U': case 'u': return U_M; case 'V': case 'v': return V_M; case 'N': case 'n': return ETA_M; case 'K': case 'k': return KE_M; case 'w': case 'W': case 'q': case 'Q': return VORTICITY_M; // case 'S': // case 's': return STREAMF_M; default: funwaveC_error_message("Syntax Error: Bad option to variable_t_index()!"); } return U_M; // this should never happen!}output_t output_t_index(char *s){ output_t tt; if (!strcasecmp(s,"file")) tt = FILE_M; else { if (!strcasecmp(s,"term")) tt = TERM_M; else if (!strcasecmp(s,"gui")) tt = GUI_M; else funwaveC_error_message("Syntax Error: Bad output option in init file."); } return tt; }file_output_t ascii_or_binary_new(char *s){ file_output_t tt; if (s==NULL) { fprintf(stderr,"**** Using default ASCII file_output_t ***\n"); tt = ASCII_M; } if (!strcasecmp(s,"binary")) tt = BINARY_M; else { if (!strcasecmp(s,"ascii")) tt = ASCII_M; else funwaveC_error_message("Syntax Error: Bad binary/ascii option to init file."); } return tt;}/* ----------------------------------------------------------------------- *//* Parses the display message for term output *//* ----------------------------------------------------------------------- */GString* parse_output_message(char *rest){ GString *M; int N,i,qflag,num=0; char tmp[120]; /* first get string "rest" length, then check for "" */ N = strlen(rest); if (rest[0]!='"') funwaveC_error_message("flags.c:parse_output_message - Bad string to terminal output"); /* now look for 2nd " */ qflag = 0; for (i=1;i<N-1;i++) if (rest[i]=='"') { qflag=1; num = i-1; } if (qflag == 0) funwaveC_error_message("flags.c:parse_output_message - Bad string to terminal output"); strncpy(tmp,&(rest[1]),num); tmp[num] = '\0'; /* get a new GString */ M = g_string_new(tmp); return M;}output_options_t* new_output_options(){ output_options_t *T; T = (output_options_t * ) g_malloc(sizeof(output_options_t)); T->fp = NULL; T->fname = NULL; T->message = NULL; return T;}/* ----------------------------------------------------------------------- *//* Parses the common output options for the oflag type *//* ----------------------------------------------------------------------- */output_options_t* parse_output_options(char *s){ output_options_t *T; char filet[32],abt[32],fname[32],*rest; char start_time[32], tunits[32]; int num, st, un1; T = new_output_options(); num = sscanf(s,"%s %s %s %s %s", filet, abt, fname, start_time, tunits); if (num==0) funwaveC_error_message("Syntax Error: No Output options given!"); T->OT = output_t_index(filet); if (T->OT ==TERM_M) { T->fp = stdout; if (num!=1) { rest = strstr(s,abt); T->message = parse_output_message(rest); } } if (T->OT == FILE_M) { if ((num<3)||(num>5)) funwaveC_error_message("Syntax Error: Not enough file output options???"); T->FOT = ascii_or_binary_new(abt); T->fname = g_string_new(fname); // T->fp = open_file_for_writing((T->fname)->str); /* This handles when the model starts writing data */ if (num==4) { st = atoi(start_time); if (st<0) funwaveC_error_message("Bad option to atoi()\n"); } else { if (num==5) { st = atoi(start_time); if (st<0) funwaveC_error_message("Bad option to atoi()\n"); un1 = fix_units(tunits); st = change_units(st,un1); } else { st = 0; } } T->start_time = st; } return T;}field2D* ptr_to_field2D(variable_t T){ switch (T){ case ETA_M: return ETA; case V_M: return VV; case U_M: return UU; case TRACER_M: return QQ; case TAUBX_M: return TAUBX; case TAUBY_M: return TAUBY; case VORTICITY_M: return NULL; case EDDY_BR_M: return breaking_eddy_viscosity(); case DEPTH_M: return H; // case STREAMF_M: return NULL; default: funwaveC_error_message("Bad arg to ptr_to_field2D"); }}/*----- oflags1 & level1 parsing of init.dat file routines ------ */void parse_level_point(char *s, oflags *F){ char vtype[16],xloc[16],yloc[16],tmp[20],*rest; int num; point *P; P = (point * ) g_malloc(sizeof(point)); num = sscanf(s,"%*s %*s %s %s %s %s",vtype,xloc,yloc,tmp); rest = strstr(s,tmp); if (num!=4) funwaveC_error_message("Syntax Error: Not enough arguments for point output!"); P->var = variable_t_index(vtype); P->px = atoi(xloc); P->py = atoi(yloc); // check_xloc(P->px,P->var); // check to make sure in bounds // check_yloc(P->py); // check to make sure in bounds P->OP = parse_output_options(rest); if (P->OP->OT == FILE_M) { P->OP->fp = open_file_for_writing(P->OP->fname->str); } F->point_list = g_slist_append(F->point_list,P);}void parse_level_cross(char *s, oflags *F){ char vtype[16],yloc[16],tmp[80],*rest; int num; cross_line *P; P = (cross_line * ) g_malloc(sizeof(cross_line)); num = sscanf(s,"%*s %*s %s %s %s",vtype,yloc,tmp); rest = strstr(s,tmp);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -