File: mgl2gif.cpp

package info (click to toggle)
mathgl 1.9-3
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 17,496 kB
  • ctags: 3,647
  • sloc: cpp: 36,386; sh: 10,198; ansic: 1,093; makefile: 325; pascal: 52; python: 35
file content (135 lines) | stat: -rw-r--r-- 4,384 bytes parent folder | download
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
/***************************************************************************
 * mgl2gif.cpp is part of Math Graphic Library
 * Copyright (C) 2007 Alexey Balakin <balakin@appl.sci-nnov.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 <stdio.h>
#include <string.h>
#include <locale.h>
#include <wchar.h>
#include "mgl/mgl_zb.h"
#include "mgl/mgl_parse.h"
struct Str
{
	wchar_t *str;
	Str *next;
	Str(Str *prev, const char *s);
	Str(Str *prev, const wchar_t *s);
	Str(Str *prev=0)
	{	str=0;	next=0;	if(prev)	prev->next=this;	}
	~Str()
	{	if(str)	delete []str;	if(next)	delete next;	}
};
void wcstrim_mgl(wchar_t *str);
//-----------------------------------------------------------------------------
Str::Str(Str *prev, const wchar_t *s)
{
	str=0;	next=0;	if(prev)	prev->next=this;
	if(s)
	{
		str = new wchar_t[wcslen(s)+1];
		wcscpy(str,s);
	}
}
//-----------------------------------------------------------------------------
Str::Str(Str *prev, const char *s)
{
	str=0;	next=0;	if(prev)	prev->next=this;
	if(s)
	{
		str = new wchar_t[strlen(s)+1];
		mbstowcs(str,s,strlen(s));
	}
}
//-----------------------------------------------------------------------------
int main(int narg, char **arg)
{
	mglGraphZB gr;
	mglParse p(true);

	if(narg==1)
	{
		printf("mgl2gif convert mgl script to bitmap gif file.\n");
		printf("Current version is 1.%g\n",MGL_VERSION);
		printf("Usage:\tmgl2gif scriptfile [outputfile parameter(s)]\n");
		printf("\tParameters have format \"-Nval\".\n");
		printf("\tHere N=0,1...9 is parameter ID and val is its value.\n");
		printf("\tOption -Lval set locale to val.\n");
		printf("\tOption -Aval add value of $0 for making animation.\n");
	}
	else
	{
		Str *head=0, *cur=0;
		FILE *fp = fopen(arg[1],"rb");
		if(fp==0)	{	printf("Couldn't open file %s\n",arg[1]);	return 1;	}
		wchar_t str[8192];
		char fname[2048], buf[2048];
		for(long i=2;i<narg;i++)	// add arguments for the script
		{
			if(arg[i][0]=='-' && arg[i][1]>='0' && arg[i][1]<='9')
				p.AddParam(arg[i][1]-'0',arg[i]+2);
			if(arg[i][0]=='-' && arg[i][1]=='L')
				setlocale(LC_CTYPE, arg[i]+2);
			if(arg[i][0]=='-' && arg[i][1]=='A')
			{
				if(cur)	cur = new Str(cur,arg[i]+2);
				else	head = cur = new Str(0,arg[i]+2);
			}
		}
		if(narg>2 && arg[2][0]!='-')	strcpy(fname,arg[2]);
		else
		{
			strcpy(fname,arg[1]);	strcat(fname,".gif");
			printf("Write output to %s\n",fname);
		}
		// first read animation parameters from file
		while(!feof(fp))
		{
			fgetws(str,8192,fp);
			wcstrim_mgl(str);
			if(str[0]=='#' && str[1]=='#' && str[2]=='a' && str[3]==' ')
			{
				if(cur)	cur = new Str(cur,str+4);
				else	head = cur = new Str(0,str+4);
			}
		}
		// now execute it and save
		gr.Message = buf;	*buf=0;
		if(head)	// as animated gif
		{
			gr.StartGIF(fname);
			for(cur = head;cur!=0;cur=cur->next)
			{
				fseek(fp,0,SEEK_SET);
				gr.NewFrame();
				p.AddParam(0,cur->str);
				p.Execute(&gr,fp,true);
				gr.EndFrame();
			}
			gr.CloseGIF();
		}
		else	// simple gif
		{
			fseek(fp,0,SEEK_SET);
			p.Execute(&gr,fp,true);
			gr.WriteGIF(fname);
		}
		fclose(fp);
	}
	return 0;
}
//-----------------------------------------------------------------------------