FrontISTR 5.2.0
Large-scale structural analysis program with finit element method
Loading...
Searching...
No Matches
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
15using namespace std;
16
17char run_name[256];
19FILE* wfp;
20
21//=============================================================================
22// etc.
23//=============================================================================
24
25static 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
36static 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
75bool fg_quit = false;
76
77class 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
88set<ccmd_rec> cmd_list;
89
91 name[0] = 0;
92 help[0] = 0;
93}
94
95ccmd_rec::ccmd_rec(const char* n) : cmd(0) {
96 strcpy(name, n);
97 help[0] = 0;
98}
99
100ccmd_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
112inline bool operator==(const ccmd_rec& a, const ccmd_rec& b) {
113 return (strcmp(a.name, b.name) == 0);
114}
115inline bool operator<(const ccmd_rec& a, const ccmd_rec& b) {
116 int c = strcmp(a.name, b.name);
117 return c < 0;
118}
119inline 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"
130void 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"
144void cmd_quit() { fg_quit = true; }
145
146//-----------------------------------------------------------------------------
147
148#define str_cmd_save "save"
149#define help_cmd_save "save"
150void 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
249bool 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
265 char buff[256];
266
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
285void set_run_name(char* argv0) { get_fname_from_path(run_name, argv0); }
286
287void 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
293int 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}
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
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)
int main()
Definition: varray_test.c:10