Subversion Repositories Kolibri OS

Rev

Rev 1764 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1005 barsuk 1
 
2
#include "parser.h"
3
#include "kolibri.h"
4
#include "use_library.h"
5
6
 
7
const char empty_text[] = "No function loaded. Type file name and press Enter. ";
8
const char er_file_not_found[] = "Cannot open file. ";
9
const char str_filename[]="Filename:";
10
const char str_editfile[]="Edit";
11
12
 
13
#define WND_W 400
14
#define WND_H 300
15
16
 
17
#define WHITE 0xffffff
18
#define BLACK 0x0
19
#define LIGHTBLUE 0x0000ff
20
#define LIGHTRED 0xff0000
21
22
 
23
#define BIGFONTCOLOR BLACK
24
#define SMALLFONTCOLOR BLACK
25
26
 
27
// minimum space: 3 pixels
28
29
 
30
#define SMALL_HEIGHT 2.0
31
#define TEXT_X 15.0
32
// numeric format for output
33
#define FORMAT "%f"
34
// format for two coords
35
#define FORMAT_COORD "(%f,%f)"
36
// special value to text if enough space
37
#define FORMAT_TEST "0.00"
38
39
 
40
#define DELTA_SMALL 0.1
41
42
 
43
Dword point_count = 0;
44
double x1,y1,x2,y2;
45
char *funct = NULL;
46
47
 
48
//Dword editbox_y = WND_H - 16, editbox_w = WND_W - 70;
49
edit_box mybox = {0,9*8-5,WND_H - 16-32,0xffffff,0x6a9480,0,0x808080,0,99,(dword)&edit_path,0};
50
51
 
52
53
 
54
55
 
56
// параметры командной строки
57
char params[1024] = "_FIND_ME_";
58
59
 
60
61
 
62
63
 
64
65
 
66
TCoord coord(double x, double y)
67
{
68
  TCoord r;
69
  r.x = x;
70
  r.y = y;
71
  return r;
72
}
73
74
 
75
TCoord mat2Graf(TCoord c, TCoord scrMin, TCoord scrMax, TCoord mMin, TCoord mMax)
76
{
77
  TCoord r;
78
  if (c.x > mMax.x)
79
    c.x = mMax.x;
80
  if (c.x < mMin.x)
81
    c.x = mMin.x;
82
  if (c.y > mMax.y)
83
    c.y = mMax.y;
84
  if (c.y < mMin.y)
85
    c.y = mMin.y;
86
  r.x = (scrMax.x - scrMin.x) / (mMax.x - mMin.x) * (c.x - mMin.x) + scrMin.x;
87
  r.y = (scrMax.y - scrMin.y) / (mMax.y - mMin.y) * (mMax.y - c.y) + scrMin.y;
88
89
 
90
}
91
92
 
93
void line_d( double x1, double y1, double x2, double y2)
94
{
95
   line(di(x1), di(y1), di(x2), di(y2));
96
}
97
98
 
99
{
100
   outtextxy(di(x), di(y), text, len);
101
}
102
103
 
