File: mglview.cpp

package info (click to toggle)
mathgl 8.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 248,044 kB
  • sloc: cpp: 87,365; ansic: 3,299; javascript: 3,284; pascal: 1,562; python: 52; sh: 51; makefile: 47; f90: 22
file content (152 lines) | stat: -rw-r--r-- 5,078 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/***************************************************************************
 * mglview.cpp is part of Math Graphic Library
 * Copyright (C) 2007-2016 Alexey Balakin <mathgl.abalakin@gmail.ru>       *
 *                                                                         *
 *   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.             *
 ***************************************************************************/
#include <locale.h>
#include <getopt.h>

#include "mgl2/mgl.h"
#if USE_FLTK
	#include "mgl2/fltk.h"
	#include <FL/Fl.H>
	#include <FL/Fl_Preferences.H>
#else
	#include "mgl2/qt.h"
#endif
//-----------------------------------------------------------------------------
std::wstring str, opt;
std::vector<std::string> anim;
mglParse p(true);
void prop_func(char id, const char *val, void *)
{	p.AddParam(id<='9' ? id-'0' : id-'a'+10, val);	}
//-----------------------------------------------------------------------------
int show(mglGraph *gr)
{
	if(anim.size()>0)
	{
		for(size_t i=0;i<anim.size();i++)
		{
			gr->NewFrame();
			p.AddParam(0,anim[i].c_str());
			p.Execute(gr,str.c_str());
			gr->EndFrame();
		}
		return gr->GetNumFrame();
	}
	p.Execute(gr,str.c_str());
	printf("%s\n",gr->Message());
	return 0;
}
//-----------------------------------------------------------------------------
int main(int argc, char **argv)
{
	mgl_textdomain(argv?argv[0]:NULL,"");
	char iname[256]="";
	mgl_suppress_warn(true);
	bool gray = false;
	while(1)
	{
		int ch = getopt(argc, argv, "1:2:3:4:5:6:7:8:9:hL:s:g:v:");
		if(ch>='1' && ch<='9')	p.AddParam(ch-'0', optarg);
		else if(ch=='s')
		{
			FILE *fp = fopen(optarg,"r");
			if(fp)
			{
				wchar_t ch;
				while(!feof(fp) && size_t(ch=fgetwc(fp))!=WEOF)	opt.push_back(ch);
				fclose(fp);
			}
		}
		else if(ch=='v')	p.SetVariant(atoi(optarg));
		else if(ch=='g')	gray= atoi(optarg);
		else if(ch=='L')
		{	setlocale(LC_ALL, optarg);	setlocale(LC_NUMERIC, "C");	}
		else if(ch=='h' || (ch==-1 && optind>=argc))
		{
			printf(_("mglview show plot from MGL script or MGLD file.\nCurrent version is %s\n"),MGL_VER_STRING);
			printf(_("Usage:\tmglview [parameter(s)] scriptfile\n"));
			printf(
				_("\t-1 str       set str as argument $1 for script\n"
				"\t...          ...\n"
				"\t-9 str       set str as argument $9 for script\n"
				"\t-g val       set gray-scale mode (val=0|1)\n"
				"\t-v val       set variant of arguments\n"
				"\t-s opt       set MGL script for setting up the plot\n"
				"\t-L loc       set locale to loc\n"
				"\t-            get script from standard input\n"
				"\t-h           print this message\n") );
			return 0;
		}
		else if(ch==-1 && optind<argc)
		{	mgl_strncpy(iname, argv[optind][0]=='-'?"":argv[optind],256);	break;	}
	}

	std::string ids;
	std::vector<std::string> par;
	bool mgld=(*iname && iname[strlen(iname)-1]=='d');
	if(!mgld)
	{
		str = opt + L"\n";
		FILE *fp = *iname?fopen(iname,"r"):stdin;
		if(fp)
		{
			wchar_t ch;
			std::string text;
			while(!feof(fp) && size_t(ch=fgetwc(fp))!=WEOF)
			{	str.push_back(ch);	text.push_back(ch);	}
			fclose(fp);

			double a1, a2, da;
			mgl_parse_comments(text.c_str(), a1, a2, da, anim, ids, par);
//			if(!ids.empty())	dr->gr->dialog(ids,par);
		}
		else	{	printf("No file for MGL script\n");	return 0;	}
	}

#if USE_FLTK
	mgl_ask_func = mgl_ask_fltk;
	mgl_progress_func = mgl_progress_fltk;
	mglFLTK gr(mgld?NULL:show, *iname?iname:"mglview");
#else
	mgl_ask_func = mgl_ask_qt;
	mglQT gr(mgld?NULL:show, *iname?iname:"mglview");
#endif
	gr.SetPropFunc(prop_func,NULL);
	gr.MakeDialog(ids, par);
	if(gray)	gr.Gray(gray);

	if(mgld)
	{
		gr.Setup(false);
		gr.NewFrame();
		const std::string loc = setlocale(LC_NUMERIC, "C");
		if(!opt.empty())
		{
			p.Execute(&gr,opt.c_str());
			printf("Setup script: %s\n",gr.Message());
			gr.ImportMGLD(iname,true);
		}
		else	gr.ImportMGLD(iname);
		setlocale(LC_NUMERIC, loc.c_str());
		gr.EndFrame();	gr.Update();
	}
	printf("%s",mgl_get_global_warn());
	return gr.Run();
}
//-----------------------------------------------------------------------------