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