104
void drawAxis( TCoord scrMin, TCoord scrMax, TCoord mMin, TCoord mMax)
105
{
106
  TCoord cZero={0.0,0.0},
107
	   gMin, gMax, gZero, step;
108
  TCoord from, to;
109
  double i=0.0;
110
  int j;
111
  double xmin, xmin2, ymin, ymin2;
112
  char buf[30]="";
113
114
 
115
 
116
// m   means Mathematical
117
// g   means Graphic(real screen position)
118
119
 
120
121
 
122
  //rtlDebugOutString(debuf);
123
124
 
125
  gMax = mat2Graf(mMax, scrMin, scrMax, mMin, mMax);
126
  gZero = mat2Graf(cZero, scrMin, scrMax, mMin, mMax);
127
128
 
129
 // setcolor(WHITE);
130
 //rectangle(di(gMin.x), di(gMin.y), di(gMax.x), di(gMax.y));
131
  // ftopku
132
133
 
134
  // osy X
135
  line_d(gMin.x, gZero.y ,gMax.x, gZero.y);
136
  // osy Y
137
  line_d(gZero.x, gMin.y, gZero.x, gMax.y);
138
  // bounding rect
139
  line_d(gMin.x, gMin.y, gMax.x, gMin.y);
140
  line_d(gMin.x, gMax.y, gMax.x, gMax.y);
141
142
 
143
  line_d(gMax.x, gMin.y, gMax.x, gMax.y);
144
145
 
146
  format(buf, 30, FORMAT_COORD, x1, y1);
147
  //rtlDebugOutString(buf);
148
  outtextxy_d(gMin.x, gMin.y + textheight(buf, 20), buf, 20);
149
  // upper left
150
  format(buf, 30, FORMAT_COORD, x1, y2);
151
  outtextxy_d(gMin.x, gMax.y - textheight(buf, 20), buf, 20);
152
  // lower right
153
  format(buf, 30, FORMAT_COORD, x2, y1);
154
  outtextxy_d(gMax.x - textwidth(buf, 20), gMin.y + textheight(buf, 20), buf, 20);
155
  // upper right
156
  format(buf, 30, FORMAT_COORD, x2, y2);
157
  outtextxy_d(gMax.x - textwidth(buf, 20), gMax.y - textheight(buf, 20), buf, 20);
158
159
 
160
161
 
162
 
163
  step.y = (mMax.y - mMin.y) / (scrMax.y - scrMin.y);
164
165
 
166
  xmin = id(di((mMin.x / DELTA_BIG) * DELTA_BIG));
167
  ymin = id(di((mMin.y / DELTA_BIG) * DELTA_BIG));
168
169
 
170
171
 
172
  {
173
	  from.x=0.0;
174
	  from.y=0.0;
175
	  from = mat2Graf(from, scrMin, scrMax, mMin, mMax);
176
	  setcolor(BLACK);
177
	  format(buf, 30, FORMAT, 0.0);
178
	  outtextxy_d(from.x - textwidth(buf, 20), from.y + textheight(buf, 20), buf, 20);
179
  }
180
181
 
182
 
183
  //settextstyle(0, 0, 1);
184
  if (DELTA_BIG / step.x > THREE)
185
  {
186
    for (i = xmin; i <= mMax.x; i += DELTA_BIG)
187
    {
188
	  if (i != 0.0)
189
	  {
190
		  from.x = i;
191
		  to.x = from.x;
192
		  from.y = -BIG_HEIGHT * step.y;
193
		  to.y = BIG_HEIGHT * step.y;
194
		  from = mat2Graf(from, scrMin, scrMax, mMin, mMax);
195
		  to = mat2Graf(to, scrMin, scrMax, mMin, mMax);
196
		  setcolor(BLACK);
197
		  line_d(from.x, from.y, to.x, to.y);
198
		  // write number
199
		  format(buf, 30, FORMAT, i);
200
		  // if it fits in the GAP, then write it
201
		  if (from.y > scrMin.y && (DELTA_BIG > (textwidth(buf, 20) + 1.0) * step.x))
202
		  {
203
			   setcolor(BIGFONTCOLOR);
204
			   outtextxy_d(from.x - textwidth(buf, 20) / 2.0, to.y - textheight(buf, 20), buf, 20);
205
		  }
206
	  }
207
    }
208
  }
209
  //rtlDebugOutString("big marks x painted\n");
210
211
 
212
  if (DELTA_BIG / step.y > THREE)
213
  {
214
    for (i = ymin; i <= mMax.y; i += DELTA_BIG)
215
    {
216
	  if (i != 0.0)
217
	  {
218
		  from.y = i;
219
		  to.y = from.y;
220
		  from.x = -BIG_HEIGHT * step.x;
221
		  to.x = BIG_HEIGHT * step.x;
222
		  from = mat2Graf(from, scrMin, scrMax, mMin, mMax);
223
		  to = mat2Graf(to, scrMin, scrMax, mMin, mMax);
224
		  setcolor(BLACK);
225
		  line_d(from.x, from.y, to.x, to.y);
226
		  format(buf, 30, FORMAT, i);
227
		  if (from.x > scrMin.x && (DELTA_BIG > textheight(buf, 20) * step.y))
228
		  {
229
			   setcolor(BIGFONTCOLOR);
230
			 outtextxy_d(from.x + TEXT_X, to.y - textheight(buf, 20) / 2.0, buf, 20);
231
		  }
232
	  }
233
    }
234
  }
235
236
 
237
  ymin2 = id(di(mMin.y / DELTA_SMALL)) * DELTA_SMALL;
238
239
 
240
  {
241
    j = di((( - xmin + xmin2 ) / DELTA_SMALL));
242
    for (i = xmin2; i <= mMax.x; i += DELTA_SMALL, j++)
243
    {
244
      if (j % 10 == 0)
245
      {
246
      // we need to skip every tenth mark, to avoid overwriting big marks
247
		j = 0;
248
		continue;
249
      }
250
      from.x = i;
251
      to.x = from.x;
252
      from.y = -SMALL_HEIGHT * step.y;
253
      to.y = SMALL_HEIGHT * step.y;
254
      from = mat2Graf(from, scrMin, scrMax, mMin, mMax);
255
	  to = mat2Graf(to, scrMin, scrMax, mMin, mMax);
256
      setcolor(BLACK);
257
      line_d(from.x, from.y, to.x, to.y);
258
      format(buf, 30, FORMAT, i);
259
260
 
261
      {
262
		setcolor(SMALLFONTCOLOR);
263
		outtextxy_d(from.x - textwidth(buf, 20) / 2.0, to.y - textheight(buf, 20), buf, 20);
264
      }
265
266
 
267
 
268
269
 
270
271
 
272
  if (DELTA_SMALL / step.y > THREE)
273
  {
274
    //rtlDebugOutString("really small marks y painted\n");
275
    j = di((( - ymin + ymin2) / DELTA_SMALL));
276
    for (i = ymin2; i <= mMax.y; i += DELTA_SMALL, j++)
277
    {
278
      if (j % 10 == 0)
279
      {
280
      // we need to skip every tenth, to avoid overwriting
281
		j = 0;
282
		continue;
283
      }
284
      from.y = i;
285
      to.y = from.y;
286
      from.x = -SMALL_HEIGHT * step.x;
287
      to.x = SMALL_HEIGHT * step.x;
288
      from = mat2Graf(from, scrMin, scrMax, mMin, mMax);
289
      to = mat2Graf(to, scrMin, scrMax, mMin, mMax);
290
      setcolor(BLACK);
291
      line_d(from.x, from.y, to.x, to.y);
292
      format(buf, 30, FORMAT, i);
293
      if (from.x > scrMin.x && (DELTA_SMALL > textheight(buf, 20) * step.y))
294
      {
295
       	setcolor(SMALLFONTCOLOR);
296
       	outtextxy_d(from.x + TEXT_X, from.y - textheight(buf, 20) / 2.0, buf, 20);
297
      }
298
    }
299
  }
300
301
 
302
303
 
304
  ends fucking piece of shit
305
*/
306
307
 
308
		   TCoord mMin, TCoord mMax, DWORD color)
309
{
310
  double x;
311
  double y;
312
  int firstPoint = 1;
313
  TCoord p, p0 = {0.0, 0.0}, step;
314
315
 
316
317
 
318
  step.x = (mMax.x - mMin.x) / (scrMax.x - scrMin.x);
319
320
 
321
  {
322
    y = fi(x);
323
// function is defined here and gets in the range
324
    if (1) // тут было условие, что функция правильно вычислена
325
    {
326
      if ((y > mMin.y) && (y < mMax.y))
327
      {
328
	      p = mat2Graf(coord(x, y), scrMin, scrMax, mMin, mMax);
329
	// if it's our first point, only remember its coords
330
	// otherwise, draw a line_d from prev to current
331
      	if (firstPoint == 0)
332
        {
333
      	  line_d(p0.x, p0.y, p.x, p.y);
334
        }
335
      	else
336
    	  firstPoint = 0;
337
338
 
339
      }
340
      else // too big/small
341
      {
342
	      firstPoint = 1;
343
      }
344
    }
345
    else // no value
346
    {
347
      firstPoint = 1;
348
    }
349
  }
350
351
 
352
353
 
354
{
355
	Dword attrib;
356
	Dword name_type;
357
	Dword create_time;
358
	Dword create_date;
359
	Dword access_time;
360
	Dword access_date;
361
	Dword modify_time;
362
	Dword modify_date;
363
	Dword size_low;
364
	Dword size_high;
365
};
366
367
 
368
int load_points3()
369
{
370
	kosFileInfo fileInfo;
371
	kosBDVK bdvk;
372
	int filePointer = 0;
373
374
 
375
	int i,j,k;
376
	double d;
377
	Dword filesize, num_number;
378
379
 
380
381
 
382
		return 0;
383
384
 
385
	strcpy(fileInfo.fileURL,edit_path);
386
	fileInfo.OffsetLow = 0;
387
	fileInfo.OffsetHigh = 0;
388
	fileInfo.dataCount = 0;
389
	fileInfo.rwMode = 5;
390
	fileInfo.bufferPtr = (Byte *)&bdvk;
391
	Dword rr = kos_FileSystemAccess( &(fileInfo) ); // в CKosFile нет определения размера
392
	sprintf(debuf, "getsize: %U\n", rr);
393
	rtlDebugOutString(debuf);
394
	if (rr != 0)
395
	{
396
		kos_WriteTextToWindow(10,10,0,0x00,(char*)er_file_not_found,strlen(er_file_not_found));
397
		return 0;
398
	}
399
400
 
401
	num_number = filesize / 2;
402
403
 
404
405
 
406
		HugeBuf[i] = 0;
407
408
 
409
	fileInfo.OffsetLow = 0;
410
411
 
412
	fileInfo.dataCount = filesize;
413
	fileInfo.rwMode = 0;
414
	fileInfo.bufferPtr = (Byte *)HugeBuf;
415
	rr = kos_FileSystemAccess( &(fileInfo) );	// какая-то проблема с hands.dll, CKosFile не работал
416
417
 
418
	rtlDebugOutString(debuf);
419
420
 
421
	strcpy(full_head+strlen(full_head), " - ");
422
	strcpy(full_head+strlen(full_head), edit_path);		// bad code
423
424
 
425
426
 
427
	k=0;
428
	while (i < filesize)
429
	{
430
431
 
432
		if (i == filesize) break;
433
		if (k==4 && HugeBuf[i] == '=')
434
		{
435
			//sprintf(debuf,"function: %S",HugeBuf + i);
436
			//rtlDebugOutString(debuf);
437
			// we have a function here
438
			//HugeBuf[0] = ' ';
439
			funct = HugeBuf + i + 1;
440
			strcpy(full_head+strlen(full_head), ". Function y=");
441
			strcpy(full_head+strlen(full_head), funct);
442
			return 1;
443
		}
444
445
 
446
		if (d == ERROR)
447
		{
448
			sprintf(debuf, "Error in input file, byte %U, count %U\n", i, k);
449
			rtlDebugOutString(debuf);
450
			kos_WriteTextToWindow(10, 10, 0, 0x00, (char*)debuf, strlen(debuf));
451
			return 0;
452
		}
453
		if (d == ERROR_END)
454
		{
455
			rtlDebugOutString("EOF :)!\n");
456
			break;
457
		}
458
459
 
460
		switch (k)
461
		{
462
		case 0:
463
			x1=d;
464
			break;
465
		case 1:
466
			x2=d;
467
			break;
468
		case 2:
469
			y1=d;
470
			break;
471
		case 3:
472
			y2=d;
473
			break;
474
		default:
475
			{
476
				if (p2 == NULL)
477
					p2 = (double *)allocmem(num_number * 8);
478
				p2[k-4]=d;
479
			}
480
		}
481
		k++;
482
	}
483
//	format(debuf, 30, "(%f,%f)-(%f,%f)",x1,y1,x2,y2);
484
//	rtlDebugOutString(debuf);
485
	point_count=(k - 4)/2;
486
487
 
488
	points = (double *)allocmem(point_count * 2 * 8);
489
	for (i = 0; i < point_count * 2; i++)
490
		points[i] = p2[i];
491
	freemem(p2);
492
//	sprintf(debuf, "count: %U\n", point_count);
493
//	rtlDebugOutString(debuf);
494
	sprintf(debuf, ". Number of points: %U.", point_count);
495
	strcpy(full_head+strlen(full_head), debuf);
496
	freemem(HugeBuf);
497
	HugeBuf = NULL;
498
	return 1;
499
}
500
501
 
502
{
503
	kosFileInfo fileInfo;
504
505
 
506
	fileInfo.OffsetLow = 0;
507
	fileInfo.OffsetHigh = (Dword)edit_path;
508
	fileInfo.rwMode = 7;	// launch
509
	kos_FileSystemAccess(&fileInfo);
510
511
 
512
513
 
514
double fu(double x)
515
{
516
	int i;
517
	double res;
518
519
 
520
 
521
	{
522
		set_exp(funct,x);
523
		get_exp(&res);		// парсить для каждого значения х? да я с ума сошел.
524
		return res;
525
	}
526
527
 
528
	{
529
		return 0.0;
530
	}
531
532
 
533
		return points[1];
534
	if (x >= points[(point_count-1) * 2])
535
		return points[(point_count-1) * 2 + 1];
536
537
 
538
	{
539
		if ((x >= points[2 * i]) && (x < points[2 * (i + 1)]))
540
			break;
541
	}
542
543
 
544
		* (points[2 * (i + 1) + 1] - points[2 * i + 1]) + points[2 * i + 1];
545
546
 
547
548
 
549
{
550
	char str[80];
551
	int i;
552
	double xx0=0.0, yy0=0.0, xx,yy;
553
	sProcessInfo info;
554
	Dword wi, he;
555
	void *p;
556
557
 
558
		info.rawData[i] = 0;
559
	kos_ProcessInfo(&info, 0xFFFFFFFF);
560
561
 
562
	wi = *(Dword *)(p);
563
	he = *(Dword *)((Byte *)p + 4);
564
565
 
566
		wi = WND_W;
567
	if (he == 0)
568
		he = WND_H;
569
570
 
571
	mybox.width = wi - mybox.left - 80;
572
573
 
574
	kos_WindowRedrawStatus(1);
575
	kos_DefineAndDrawWindow(10,40,WND_W,WND_H,
576
		0x33,0xFFFFFF,0,0,(Dword)full_head);
577
578
 
579
580
 
581
	{
582
		kos_WriteTextToWindow((wi - 6 * strlen(empty_text))/2,he/2,0,0x000000,(char *)empty_text,strlen(empty_text));
583
	}
584
	else
585
	{
586
		drawFunction(&fu, coord(10, 20), coord(id(wi - 20), id(he - 70)),
587
							coord(x1,y1), coord(x2,y2), 0x00ff0000);
588
589
 
590
591
 
592
593
 
594
		edit_box_draw((DWORD)&mybox);
595
596
 
597
	kos_WriteTextToWindow(wi - 58, mybox.top + 4, 0, 0, (char*)str_editfile, strlen(str_editfile));
598
599
 
600
	kos_WindowRedrawStatus(2);
601
}
602
603
 
604
{
605
	kos_InitHeap();
606
	full_head = (char*)allocmem(300);
607
	strcpy(full_head, "Graph");
608
	load_edit_box();
609
	if (params[0]) // fuck[0] for debug
610
	{
611
		rtlDebugOutString("launched with params");
612
		rtlDebugOutString((char*)params);
613
		strcpy(edit_path, params);
614
		//rtlDebugOutString((char*)edit_path);
615
		load_points3();
616
	}
617
	rtlDebugOutString("data loaded.\n");
618
	draw_window();
619
	for (;;)
620
	{
621
		edit_box_mouse((dword)&mybox);
622
		switch (kos_WaitForEvent())
623
		{
624
		case 1:
625
			draw_window();
626
			break;
627
		case 2:
628
			// key pressed, read it
629
			Byte keyCode;
630
			kos_GetKey(keyCode);
631
632
 
633
				{
634
					case 0x0D:
635
							if (HugeBuf!=NULL)
636
							{
637
								//sprintf(debuf, "freemem: HugeBuf = %X", HugeBuf);
638
								//rtlDebugOutString(debuf);
639
								freemem((void*)HugeBuf);		// что за баг - понять не могу.
640
								HugeBuf = NULL;
641
								funct = NULL;
642
							}
643
							if (points!=NULL)
644
							{
645
								//sprintf(debuf, "freemem: points = %X", points);
646
								//rtlDebugOutString(debuf);
647
								freemem((void*)points);		// и тут. ну не обращаюсь я к этому указателю, только память в него
648
														// потом снова выделяю
649
								points = NULL;
650
							}
651
							point_count = 0;
652
							kos_DrawBar(10,10,200,20,0xFFFFFF); // фон для сообщений об ошибках
653
							if (load_points3())
654
								draw_window();
655
							break;
656
					default:
657
						{
658
							__asm
659
							{
660
								mov ah, keyCode
661
							}
662
							edit_box_key((dword)&mybox);
663
						}
664
				}
665
			break;
666
667
 
668
 
669
			// button pressed; we have only one button, close
670
			Dword button;
671
			kos_GetButtonID(button);
672
			if (button == 1)
673
				kos_ExitApp();
674
			else if (button == 5)
675
			{
676
				LaunchTinypad();
677
			}
678
		}
679
	}
680
}
681
>
682