FrontISTR  5.7.0
Large-scale structural analysis program with finit element method
neu_reporter.cpp
Go to the documentation of this file.
1 /*****************************************************************************
2  * Copyright (c) 2019 FrontISTR Commons
3  * This software is released under the MIT License, see LICENSE.txt
4  *****************************************************************************/
5 /*
6  neu_reporter ver.1.0
7  -----------------------
8  reporting neutral file
9 */
10 
11 #include <iostream>
12 #include <stdio.h>
13 #include "CNFData.h"
14 
15 using namespace std;
16 
17 char run_name[256];
19 FILE* wfp;
20 
21 //=============================================================================
22 // etc.
23 //=============================================================================
24 
25 static void get_fname_from_path(char* fname, const char* path) {
26  int n = strlen(path);
27  char* p = (char*)&path[n - 1];
28  while (path < p && (*p != '\\') && (*p != '/')) p--;
29  if ((*p == '\\') || (*p == '/')) p++;
30  strcpy(fname, p);
31 }
32 
33 //-----------------------------------------------------------------------------
34 
35 // return 0 : quit
36 static int input_block_id() {
37  char buff[256];
38  int block_id;
39 
40  while (1) {
41  printf("block# ? (h:show selectable block id, e:exit) > ");
42  fflush(stdout);
43  fgets(buff, sizeof(buff), stdin);
44  char c = buff[0];
45  if (c == 'h') {
46  for (int i = 0; i < NFD_SupportedBlockListSize; i++) {
47  fprintf(wfp, "%8d\n", NFD_SupportedBlockList[i]);
48  }
49  } else if (c == 'e') {
50  return 0;
51  } else {
52  if (sscanf(buff, "%d", &block_id) == 1) {
53  bool fg = false;
54  for (int i = 0; i < NFD_SupportedBlockListSize; i++) {
55  if (NFD_SupportedBlockList[i] == block_id) {
56  fg = true;
57  break;
58  }
59  }
60  if (fg) break;
61  printf("Not supported block id\n");
62  } else {
63  printf("Cannot converting number\n");
64  }
65  }
66  }
67 
68  return block_id;
69 }
70 
71 //=============================================================================
72 // command record & list
73 //=============================================================================
74 
75 bool fg_quit = false;
76 
77 class ccmd_rec {
78  public:
79  char name[256];
80  char help[256];
81  void (*cmd)();
82  ccmd_rec();
83  ccmd_rec(const char* n); // for key
84  ccmd_rec(const char* n, const char* h, void (*f)());
85  ccmd_rec(const ccmd_rec& rec);
86 };
87 
88 set<ccmd_rec> cmd_list;
89 
90 ccmd_rec::ccmd_rec() : cmd(0) {
91  name[0] = 0;
92  help[0] = 0;
93 }
94 
95 ccmd_rec::ccmd_rec(const char* n) : cmd(0) {
96  strcpy(name, n);
97  help[0] = 0;
98 }
99 
100 ccmd_rec::ccmd_rec(const char* n, const char* h, void (*f)()) {
101  strcpy(name, n);
102  strcpy(help, h);
103  cmd = f;
104 }
105 
107  strcpy(name, rec.name);
108  strcpy(help, rec.help);
109  cmd = rec.cmd;
110 }
111 
112 inline bool operator==(const ccmd_rec& a, const ccmd_rec& b) {
113  return (strcmp(a.name, b.name) == 0);
114 }
115 inline bool operator<(const ccmd_rec& a, const ccmd_rec& b) {
116  int c = strcmp(a.name, b.name);
117  return c < 0;
118 }
119 inline bool operator>(const ccmd_rec& a, const ccmd_rec& b) {
120  int c = strcmp(a.name, b.name);
121  return c > 0;
122 }
123 
124 //=============================================================================
125 // commands
126 //=============================================================================
127 
128 #define str_cmd_help "h"
129 #define help_cmd_help "show command list"
130 void cmd_help() {
131  printf("\n");
132  set<ccmd_rec>::iterator iter;
133  for (iter = cmd_list.begin(); iter != cmd_list.end(); iter++) {
134  printf(" %8s : %s\n", iter->name, iter->help);
135  }
136  printf("\n");
137  fflush(stdout);
138 }
139 
140 //-----------------------------------------------------------------------------
141 
142 #define str_cmd_quit "quit"
143 #define help_cmd_quit "quit"
144 void cmd_quit() { fg_quit = true; }
145 
146 //-----------------------------------------------------------------------------
147 
148 #define str_cmd_save "save"
149 #define help_cmd_save "save"
150 void cmd_save() {
151  char fname[256];
152  printf("save file name >> ");
153  fflush(stdout);
154  fgets(fname, sizeof(fname), stdin);
155  fname[strlen(fname) - 1] = 0;
156 
157  try {
158  data.Save(fname);
159  } catch (...) {
160  printf("error in saving..\n");
161  fflush(stdout);
162  }
163 }
164 
165 //-----------------------------------------------------------------------------
166 
167 #define str_cmd_open_outfile "open"
168 #define help_cmd_open_outfile "open output file"
170  char fname[256];
171  printf("output file? >");
172  fflush(stdout);
173  fgets(fname, sizeof(fname), stdin);
174  fname[strlen(fname) - 1] = 0; // remove CR/LF
175 
176  FILE* fp = fopen(fname, "w");
177  if (!fp) {
178  cout << "Cannot open file" << endl;
179  return;
180  }
181 
182  if (wfp != stdout && wfp != stderr) {
183  fclose(wfp);
184  printf("previous output file closed.\n");
185  }
186  wfp = fp;
187 
188  printf("%s is opened.\n", fname);
189 }
190 
191 //-----------------------------------------------------------------------------
192 
193 #define str_cmd_close_outfile "close"
194 #define help_cmd_close_outfile "close output file"
196  if (wfp != stdout && wfp != stderr) {
197  fclose(wfp);
198  printf("output file closed.\n");
199  wfp = stdout;
200  }
201 }
202 
203 //-----------------------------------------------------------------------------
204 
205 #define str_cmd_write_summary "s"
206 #define help_cmd_write_summary "write summary of loaded neutral data"
208  char fname[256];
209  get_fname_from_path(fname, data.neu_file);
210 
211  fprintf(wfp, "summary of %s\n", fname);
213 }
214 
215 //-----------------------------------------------------------------------------
216 
217 #define str_cmd_write_block "b"
218 #define help_cmd_write_block "write data block"
220  int block_id = input_block_id();
221  if (block_id == 0) return;
222  data.WriteDataBlock(wfp, block_id);
223 }
224 
225 //=============================================================================
226 // commands registration & execution
227 //=============================================================================
228 
230 #define GENERATE_CODE(x) \
231  { \
232  ccmd_rec rec(str_##x, help_##x, x); \
233  cmd_list.insert(rec); \
234  }
235 
243 
244 #undef GENERATE_CODE
245 }
246 
247 //-----------------------------------------------------------------------------
248 
249 bool execute_command(const char* name) {
250  ccmd_rec key(name);
251 
252  set<ccmd_rec>::iterator iter;
253  iter = find(cmd_list.begin(), cmd_list.end(), key);
254  if (iter == cmd_list.end()) {
255  printf("not such command (h:help)\n");
256  return false;
257  }
258  (*iter->cmd)();
259  return true;
260 }
261 
262 //-----------------------------------------------------------------------------
263 
264 void command_line() {
265  char buff[256];
266 
267  regist_commands();
268  cmd_help();
269  do {
270  printf("neu_reporter>");
271  fflush(stdout);
272  fgets(buff, sizeof(buff), stdin);
273  buff[strlen(buff) - 1] = 0; // remove CR/LF
274  execute_command(buff);
275  fflush(wfp);
276  } while (!fg_quit);
278  printf("end of neu reporter\n");
279 }
280 
281 //=============================================================================
282 // main & etc.
283 //=============================================================================
284 
285 void set_run_name(char* argv0) { get_fname_from_path(run_name, argv0); }
286 
287 void help() {
288  cout << "Neutral File Reporter Ver.1.0" << endl;
289  cout << "Copyright (C) 2005 Noboru Imai" << endl;
290  cout << "[usage] " << run_name << " [NUE file]" << endl;
291 }
292 
293 int main(int argc, char** argv) {
294  set_run_name(argv[0]);
295  wfp = stdout;
296 
297  if (argc <= 1) {
298  help();
299  return -1;
300  }
301 
302  try {
303  data.Load(argv[1]);
304  } catch (CNFError e) {
305  cout << e.Msg() << endl;
306  return -1;
307  }
308 
309  cout << "command line start..." << endl;
310 
311  command_line();
312 
313  return 0;
314 }
cmd_open_outfile
void cmd_open_outfile()
Definition: neu_reporter.cpp:169
ccmd_rec
Definition: neu_reporter.cpp:77
cmd_write_block
void cmd_write_block()
Definition: neu_reporter.cpp:219
cmd_list
set< ccmd_rec > cmd_list
Definition: neu_reporter.cpp:88
ccmd_rec::help
char help[256]
Definition: neu_reporter.cpp:80
set_run_name
void set_run_name(char *argv0)
Definition: neu_reporter.cpp:285
ccmd_rec::name
char name[256]
Definition: neu_reporter.cpp:79
CNFData.h
CNFError::Msg
virtual const char * Msg()
Definition: CNFMessage.cpp:21
CNFData::Save
virtual void Save(const char *fname)
Definition: CNFData.cpp:152
help
void help()
Definition: neu_reporter.cpp:287
run_name
char run_name[256]
Definition: neu_reporter.cpp:17
NFD_SupportedBlockListSize
const int NFD_SupportedBlockListSize
Definition: CNFData.h:42
cmd_quit
void cmd_quit()
Definition: neu_reporter.cpp:144
wfp
FILE * wfp
Definition: neu_reporter.cpp:19
cmd_save
void cmd_save()
Definition: neu_reporter.cpp:150
regist_commands
void regist_commands()
Definition: neu_reporter.cpp:229
CNFData::WriteDataBlock
bool WriteDataBlock(FILE *fp, int id)
Definition: CNFData.cpp:535
fg_quit
bool fg_quit
Definition: neu_reporter.cpp:75
CNFData
Definition: CNFData.h:46
command_line
void command_line()
Definition: neu_reporter.cpp:264
data
CNFData data
Definition: neu_reporter.cpp:18
CNFData::WriteSummary
void WriteSummary(FILE *fp=0)
Definition: CNFData.cpp:581
operator==
bool operator==(const ccmd_rec &a, const ccmd_rec &b)
Definition: neu_reporter.cpp:112
GENERATE_CODE
#define GENERATE_CODE(x)
CNFData::Load
virtual void Load(const char *fname)
Definition: CNFData.cpp:66
main
int main(int argc, char **argv)
Definition: neu_reporter.cpp:293
NFD_SupportedBlockList
const int NFD_SupportedBlockList[]
Definition: CNFData.h:43
operator<
bool operator<(const ccmd_rec &a, const ccmd_rec &b)
Definition: neu_reporter.cpp:115
CNFError
Definition: CNFMessage.h:51
execute_command
bool execute_command(const char *name)
Definition: neu_reporter.cpp:249
cmd_close_outfile
void cmd_close_outfile()
Definition: neu_reporter.cpp:195
cmd_help
void cmd_help()
Definition: neu_reporter.cpp:130
cmd_write_summary
void cmd_write_summary()
Definition: neu_reporter.cpp:207
operator>
bool operator>(const ccmd_rec &a, const ccmd_rec &b)
Definition: neu_reporter.cpp:119
ccmd_rec::ccmd_rec
ccmd_rec()
Definition: neu_reporter.cpp:90
CNFData::neu_file
char neu_file[256]
Definition: CNFData.h:89
ccmd_rec::cmd
void(* cmd)()
Definition: neu_reporter.cpp:81