PEBL 2.2
Psychology Experiment Building Language - Cross-platform psychological experiment development system
PEBLStream.cpp
Go to the documentation of this file.
1//* -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*- */
3// Name: libs/PEBLStream.cpp
4// Purpose: Built-in stream functions for PEBL
5// Author: Shane T. Mueller, Ph.D.
6// Copyright: (c) 2003-2026 Shane T. Mueller <smueller@obereed.net>
7// License: GPL 2
8//
9//
10//
11// This file is part of the PEBL project.
12//
13// PEBL is free software; you can redistribute it and/or modify
14// it under the terms of the GNU General Public License as published by
15// the Free Software Foundation; either version 2 of the License, or
16// (at your option) any later version.
17//
18// PEBL is distributed in the hope that it will be useful,
19// but WITHOUT ANY WARRANTY; without even the implied warranty of
20// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21// GNU General Public License for more details.
22//
23// You should have received a copy of the GNU General Public License
24// along with PEBL; if not, write to the Free Software
25// Foundation, Inc., 59 Temple Place, Suite 330,
26// Boston, MA 02111-1307 USA
28
29#ifdef PEBL_WIN32
30// Prevent Windows from defining macros that conflict with our methods
31#define WIN32_LEAN_AND_MEAN // Reduces windows.h bloat
32#define NOGDI // Prevents GetObject and other GDI macros
33#define NOSERVICE // Prevents service-related macros
34#define NOMCX // Prevents MCX macros
35#define NOIME // Prevents IME macros
36// CopyFile is not preventable by NO* macros, so we'll undef it later
37// Include winsock2.h first to avoid conflicts with windows.h
38#include <winsock2.h>
39#endif
40
41#include "PEBLStream.h"
42
43#ifdef PEBL_EMSCRIPTEN
44#include "../base/Evaluator-es.h"
45#else
46#include "../base/Evaluator.h"
47#endif
48
49#include "../devices/PStream.h"
50#include "../utility/PError.h"
51#include "../utility/PEBLPath.h"
52#include "../utility/PEBLUtility.h"
53
54#ifdef PEBL_HTTP
55#include "../utility/PEBLHTTP.h"
56#ifdef PEBL_VALIDATOR
57#include "../platforms/validator/PlatformNetwork.h"
58#else
59#include "../platforms/sdl/PlatformNetwork.h"
60#endif
61#endif
62
63
64#include "../devices/PParallelPort.h"
65#ifdef PEBL_VALIDATOR
66// SDLUtility not needed in validator mode
67#else
68#include "../platforms/sdl/SDLUtility.h"
69#endif
70
71
72
73#include <list>
74#include <iostream>
75#include <fstream>
76#include <vector>
77#include <cstring>
78
79//This is for pport stuff used for testing.
80#include <stdio.h>
81#include <stdlib.h>
82#include <unistd.h>
83
84#if defined(PEBL_LINUX)
85#include <sys/io.h>
86#endif
87
88
89
90#define BASE 0x378
91
92const static int BSIZE =4096;
93
94#ifdef PEBL_WIN32
95// Undefine Windows API macros that conflict with our method names
96// Must be done after all headers are included
97#ifdef GetObject
98#undef GetObject
99#endif
100#ifdef SetPort
101#undef SetPort
102#endif
103#ifdef CopyFile
104#undef CopyFile
105#endif
106#endif
107
108using std::cout;
109using std::flush;
110using std::cerr;
111using std::endl;
112using std::ios_base;
113
114
116{
117
118 PList * plist = v.GetComplexData()->GetList();
119 Variant v1 = plist->First();
120 //No type assertion is needed, because everything should work.
121 cout << v1 << endl;
122
123 return v1;
124}
125
126
128{
129
130 PList * plist = v.GetComplexData()->GetList();
131 Variant v1 = plist->First();
132 //No type assertion is needed, because everything should work.
133 cout << v1 << flush;
134
135 return v1;
136}
137
139{
140
141 PList * plist = v.GetComplexData()->GetList();
142 Variant v1 = plist->First();
143 PError::SignalFatalError("Function [Format()] not implemented.");
144 return v1;
145}
146
147
150{
151 PList * plist = v.GetComplexData()->GetList();
152 Variant v1 = plist->First();// plist->PopFront();
153 PError::AssertType(v1, PEAT_STRING, "Argument error in function FileOpenRead(<string>)]: ");
154
157 mystream->SetProperty("FILENAME",v1);
158 mystream->SetProperty("DIRECTION","READ");
159 PComplexData * pcd = new PComplexData(mystream);
160 Variant tmp = Variant(pcd);
161 delete pcd;
162 pcd=NULL;
163 return tmp;
164}
165
168{
169
170
171 // should be able to create the directory if it doesn't exist.
172 // char sysText[256];
173 // sprintf( systext, "mkdir -p %s", path ); // path is a char * to the string representing the directory you want to create
174 // system( systext );
175
176
177 PList * plist = v.GetComplexData()->GetList();
178 Variant v1 = plist->First(); //plist->PopFront();
179 PError::AssertType(v1, PEAT_STRING, "Argument error in function FileOpenWrite(<string>)]: ");
180
182 Variant v11=Variant(v1);
183 if(out)
184 {
185 // The file exists, so let's do some text munching and
186 // create a new filename to use.
187 std::string fname = (v1);
188 size_t i = fname.rfind(".");
189 std::string base, ext;
190 if(i != 0)
191 {
192 base = fname.substr(0,i);
193 ext = fname.substr(i+1,fname.length());
194 }
195 else
196 {
197 //No . found
198 base = fname;
199 ext = "";
200 }
201 //See if the last character of base is numeric
202 int version = base[base.length()];
203 if(version>0)
204 {
205 base = base.substr(0,base.length()-1);
206 }
207
208 v11= Variant(base) + Variant(version) + Variant(".")+Variant(ext);
209 while( PEBLUtility::FileExists(v11))
210 {
211
212 v11 = Variant(base) + Variant(version++) + Variant(".")+Variant(ext);
213
214 }
215
216 PError::SignalWarning(Variant("File [") + v1 + Variant("] already exists. Using [")+ v11 + Variant("] instead"));
217 }
220 mystream->SetProperty("FILENAME",v11);
221 mystream->SetProperty("DIRECTION","WRITE");
222 PComplexData * pcd = new PComplexData(mystream);
223 Variant tmp = Variant(pcd);
224 delete pcd;
225 pcd=NULL;
226 return tmp;
227
228}
229
230
231
232
235{
236
237
238 // should be able to create the directory if it doesn't exist.
239 // char sysText[256];
240 // sprintf( systext, "mkdir -p %s", path ); // path is a char * to the string representing the directory you want to create
241 // system( systext );
242
243
244 PList * plist = v.GetComplexData()->GetList();
245 Variant v1 = plist->First(); //plist->PopFront();
246 PError::AssertType(v1, PEAT_STRING, "Argument error in function FileOpenOverWrite(<string>)]: ");
247
250 mystream->SetProperty("FILENAME",v1);
251 mystream->SetProperty("DIRECTION","WRITE");
252
253 PComplexData * pcd = new PComplexData(mystream);
254 Variant tmp = Variant(pcd);
255 delete pcd;
256 pcd=NULL;
257 return tmp;
258
259}
260
261
264{
265 PList * plist = v.GetComplexData()->GetList();
266 Variant v1 = plist->First(); //plist->PopFront();
267 PError::AssertType(v1, PEAT_STRING, "Argument error in function FileOpenAppend(<string>)]: ");
268
269
272 mystream->SetProperty("FILENAME",v1);
273 mystream->SetProperty("DIRECTION","WRITE");
274
275 PComplexData * pcd = new PComplexData(mystream);
276 Variant tmp = Variant(pcd);
277 delete pcd;
278 pcd=NULL;
279 return tmp;
280
281
282}
283
286{
287 //v[1] should have the file stream to close
288 PList * plist = v.GetComplexData()->GetList();
289
290 Variant v1 = plist->First();// plist->PopFront();
291 PError::AssertType(v1, PEAT_FILESTREAM, "Argument error in function [FileClose(<file-stream>)]: ");
292
293
294 counted_ptr<PEBLObjectBase> tmp2 = (v1.GetComplexData())->GetObject();
295 PStream * mystream = dynamic_cast<PStream*>(tmp2.get());
296 return Variant(mystream->Close());
297}
298
299
301{
302 //v[1] should have the file stream
303 //v[2] should have the variant to print
304 PList * plist = v.GetComplexData()->GetList();
305
306 Variant v1 = plist->First(); //plist->PopFront();
307
308
309 PError::AssertType(v1, PEAT_FILESTREAM, "Argument error in first parameter of function [FilePrint(<file-stream>, <text>)]: ");
310
311 counted_ptr<PEBLObjectBase> tmp2 = (v1.GetComplexData())->GetObject();
312 PStream * mystream = dynamic_cast<PStream*>(tmp2.get());
313
314
315 Variant v2 = plist->Nth(2); //plist->PopFront();
316 std::string s = v2.GetString();
317
318 mystream->WriteString(s + "\n");
319 return (v2+Variant("\n"));
320}
321
323{
324 //v[1] should have the file stream
325 //v[2] should have the variant to print
326 PList * plist = v.GetComplexData()->GetList();
327
328 Variant v1 = plist->First(); //plist->PopFront();
329
330 PError::AssertType(v1, PEAT_FILESTREAM, "Argument error in first parameter of function [FilePrint_(<file-stream>, <text>)]: ");
331
332 counted_ptr<PEBLObjectBase> tmp2 = (v1.GetComplexData())->GetObject();
333 PStream * mystream = dynamic_cast<PStream*>(tmp2.get());
334
335 Variant v2 = plist->Nth(2); //plist->PopFront();
336 mystream->WriteString(v2);
337 return v2;
338}
339
341{
342 //v[1] should have the file stream
343 PList * plist = v.GetComplexData()->GetList();
344
345 Variant v1 = plist->First(); //plist->PopFront();
346
347 PError::AssertType(v1, PEAT_FILESTREAM, "Argument error in function [FileReadCharacter(<file-stream>)]: ");
348 counted_ptr<PEBLObjectBase> tmp2 = (v1.GetComplexData())->GetObject();
349
350 PStream * mystream = dynamic_cast<PStream*>(tmp2.get());
351 char c = (mystream->ReadChar());
352 return Variant((char)c);
353}
354
356{
357 //v[1] should have the file stream
358 PList * plist = v.GetComplexData()->GetList();
359
360 Variant v1 = plist->First(); //plist->PopFront();
361
362 PError::AssertType(v1, PEAT_FILESTREAM, "Argument error in function [FileReadWord(<file-stream>)]: ");
363
364 counted_ptr<PEBLObjectBase> tmp2 = (v1.GetComplexData())->GetObject();
365 PStream * mystream = dynamic_cast<PStream*>(tmp2.get());
366
367 return Variant(mystream->ReadToken(' '));
368}
369
370
371//This takes a filestream and returns the next line.
373{
374 PList * plist = v.GetComplexData()->GetList();
375 //v[1] should have the file stream
376 Variant v1 = plist->First();// plist->PopFront();
377
378 PError::AssertType(v1, PEAT_FILESTREAM, "Argument error in function [FileReadLine(<file-stream>)]: ");
379 counted_ptr<PEBLObjectBase> tmp2 = (v1.GetComplexData())->GetObject();
380 PStream * mystream = dynamic_cast<PStream*>(tmp2.get());
381
382 return Variant(mystream->ReadLineClean());
383}
384
385
386
387
392{
393
394 //v[1] should have the filename
395 PList * plist = v.GetComplexData()->GetList();
396
397 Variant v1 = plist->First(); //plist->PopFront();
398 PError::AssertType(v1, PEAT_STRING, "Argument error in function [FileReadList(<filename>)]: ");
399
400 //Search through the paths for the file, because it may be a 'special' file.
401 string filename = Evaluator::gPath.FindFile(v1.GetString());
402
403 if(filename == "")
404 PError::SignalFatalError(string("Unable to find file [") + v1.GetString() + string("]."));
405
406 //It must be good, so open it.
407 PStream * mystream = new PStream(filename,sdRead, stASCII);
408
409
410
411 PList * returnlist = new PList();
412 std::string tmpstring;
413 while(!mystream->Eof())
414 {
415 tmpstring = mystream->ReadLineClean();
416
417 if(strcmp("",tmpstring.c_str()) && //Ignore blank lines
418 strncmp("#",tmpstring.c_str(),1) //Ignore lines starting with #
419 )
420 {
421 returnlist->PushBack(tmpstring);
422 }
423 }
424 mystream->Close();
425 delete mystream;
426
428 PComplexData * pcd = new PComplexData(tmp2);
429 Variant tmp3 = Variant(pcd);
430 delete pcd;
431 pcd=NULL;
432 return tmp3;
433}
434
435
440{
441 //v[1] should have the file stream
442 PList * plist = v.GetComplexData()->GetList();
443 Variant v1 = plist->First(); //plist->PopFront();
444 PError::AssertType(v1, PEAT_STRING, "Argument error in first parameter of function [FileReadTable(<list>,opt:<sep>)]: ");
445
446 //Search through the paths for the file, because it may be a 'special' file.
447 string filename = Evaluator::gPath.FindFile(v1.GetString());
448
449 if(filename == "")
450 PError::SignalFatalError(string("Unable to find file [") +v1.GetString() + string("]."));
451
452 //It must be good, so open it.
453 PStream * myStream = new PStream(filename,sdRead, stASCII);
454
455 char separator;
456 //See if there is another parameter; if there is, it is the token separator.
457 if(plist->Length()>1)
458 {
459 PError::AssertType(plist->Nth(2), PEAT_STRING, "Argument error in second parameter of function [FileReadTable(<list>,opt:<sep>)]: ");
460 std::string tmp = plist->Nth(2);
461 separator = tmp[0];
462
463 }
464 else
465 {
466 separator = ' ';
467 }
468
469 //Make an outer list to put everything in.
470
471 PList * returnlist = new PList();
472
473 //Make an inner list
474 Variant innerlist;
475
476 std::string tmpstring;
477
478 while(!myStream->Eof())
479 {
480
481 tmpstring = myStream->ReadLineClean();
482 if((strcmp("",tmpstring.c_str()) == 0) || //Ignore blank lines
483 (strncmp("#",tmpstring.c_str(),1)==0) //Ignore lines starting with #
484 )
485 {
486
487 //This line is garbage, so read it and throw it away.
488
489 }
490 else
491 {
492 innerlist = PEBLUtility::Tokenize(tmpstring.c_str(),separator);
493 returnlist->PushBack(innerlist);
494 }
495 }
496 myStream->Close();
497 delete myStream;
499 PComplexData * pcd = new PComplexData(tmp2);
500 Variant tmp3 = Variant(pcd);
501 delete pcd;
502 pcd=NULL;
503 return tmp3;
504
505}
506
507
514{
515
516 //v[1] should have the filename
517 PList * plist = v.GetComplexData()->GetList();
518 Variant v1 = plist->First(); //plist->PopFront();
519 PError::AssertType(v1, PEAT_STRING, "Argument error in function [FileReadText(<filename>)]: ");
520
521 //Search through the paths for the file, because it may be a 'special' file.
522 string filename = Evaluator::gPath.FindFile(v1.GetString());
523
524 if(filename == "")
525 PError::SignalFatalError(string("Unable to find file [") + v1.GetString() + string("]."));
526
527 //It must be good, so open it.
528 PStream * mystream = new PStream(filename,sdRead, stASCII);
529
530
531 Variant returnText = "";
532 std::string tmpstring;
533 while(!mystream->Eof())
534 {
535 tmpstring = mystream->ReadLine();
536
537 if( strncmp("#",tmpstring.c_str(),1) //Ignore lines starting with #
538 )
539 {
540 //This is wrong--it will add extra space where it is not needed.
541 returnText = returnText + Variant(tmpstring);
542 }
543 }
544 mystream->Close();
545 delete mystream;
546
547 return returnText;
548}
549
550
551
554{
555 //v[1] should have the file stream
556 PList * plist = v.GetComplexData()->GetList();
557 Variant v1 = plist->First(); //plist->PopFront();
558
559 PError::AssertType(v1, PEAT_FILESTREAM, "Argument error in function [EndOfLine(<file-stream>)]: ");
560 counted_ptr<PEBLObjectBase> tmp2 = (v1.GetComplexData())->GetObject();
561 PStream * mystream = dynamic_cast<PStream*>(tmp2.get());
562 return Variant(mystream->Eol());
563}
564
565
568{
569 //v[1] should have the file stream
570 PList * plist = v.GetComplexData()->GetList();
571 Variant v1 = plist->First(); //plist->PopFront();
572
573 PError::AssertType(v1, PEAT_FILESTREAM, "Argument error in function [EndOfFile(<file-stream>)]: ");
574 counted_ptr<PEBLObjectBase> tmp2 = (v1.GetComplexData())->GetObject();
575 PStream * mystream = dynamic_cast<PStream*>(tmp2.get());
576 return Variant(mystream->Eof());
577}
578
579
580//Copies the contents of one file to another.
582{
583 //v[1] should have the filename
584 PList * plist = v.GetComplexData()->GetList();
585 Variant v1 = plist->First(); //plist->PopFront();
586 PError::AssertType(v1, PEAT_STRING, "Argument error in function [AppendFile(<file1>,<file2>)]: ");
587
588 string filename1 = v1.GetString();
589
590
591 Variant v2 = plist->Nth(2);
592 PError::AssertType(v2, PEAT_STRING, "Argument error in function [AppendFile(<file1>,<file2>)]: ");
593
594 //Search through the paths for the file, because it may be a 'special' file.
595 string filename2 = Evaluator::gPath.FindFile(v2.GetString());
596
597 if(filename2 == "")
598 PError::SignalFatalError(string("Unable to find file [") + v2.GetString() + string("] in AppendFile"));
599
600 //open files in binary mode so we can do a direct copy.
601
602 std::ifstream in(filename2.c_str(), ios_base::in|ios_base::binary);
603 std::ofstream out(filename1.c_str(), ios_base::out|ios_base::app|ios_base::binary);
604
605
606 if(!in.is_open())
607 {
608 PError::SignalWarning("Unable to open file ["+filename2+"]. No data copied.");
609 }
610 if(!out.is_open())
611 {
612 PError::SignalFatalError("Unable to open file ["+filename1+"]. Check permissions or determine whether directory exists.");
613 }
614
615
616 char buf[BSIZE];
617 do {
618 in.read(&buf[0],BSIZE);
619 out.write(&buf[0],in.gcount());
620 }while (in.gcount()>0);
621
622 in.close();
623 out.close();
624 return Variant(1);
625}
626
627
628//Copies the contents of one file to another.
629//This makes the copy byte-by-byte. It is probably better to use a systemcall function
630//to make a copy of an entire file at once. This is likely to be slower and possibly error-prone
631//(i.e., permissions and other file properties may not copy.)
633{
634 //v[1] should have the filename
635 PList * plist = v.GetComplexData()->GetList();
636 Variant v1 = plist->First(); //plist->PopFront();
637 PError::AssertType(v1, PEAT_STRING, "Argument error in function [CopyFile(<sourcefile>,<destfile>)]: ");
638
639
640 string srcfile = Evaluator::gPath.FindFile(v1.GetString());
641 if(srcfile == "")
642 PError::SignalFatalError(string("Unable to find file [") + v1.GetString() + string("] in CopyFile"));
643
644
645
646 Variant v2 = plist->Nth(2);
647 PError::AssertType(v2, PEAT_STRING, "Argument error in function [CopyFile(<sourcefile>,<destfile>)]: ");
648
649
650 string destfile = v1.GetString();
651
652
653 //open files in binary mode so we can do a direct copy.
654
655 std::ifstream in(srcfile.c_str(), ios_base::in|ios_base::binary);
656 std::ofstream out(destfile.c_str(), ios_base::out|ios_base::binary);
657
658
659 if(!in.is_open())
660 {
661 PError::SignalWarning("Unable to open file ["+srcfile+"]. File not copied.");
662 }
663 if(!out.is_open())
664 {
665 PError::SignalFatalError("Unable to open file ["+destfile+"]. Check permissions or determine whether directory exists.");
666 }
667
668
669 char buf[BSIZE];
670 do {
671 in.read(&buf[0],BSIZE);
672 out.write(&buf[0],in.gcount());
673 }while (in.gcount()>0);
674
675 in.close();
676 out.close();
677 return Variant(1);
678}
679
680
681
682#ifdef PEBL_NETWORK
683
685{
686 //v[1] should have the IP Number as an integer
687 //v[2] should have the port number
688
689 PList * plist = v.GetComplexData()->GetList();
690 Variant v1 = plist->First();// plist->PopFront();
691 PError::AssertType(v1, PEAT_INTEGER, "Argument error in first parameter of function [ConnectToHost(<hostname>,<port>)]: ");
692
693 Variant v2 = plist->Nth(2);
694 PError::AssertType(v2, PEAT_INTEGER, "Argument error in second parameter of function [ConnectToHost(<hostname>,<port>)]: ");
695
696
698 PlatformNetwork * mynet = dynamic_cast<PlatformNetwork*>(tmp2.get());
699
700 mynet->Init();
701 mynet->SetHostIP((unsigned int)(long unsigned int)v1);
702 mynet->SetPort((int)v2);
703 mynet->Open();
704
705 PComplexData * pcd = new PComplexData(tmp2);
706 Variant tmp3 = Variant(pcd);
707 delete pcd;
708 pcd=NULL;
709 return tmp3;
710}
711
713{
714
715 //v[1] should have the host name
716 //v[2] should have the port number
717 PList * plist = v.GetComplexData()->GetList();
718 Variant v1 = plist->First();// plist->PopFront();
719 PError::AssertType(v1, PEAT_STRING, "Argument error in first parameter of function [ConnectToHost(<hostname>,<port>)]: ");
720
721 Variant v2 = plist->Nth(2);
722 PError::AssertType(v2, PEAT_INTEGER, "Argument error in second parameter of function [ConnectToHost(<hostname>,<port>)]: ");
723
724
726 PlatformNetwork * mynet = dynamic_cast<PlatformNetwork*>(tmp2.get());
727
728 mynet->Init();
729 mynet->SetHostName(v1);
730 mynet->SetPort((int)v2);
731
732
733 mynet->Open();
734
735 if(mynet->IsOpen())
736 {
737
738
739 PComplexData * pcd = new PComplexData(tmp2);
740 Variant tmp3 = Variant(pcd);
741 delete pcd;
742 pcd=NULL;
743 return tmp3;
744
745 }else{
746
747 return Variant(0);
748 }
749
750
751}
752
753
754
755
756
758{
759 //v[1] should have the port number
760 PList * plist = v.GetComplexData()->GetList();
761 Variant v1 = plist->First();// plist->PopFront();
762
763 PError::AssertType(v1, PEAT_INTEGER, "Argument error in first parameter of function [SetNetworkPort(<port>)]: ");
764
765
767 PlatformNetwork * mynet = dynamic_cast<PlatformNetwork*>(tmp2.get());
768 mynet->Init();
769 mynet->SetHostName("");
770 mynet->SetPort((int)v1);
771 // mynet->Accept();
772
773 PComplexData * pcd = new PComplexData(tmp2);
774 Variant tmp3 = Variant(pcd);
775 delete pcd;
776 pcd=NULL;
777 return tmp3;
778}
779
780
781// Once a listener has been opened, this will
782//see if anyone is knocking on the door.
783//
784
786{
787 //v[1] should have the network object which should have been opened but not yet
788 //accepted a connection
789 PList * plist = v.GetComplexData()->GetList();
790 Variant v1 = plist->First();// plist->PopFront();
791 PError::AssertType(v1, PEAT_NETWORKCONNECTION, "Argument error in first parameter of function [CheckFroNetworkConnection(<network>)]: ");
792 counted_ptr<PEBLObjectBase> tmp2 = (v1.GetComplexData())->GetObject();
793 PlatformNetwork * mynet = dynamic_cast<PlatformNetwork*>(tmp2.get());
794
795
796 bool result ; //
797
798 //This should already be initialized with a port and hostname.
799 //mynet->Init();
800 //mynet->SetHostName("");
801 //mynet->SetPort((int)v1);
802
803 result = mynet->CheckForConnection();
804
805 //Accept the connection if we have one:
806 if(result)
807 mynet->Accept(1000);
808
809 //PComplexData * pcd = new PComplexData(tmp2);
810 // Variant tmp3 = Variant(pcd);
811 // delete pcd;
812 // pcd=NULL;
813 // return tmp3;
814 return Variant(result);
815}
816
817
818
820{
821 PList * plist = v.GetComplexData()->GetList();
822 Variant v1 = plist->First();// plist->PopFront();
823
824 PError::AssertType(v1, PEAT_INTEGER, "Argument error in first parameter of function [OpenNetworkListener(<port>)]: ");
825
826
828 PlatformNetwork * mynet = dynamic_cast<PlatformNetwork*>(tmp2.get());
829 mynet->Init();
830 mynet->SetHostName("");
831 mynet->SetPort((int)v1);
832 mynet->CreateListener();
833
834 PComplexData * pcd = new PComplexData(tmp2);
835 Variant tmp3 = Variant(pcd);
836 //delete pcd;
837 //pcd=NULL;
838 return tmp3;
839}
840
841
842
844{
845 PList * plist = v.GetComplexData()->GetList();
846 Variant v1 = plist->First(); //plist->PopFront();
847 PError::AssertType(v1, PEAT_NETWORKCONNECTION, "Argument error in function [AcceptNetworkConnection(<network>,<timeout>)]: ");
848
849 counted_ptr<PEBLObjectBase> tmp2 = (v1.GetComplexData())->GetObject();
850 PlatformNetwork * mynet = dynamic_cast<PlatformNetwork*>(tmp2.get());
851
852 Variant timeout = plist->Nth(2); //plist->PopFront();
853
854 PError::AssertType(timeout, PEAT_INTEGER, "Argument error in second argument of function [AcceptNetworkConnection(<port>,<timeout>)]: ");
855
856 return Variant( mynet->Accept(timeout));
857
858}
859
860
861
862
864{
865
866 PList * plist = v.GetComplexData()->GetList();
867 Variant v1 = plist->First();// plist->PopFront();
868
869 PError::AssertType(v1, PEAT_INTEGER, "Argument error in first parameter of function [WaitForNetworkConnection(<port>)]: ");
870
871
873 PlatformNetwork * mynet = dynamic_cast<PlatformNetwork*>(tmp2.get());
874 mynet->Init();
875 mynet->SetHostName("");
876 mynet->SetPort((int)v1);
877
878 //The following waits forever, until a network connection
879 //opens. If you want more control, use Accept()
880 mynet->Accept();
881
882 PComplexData * pcd = new PComplexData(tmp2);
883 Variant tmp3 = Variant(pcd);
884 delete pcd;
885 pcd=NULL;
886 return tmp3;
887
888}
889
891{
892 //v[1] should have the network connection
893 PList * plist = v.GetComplexData()->GetList();
894 Variant v1 = plist->First(); //plist->PopFront();
895 PError::AssertType(v1, PEAT_NETWORKCONNECTION, "Argument error in function [CloseNetworkConnection(<network>)]: ");
896
897 counted_ptr<PEBLObjectBase> tmp2 = (v1.GetComplexData())->GetObject();
898 PlatformNetwork * mynet = dynamic_cast<PlatformNetwork*>(tmp2.get());
899 mynet->Close();
900
901 return Variant(1);
902
903}
904
906{
907 //v[1] should have the Network connection object
908 //v[2] should have the data
909 PList * plist = v.GetComplexData()->GetList();
910 Variant v1 = plist->First(); //plist->PopFront();
911 PError::AssertType(v1, PEAT_NETWORKCONNECTION, "Argument error in first parameter of function [SendData(<network>,<data>)]: ");
912 counted_ptr<PEBLObjectBase> tmp2 = (v1.GetComplexData())->GetObject();
913 PlatformNetwork * mynet = dynamic_cast<PlatformNetwork*>(tmp2.get());
914
915 Variant v2 = plist->Nth(2);
916 PError::AssertType(v2, PEAT_STRING, "Argument error in second parameter of function [SendData(<network>,<data>)]: ");
917
918 //int tries = 0; //Try to send up to 10 times
919 bool success = mynet->SendString(v2);
920
921 // struct timespec a,b;
922 // a.tv_sec = 0;
923 // a.tv_nsec = 10000000; //100 ms
924
925 // while(!success | ++tries<10)
926 // {
927 // success = mynet->SendString(v2);
928 // nanosleep(&a,&b);
929 // }
930
931 return Variant(success);
932}
933
935{
936 //v[1] should have the Network connection object
937 //v[2] should have the max length of the data we are looking for.
938
939 PList * plist = v.GetComplexData()->GetList();
940 Variant v1 = plist->First();// plist->PopFront();
941 PError::AssertType(v1, PEAT_NETWORKCONNECTION, "Argument error in first parameter of function [GetData(<network>,<size>)]: ");
942 counted_ptr<PEBLObjectBase> tmp2 = (v1.GetComplexData())->GetObject();
943 PlatformNetwork * mynet = dynamic_cast<PlatformNetwork*>(tmp2.get());
944
945 Variant v2 = plist->Nth(2);
946 PError::AssertType(v2, PEAT_INTEGER, "Argument error in second parameter of function [GetData(<network>,<size>)]: ");
947
948 //cout << "Trying to receive " << v2 << " bytes\n";
949 Variant ret = mynet->Receive(v2);
950 return ret;
951
952}
953
955{
956
957 //v[1] should have the network connection
958 PList * plist = v.GetComplexData()->GetList();
959 Variant v1 = plist->First();// plist->PopFront();
960 PError::AssertType(v1, PEAT_NETWORKCONNECTION, "Argument error in function [GetIPAddress(<network>)]: ");
961
962 counted_ptr<PEBLObjectBase> tmp2 = (v1.GetComplexData())->GetObject();
963 PlatformNetwork * mynet = dynamic_cast<PlatformNetwork*>(tmp2.get());
964 Variant add = mynet->GetIPAddress();
965
966 return add;
967}
968
969
971{
972
973 //v[1] should have the network connection
974 PList * plist = v.GetComplexData()->GetList();
975 Variant v1 = plist->First(); //plist->PopFront();
976 PError::AssertType(v1, PEAT_NETWORKCONNECTION, "Argument error in function [GetIPAddress(<network>)]: ");
977
978 counted_ptr<PEBLObjectBase> tmp2 = (v1.GetComplexData())->GetObject();
979 PlatformNetwork * mynet = dynamic_cast<PlatformNetwork*>(tmp2.get());
980 Variant add = mynet->GetMyIPAddress();
981
982 return add;
983}
984
985#else
986// Stub implementations when PEBL_NETWORK is not defined
987// These allow scripts to call network functions but they return appropriate "not available" values
988
990{
991 PError::SignalWarning("Network support not compiled in. ConnectToIP returning 0 (failed).");
992 return Variant(0);
993}
994
996{
997 PError::SignalWarning("Network support not compiled in. ConnectToHost returning 0 (failed).");
998 return Variant(0);
999}
1000
1002{
1003 PError::SignalWarning("Network support not compiled in. SetNetworkPort has no effect.");
1004 return Variant(0);
1005}
1006
1011
1013{
1014 PError::SignalWarning("Network support not compiled in. OpenNetworkListener returning 0 (failed).");
1015 return Variant(0);
1016}
1017
1022
1024{
1025 PError::SignalWarning("Network support not compiled in. WaitForNetworkConnection returning 0 (failed).");
1026 return Variant(0);
1027}
1028
1030{
1031 // Silently succeed - nothing to close
1032 return Variant(1);
1033}
1034
1036{
1037 return Variant(0);
1038}
1039
1041{
1042 return Variant("");
1043}
1044
1046{
1047 return Variant("0.0.0.0");
1048}
1049
1051{
1052 return Variant("0.0.0.0");
1053}
1054
1055#endif
1056
1057
1058#ifdef PEBL_HTTP
1059
1060//This gets text via http.
1062{
1063 PList * plist = v.GetComplexData()->GetList();
1064 Variant v1 = plist->First();
1065 PError::AssertType(v1, PEAT_STRING, "Error in first argument of function [GetHTTPFile(<host>,<port>,<fileurl>,<savename>)]: ");
1066
1067 Variant port = plist->Nth(2);
1068 PError::AssertType(port, PEAT_INTEGER, "Error in second argument of function [GetHTTPFile(<host>,<port>,<fileurl>,<savename>)]: ");
1069
1070
1071 Variant v3 = plist->Nth(3);
1072 PError::AssertType(v3, PEAT_STRING, "Error in third argument of function [GetHTTPFile(<host>,<port>,<fileurl>,<savename>)]: ");
1073
1074 Variant v4 = plist->Nth(4);
1075 PError::AssertType(v4, PEAT_STRING, "Error in fourth argument function [GetHTTPFile(<host>,<port>,<fileurl>,<savename>)]: ");
1076
1077 PEBLHTTP http(v1,port);
1078
1079 int out = http.GetHTTPFile(v3,v4);
1080 unsigned int status = http.GetStatus();
1081
1082 return Variant((int)status);
1083}
1084
1085
1086//This gets text via http.
1088{
1089 PList * plist = v.GetComplexData()->GetList();
1090 Variant v1 = plist->First();
1091 PError::AssertType(v1, PEAT_STRING, "Error in first argument of function [GetHTTPText(<host>,<port>,<fileurl>)]: ");
1092
1093 Variant port = plist->Nth(2);
1094 PError::AssertType(port, PEAT_INTEGER, "Error in second argument of function [GetHTTPText(<host>,<port>,<fileurl>)]: ");
1095
1096 Variant v3 = plist->Nth(3);
1097 PError::AssertType(v3, PEAT_STRING, "Error in third argument of function [GetHTTPText(<host>,<port>,<fileurl>)]: ");
1098
1099 PEBLHTTP http(v1,port);
1100
1101 std::string out = http.GetHTTPText(v3);
1102 unsigned int status = http.GetStatus();
1103
1104
1105 //return a pair; the status code + the text
1106
1107
1108 PList * returnlist = new PList();
1109 returnlist->PushBack(Variant((int)status));
1110 returnlist->PushBack(Variant(out));
1112 PComplexData * pcd = new PComplexData(tmp2);
1113 Variant tmp3 = Variant(pcd);
1114 delete pcd;
1115 pcd=NULL;
1116 return tmp3;
1117}
1118
1119
1120
1122{
1123 PList * plist = v.GetComplexData()->GetList();
1124 Variant v1 = plist->First(); //plist->PopFront();
1125 PError::AssertType(v1, PEAT_STRING, "Error in first argument of function [PostHTTP(<host>,<port>,<page>,[<list>,<of>,<headers>],<body-args>)]: ");
1126
1127 Variant port = plist->Nth(2);
1128 PError::AssertType(port, PEAT_INTEGER, "Error in second argument of function [PostHTTP(<host>,<port>,<page>,[<list>,<of>,<headers>],<body-args>)]: ");
1129
1130 Variant v3 = plist->Nth(3);
1131 PError::AssertType(v3, PEAT_STRING, "Error in third argument of function [PostHTTP(<host>,<port>,<page>,[<list>,<of>,<headers>],<body-args>)]: ");
1132
1133
1134 Variant v4 = plist->Nth(4);
1135 PError::AssertType(v4, PEAT_LIST, "Error in fourth argument of function [PostHTTP(<host>,<port>,<page>,[<list>,<of>,<headers>],<body-args>)]: ");
1136
1137 Variant v5 = plist->Nth(5);
1138 PError::AssertType(v5, PEAT_STRING, "Error in fifth argument of function [PostHTTP(<host>,<port>,<page>,[<list>,<of>,<headers>],<body-args>)]: ");
1139
1140
1141
1142 PEBLHTTP http(v1,port);
1143
1144
1145
1146 Variant out = http.PostHTTP(v3,v4,v5);
1147 return out;
1148}
1149
1150
1151
1153{
1154 PList * plist = v.GetComplexData()->GetList();
1155 Variant v1 = plist->First(); //plist->PopFront();
1156 PError::AssertType(v1, PEAT_STRING, "Error in first argument of function [PostHTTPFile(<host>,<port>,<page>,<list-of-args>,<filename>,<field>)]: ");
1157
1158 Variant port = plist->Nth(2);
1159 PError::AssertType(port, PEAT_INTEGER, "Error in second argument of function [PostHTTPFile(<host>,<port>,<page>,<list-of-args>,<filename>,<field>)]: ");
1160
1161 Variant page = plist->Nth(3);
1162 PError::AssertType(page, PEAT_STRING, "Error in third argument of function [PostHTTPFile(<host>,<port>,<page>,<list-of-args>,<filename>,<field>)]: ");
1163
1164
1165 Variant args = plist->Nth(4);
1166 PError::AssertType(args, PEAT_LIST, "Error in fourth argument of function [PostHTTPFile(<host>,<port>,<page>,<list-of-args>,<filename>,<field>)]: ");
1167
1168
1169 Variant fname = plist->Nth(5);
1170 PError::AssertType(fname, PEAT_STRING, "Error in Fifth argument of function [PostHTTPFile(<host>,<port>,<page>,<list_of_args>,<filename>,<field>]: ");
1171
1172
1173 Variant field = plist->Nth(6);
1174 PError::AssertType(field, PEAT_STRING, "Error in Fifth argument of function [PostHTTPFile(<host>,<port>,<page>,<list-of-args>,<filename>,<field>]: ");
1175
1176
1177 PEBLHTTP http(v1,port);
1178
1179
1180 Variant out = http.PostMulti(page,args,fname,field);
1181
1182
1183 return out;
1184}
1185
1186#else
1187// Stub implementations when PEBL_HTTP is not defined
1188// These allow scripts to call HTTP functions but they return appropriate "not available" values
1189
1191{
1192 // Return 503 Service Unavailable status code
1193 return Variant(503);
1194}
1195
1197{
1198 // Return [503, ""] list (status code 503, empty text)
1199 PList * returnlist = new PList();
1200 returnlist->PushBack(Variant(503));
1201 returnlist->PushBack(Variant(""));
1203 PComplexData * pcd = new PComplexData(tmp2);
1204 Variant tmp3 = Variant(pcd);
1205 delete pcd;
1206 pcd=NULL;
1207 return tmp3;
1208}
1209
1211{
1212 // Return 503 Service Unavailable
1213 return Variant(503);
1214}
1215
1217{
1218 // Return 503 Service Unavailable
1219 return Variant(503);
1220}
1221
1222#endif
1223
1224
1226{
1227 PList * plist = v.GetComplexData()->GetList();
1228 Variant v1 = plist->First();// plist->PopFront();
1229 PError::AssertType(v1, PEAT_STRING, "Argument error in first parameter of function [MD5Sum(<text>)]:");
1230 std::string sum = PEBLUtility::MD5String(v1);
1231 return Variant(sum);
1232}
1233
1234
1236{
1237 PList * plist = v.GetComplexData()->GetList();
1238 Variant v1 = plist->First();// plist->PopFront();
1239 PError::AssertType(v1, PEAT_STRING, "Argument error in first parameter of function [MD5File(<fname>)]:");
1240 std::string sum = PEBLUtility::MD5File(v1);
1241 return Variant(sum);
1242}
1243
1244
1246{
1247 //v[1] should have an object/window name.
1248 //v[2] should have a filename
1249
1250 PList * plist = v.GetComplexData()->GetList();
1251 Variant v1 = plist->First();// plist->PopFront();
1252 PError::AssertType(v1, PEAT_STRING, "Argument error in first parameter of function [WritePNG(<filename>,<object>,<range>)]: ");
1253
1254
1255 Variant v2 = plist->Nth(2);
1256 PError::AssertType(v2, PEAT_WINDOW, "Argument error in second parameter of function [WritePNG(<filename>,<object>,<range>)]: ");
1257 counted_ptr<PEBLObjectBase> tmp2 = (v2.GetComplexData())->GetObject();
1258
1259
1260 Variant v3 = plist->Nth(3);
1261
1262
1263 PError::AssertType(v3, PEAT_LIST, "Argument error in third parameter of function [WritePNG(<filename>,<object>,<range>)]: ");
1264 // counted_ptr<PEBLObjectBase> tmp3 = (range.GetComplexData())->GetObject();
1265
1266 PList * range = (PList*)(v3.GetComplexData()->GetObject().get());
1267
1268
1269
1270 PlatformWindow * myWin = dynamic_cast<PlatformWindow*>(tmp2.get());
1271
1272 //range must have four numeric values
1273 //start off with entire window.
1274 int x=0;
1275 int y=0;
1276 int w = myWin->GetWidth();
1277 int h = myWin->GetHeight();
1278
1279 //if third argument is properly formed, select the subrange
1280 long int length = range->Length();
1281 if(length == 4)
1282 {
1283 int newx=range->Nth(1);
1284 int newy=range->Nth(2);
1285 int neww=range->Nth(3);
1286 int newh=range->Nth(4);
1287
1288 if(newx<0 || newx > myWin->GetWidth() ||
1289 newy < 0 || newy > myWin->GetHeight() ||
1290 neww < 0 || (neww + newx ) > myWin->GetWidth() ||
1291 newh < 0 || (newh + newy) > myWin->GetHeight())
1292 {
1293 PError::SignalWarning("Screenshot bounds outside range of screen. Using entire screen");
1294 } else
1295 {
1296 x = newx;
1297 y = newy;
1298 h = newh;
1299 w = neww;
1300
1301 }
1302
1303
1304 }
1305
1306
1307
1308
1309 int result = myWin->SaveScreenShot(x,y,w,h,v1);
1310
1311
1312 if(result != 0)
1313 {
1314 PError::SignalWarning(Variant("Failed to write png file ") + v2);
1315 }
1316
1317 return Variant(result);
1318}
1319
1320
1321
1322
1324{
1325 PList * plist = v.GetComplexData()->GetList();
1326 Variant v1 = plist->First();
1327 PError::AssertType(v1, PEAT_STRING, "Argument error in first parameter of function [ParseJSON(<json-text>)]: ");
1328
1330 return out;
1331}
1332
1333
1334
1335#ifdef PEBL_USEPORTS
1336Variant PEBLStream::OpenPPort(Variant v)
1337{
1338 //v[1] should have a text string indicating
1339 //lpt1/lpt2/lpt3 (which may not really map onto
1340 //those ports for any individual system.
1341
1342
1343 PList * plist = v.GetComplexData()->GetList();
1344 Variant v1 = plist->First();// plist->PopFront();
1345 PError::AssertType(v1, PEAT_STRING, "Argument error in first parameter of function [OpenPPort(<port>)]: ");
1346
1347 unsigned char data; /* value of the data register */
1348 unsigned char control; /* value of the control register */
1349
1350#if 0
1351 if(0)
1352 {
1353 //hardcode some parport stuff here for testing.
1354 if (iopl(3))
1355 printf("Could't get the port addresses\n");
1356
1357
1358
1359 /* enable data register output by bringing bit 5 of control */
1360 /* register low */
1361
1362 control = inb(BASE + 2);
1363 //cout << "control byte:" << (int)control << endl;
1364 outb(control & ~0x20, BASE + 2);
1365
1366 /* write the value */
1367 outb((unsigned char) 255, BASE);
1368
1369 /* Wait a few secs, so we can see it.*/
1370 struct timespec a,b;
1371 a.tv_sec = 5;
1372 a.tv_nsec = 100000; //1 ms
1373 int retval = nanosleep(&a,&b);
1374
1375 /* disable data register output by bringing bit 5 of control */
1376 /* register high */
1377
1378 control = inb(BASE + 2);
1379 //cout << "control byte:" << (int)control << endl;
1380 outb(control | 0x20, BASE + 2);
1381
1382
1383 /* read and print the value */
1384 //printf("Port 0x%x reads 0x%x\n", BASE, inb(BASE));
1385 }
1386#endif
1387
1389 PParallelPort * pport = dynamic_cast<PParallelPort*>(tmp2.get());
1390
1391
1392 pport->SetPort(v1);
1393 pport->Init();
1394
1395
1396 PComplexData * pcd = new PComplexData(tmp2);
1397 Variant tmp3 = Variant(pcd);
1398 delete pcd;
1399 pcd=NULL;
1400 return tmp3;
1401
1402}
1403
1404
1405//This sets the pport state on the data bits
1406Variant PEBLStream::SetPPortState(Variant v)
1407{
1408 //v[1] should have an pport
1409 //v[2] should have an integer, interpreted as a list of bits 0/1
1410
1411 PList * plist = v.GetComplexData()->GetList();
1412 Variant v1 = plist->First();// plist->PopFront();
1413 PError::AssertType(v1, PEAT_PARALLELPORT, "Argument error in first parameter of function [SetPPortState(<pport>,<integer>)]: ");
1414
1415 counted_ptr<PEBLObjectBase> tmp1 = (v1.GetComplexData())->GetObject();
1416 PParallelPort * mypport = dynamic_cast<PParallelPort*>(tmp1.get());
1417
1418
1419 Variant v2 = plist->Nth(2);
1420 PError::AssertType(v2, PEAT_INTEGER, "Argument error in second parameter of function [SetPPortState(<pport>,<integer>)]: ");
1421
1422 char x = (int)v2;
1423 mypport->SetDataState(x);
1424
1425
1426 return Variant(true);
1427}
1428
1429Variant ConvertByteToList(int x)
1430{
1431 int d = 8;
1432 // Variant buffer="";
1433
1434 PList *list = new PList();
1435 for (;d>0;d--)
1436 {
1437 list->PushBack(Variant((int)(x & 1)));
1438 //buffer = buffer + Variant("|") + Variant((int)(x & 1));
1439 x >>= 1;
1440
1441 }
1442
1443 //buffer = buffer + Variant("|");
1444
1446 PComplexData * tmpPCD= (new PComplexData(tmplist));
1447 Variant tmp = Variant(tmpPCD);
1448 delete tmpPCD;
1449 tmpPCD=NULL;
1450 return tmp;
1451
1452}
1453
1454Variant PrintByte(int x)
1455{
1456 int d = 8;
1457 Variant buffer="";
1458
1459 for (;d>0;d--)
1460 {
1461 buffer = buffer + Variant("|") + Variant((int)(x & 1));
1462 x >>= 1;
1463
1464 }
1465
1466 buffer = buffer + Variant("|");
1467
1468 return buffer;
1469}
1470
1471// This gets the state of the parallel port. It may be platform-specific.
1472//
1473Variant PEBLStream::GetPPortState(Variant v)
1474{
1475 //v[1] should have an pport
1476
1477 PList * plist = v.GetComplexData()->GetList();
1478 Variant v1 = plist->First();// plist->PopFront();
1479 PError::AssertType(v1, PEAT_PARALLELPORT, "Argument error in first parameter of function [GetPPortState(<pport>]: ");
1480
1481 counted_ptr<PEBLObjectBase> tmp1 = (v1.GetComplexData())->GetObject();
1482 PParallelPort * mypport = dynamic_cast<PParallelPort*>(tmp1.get());
1483
1484
1485 char x1 = mypport->GetDataState();
1486
1487 Variant bytelist = ConvertByteToList(x1);
1488
1489 return bytelist;
1490}
1491
1492
1493
1494// This sets the state of the parallel port to either input or output.
1495//
1496Variant PEBLStream::SetPPortMode(Variant v)
1497{
1498 PList * plist = v.GetComplexData()->GetList();
1499 Variant v1 = plist->First(); //plist->PopFront();
1500 PError::AssertType(v1, PEAT_PARALLELPORT, "Argument error in first parameter of function [SetPPortMode(<pport>,<mode>)]: ");
1501
1502 counted_ptr<PEBLObjectBase> tmp1 = (v1.GetComplexData())->GetObject();
1503 PParallelPort * mypport = dynamic_cast<PParallelPort*>(tmp1.get());
1504
1505
1506 Variant v2 = plist->Nth(2);
1507 PError::AssertType(v2, PEAT_STRING, "Argument error in second parameter of function [SetPPortState(<pport>,<mode>)]: ");
1508
1509 if(v2 == "<input>")
1510 mypport->SetInputMode();
1511 else if(v2 == "<output>")
1512 mypport->SetOutputMode();
1513 else
1514 PError::SignalFatalError(Variant("Unknown mode type [")+v2+
1515 Variant("] in SetPPortState. Must be <input> or <output>."));
1516
1517 return Variant(true);
1518}
1519
1520
1521#if 0
1522// This waits for a change in the PPORT state (on the data bits)
1523//
1524Variant PEBLStream::WaitForPPortData(Variant v)
1525{
1526 PList * plist = v.GetComplexData()->GetList();
1527 Variant v1 = plist->First();// plist->PopFront();
1528 PError::AssertType(v1, PEAT_PARALLELPORT, "Argument error in first parameter of function [WaitForPPortData(<pport>)]: ");
1529
1530 counted_ptr<PEBLObjectBase> tmp1 = (v1.GetComplexData())->GetObject();
1531 PParallelPort * mypport = dynamic_cast<PParallelPort*>(tmp1.get());
1532
1533 //Create a pport test correspending to keydown.
1534 //1 is the value (down), DT_EQUAL is the test, key is the interface (e.g., the 'A' key)
1535 PDevice * device = new PParallelPort(mypport);
1536 ValueState * state = new ValueState(1, DT_EQUAL, key, device, PDT_KEYBOARD);
1537
1538 //NULL,NULL will terminate the looping
1539 string funcname = "";
1540 PList* params = NULL;
1541 Evaluator::mEventLoop.RegisterState(state,funcname, params);
1542 PEvent returnval = Evaluator::mEventLoop.Loop();
1543
1544 //Now, clear the event loop tests
1546
1547 return Variant(returnval.GetDummyEvent().value);
1548
1549}
1550
1551
1552// This waits for a change in the PPORT state (on the status bits)
1553//
1554Variant PEBLStream::WaitForPPortStatus(Variant v)
1555{
1556 PList * plist = v.GetComplexData()->GetList();
1557 Variant v1 = plist->First(); //plist->PopFront();
1558 PError::AssertType(v1, PEAT_PARALLELPORT, "Argument error in first parameter of function [SetPPortMode(<pport>,<mode>)]: ");
1559
1560 counted_ptr<PEBLObjectBase> tmp1 = (v1.GetComplexData())->GetObject();
1561 PParallelPort * mypport = dynamic_cast<PParallelPort*>(tmp1.get());
1562
1563
1564 Variant v2 = plist->Nth(2);
1565 PError::AssertType(v2, PEAT_STRING, "Argument error in second parameter of function [SetPPortState(<pport>,<mode>)]: ");
1566
1567 if(v2 == "<input>")
1568 mypport->SetInputMode();
1569 else if(v2 == "<output>")
1570 mypport->SetOutputMode();
1571 else
1572 PError::SignalFatalError(Variant("Unknown mode type [")+v2+
1573 Variant("] in SetPPortState. Must be <input> or <output>."));
1574
1575 return Variant(true);
1576}
1577#endif
1578
1579
1580// This sets the state of the parallel port to either input or output.
1581//
1582Variant PEBLStream::OpenComPort(Variant v)
1583{
1584
1585 PList * plist = v.GetComplexData()->GetList();
1586 Variant v1 = plist->First();// plist->PopFront();
1587 PError::AssertType(v1, PEAT_INTEGER, "Argument error in first parameter of function [OpenComPort(<portnumber>,<baud>,opt:<mode>)]: ");
1588
1589
1590
1591 Variant v2 = plist->Nth(2); //plist->PopFront();
1592 PError::AssertType(v1, PEAT_INTEGER, "Argument error in second parameter of function [OpenComPort(<portnumber>),<baud>,opt:<mode>]: ");
1593
1594 std::string mode = "8N1";
1595 if(plist->Length()>2)
1596 {
1597 PError::AssertType(plist->Nth(2), PEAT_STRING, "Argument error in third parameter of function [OpenComPort(<portnumber>),<baud>,opt:<mode>]: ");
1598
1599 std::string mode = plist->Nth(3);
1600
1601 }
1602
1603
1604
1605
1606 counted_ptr<PEBLObjectBase> tmp2 = counted_ptr<PEBLObjectBase>(new PComPort((int)v1, (int)v2,mode));
1607 PComPort * cport = dynamic_cast<PComPort*>(tmp2.get());
1608
1609 cport->Init();
1610
1611
1612 PComplexData * pcd = new PComplexData(tmp2);
1613 Variant tmp3 = Variant(pcd);
1614 delete pcd;
1615 pcd=NULL;
1616 return tmp3;
1617}
1618
1619
1620
1621//
1622//
1623Variant PEBLStream::ComPortGetByte(Variant v)
1624{
1625 PList * plist = v.GetComplexData()->GetList();
1626 Variant v1 = plist->First();// plist->PopFront();
1627 PError::AssertType(v1, PEAT_COMPORT, "Argument error in first parameter of function [ComPortGetByte(<port>)]: ");
1628
1629 counted_ptr<PEBLObjectBase> tmp1 = (v1.GetComplexData())->GetObject();
1630 PComPort * cport = dynamic_cast<PComPort*>(tmp1.get());
1631
1632 unsigned char out;
1633 int retval= cport->GetByte(out);
1634 if(retval==0)
1635 return Variant(-1);
1636 else
1637 return Variant((int)out);
1638}
1639
1640//
1641//
1642Variant PEBLStream::ComPortSendByte(Variant v)
1643{
1644 PList * plist = v.GetComplexData()->GetList();
1645 Variant v1 = plist->First(); //plist->PopFront();
1646 PError::AssertType(v1, PEAT_COMPORT, "Argument error in first parameter of function [ComPortSendByte(<port>,<int>)]: ");
1647
1648 Variant v2 = plist->Nth(2);
1649 //Second argumentcould be a bypte or a character.
1650 // PError::AssertType(v2,PEAT_INTEGER,"Argument error in second parameter of function [ComPortSendByte(<port>,<int>)]: ");
1651
1652
1653 counted_ptr<PEBLObjectBase> tmp1 = (v1.GetComplexData())->GetObject();
1654 PComPort * cport = dynamic_cast<PComPort*>(tmp1.get());
1655
1656 unsigned char send = 0;
1657 if(v2.IsInteger())
1658 {
1659 send = v2.GetInteger();
1660 } else if(v2.IsString())
1661 {
1662
1663 std::string tmp = (v2.GetString());
1664 send = tmp[0];
1665 }
1666
1667 cport->PSendByte(send);
1668
1669 return Variant(0);
1670}
1671
1672
1673
1674
1675#endif
#define NULL
Definition BinReloc.cpp:317
@ DT_EQUAL
Definition DeviceState.h:43
@ PDT_KEYBOARD
Definition PDevice.h:42
#define BASE
@ PEAT_NETWORKCONNECTION
Definition PError.h:62
@ PEAT_COMPORT
Definition PError.h:65
@ PEAT_LIST
Definition PError.h:61
@ PEAT_PARALLELPORT
Definition PError.h:64
@ PEAT_INTEGER
Definition PError.h:44
@ PEAT_WINDOW
Definition PError.h:70
@ PEAT_STRING
Definition PError.h:46
@ PEAT_FILESTREAM
Definition PError.h:56
@ sdAppend
Definition PStream.h:40
@ sdWrite
Definition PStream.h:39
@ sdRead
Definition PStream.h:38
@ stASCII
Definition PStream.h:47
static PEBLPath gPath
static PEventLoop * mEventLoop
counted_ptr< PEBLObjectBase > GetObject() const
PList * GetList() const
std::string FindFile(const string &filename)
Definition PEBLPath.cpp:368
PEvent Loop()
Initiates the looping tests.
void RegisterState(DeviceState *state, const std::string &function, Variant parameters)
PEBL_DummyEvent GetDummyEvent() const
Definition PEvent.cpp:414
Definition PList.h:45
Variant Nth(unsigned int n)
Definition PList.cpp:181
unsigned long Length() const
Definition PList.h:89
void PushBack(const Variant &v)
Definition PList.cpp:149
Variant First()
Definition PList.cpp:169
virtual void SetHostIP(unsigned int host)
Definition PNetwork.cpp:81
virtual bool IsOpen()
Definition PNetwork.cpp:119
virtual void SetHostName(std::string hostname)
Definition PNetwork.cpp:97
virtual void SetPort(unsigned int port)
Definition PNetwork.cpp:108
virtual void SetDataState(char x)
virtual void Init()
virtual void SetOutputMode()
virtual void SetPort(Variant v)
virtual void SetInputMode()
virtual char GetDataState()
bool Eof()
Definition PStream.cpp:388
std::string ReadLineClean()
Definition PStream.cpp:323
std::string ReadToken(const char separator)
This reads up until the next separator token (or eof character)
Definition PStream.cpp:251
void WriteString(const std::string &buffer)
This method just writes the char* string to the stream.
Definition PStream.cpp:211
bool Eol()
Definition PStream.cpp:356
bool Close()
Definition PStream.cpp:394
std::string ReadLine()
Definition PStream.cpp:290
char ReadChar()
Definition PStream.cpp:230
int SaveScreenShot(int x, int y, int w, int h, const Variant fname)
virtual pInt GetHeight() const
virtual pInt GetWidth() const
pInt GetInteger() const
Definition Variant.cpp:997
bool IsString() const
Definition Variant.cpp:948
std::string GetString() const
Definition Variant.cpp:1056
bool IsInteger() const
Definition Variant.cpp:942
PComplexData * GetComplexData() const
Definition Variant.cpp:1299
X * get() const
Definition rc_ptrs.h:110
Variant SendData(Variant v)
Variant GetData(Variant v)
Variant AcceptNetworkConnection(Variant v)
Variant ConnectToHost(Variant v)
Variant FileReadText(Variant v)
This will read all of the text in a file into a single variant.
Variant GetMyIPAddress(Variant v)
Variant PostHTTPFile(Variant v)
Variant FileOpenAppend(Variant v)
This opens a filestream for writing, appending to end of file.
Variant FileReadWord(Variant v)
Variant GetIPAddress(Variant v)
Variant FileReadLine(Variant v)
Reads until the next line.
Variant FilePrint_(Variant v)
This prints contents of variant to file, without appending a newline.
Variant CopyFile(Variant v)
Variant FileOpenOverwrite(Variant v)
This opens a filestream for writing, replacing current file, without making a backup.
Variant FileReadCharacter(Variant v)
Reads a character from a file.
Variant EndOfFile(Variant v)
This detects if you are at the end of a file.
Variant WritePNG(Variant v)
Variant GetHTTPFile(Variant v)
Variant FileClose(Variant v)
This Closes a filestream.
Variant FileReadTable(Variant v)
Makes a list of lists, with each row contained in a list.
Variant EndOfLine(Variant v)
This detects if you are at the end of a line.
Variant FileOpenRead(Variant v)
This opens a filestream for reading.
Variant MD5File(Variant v)
Variant OpenNetworkListener(Variant v)
Variant GetHTTPText(Variant v)
Variant SetNetworkPort(Variant v)
Variant FileOpenWrite(Variant v)
This opens a filestream for writing.
Variant ConnectToIP(Variant v)
Variant CheckForNetworkConnection(Variant v)
Variant ParseJSON(Variant v)
Variant PostHTTP(Variant v)
Variant CloseNetworkConnection(Variant v)
Variant AppendFile(Variant v)
Variant WaitForNetworkConnection(Variant v)
Variant MD5Sum(Variant v)
Variant Format(Variant v)
Variant Print(Variant v)
This prints the argument with a newline character.
Variant Print_(Variant v)
This prints the argument without a linebreak at the end.
Variant FilePrint(Variant v)
This prints contents of variant to file, followed by a newline.
Variant FileReadList(Variant v)
Places all words in file into a list.
Variant ParseJSON(const std::string &text)
std::string MD5String(const std::string &text)
std::string MD5File(const std::string &filename)
Variant FileExists(std::string path)
Variant Tokenize(const char *line, char separator)
void SignalWarning(const std::string &message)
Definition PError.cpp:119
void AssertType(Variant v, int type, const std::string &outsidemessage)
void SignalFatalError(const std::string &message)