aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2011-06-03 07:18:23 +0000
committerMike Buland <eichlan@xagasoft.com>2011-06-03 07:18:23 +0000
commitf16f239688b632fc54684c3e0e1430fd89a67db5 (patch)
tree6a6d4f3b5d5f4974af4c3cc47c7892e46c5ef201 /src
parent4f1e5849d4a48eb674d81164ba4baba4ad51a89f (diff)
downloadbuild-f16f239688b632fc54684c3e0e1430fd89a67db5.tar.gz
build-f16f239688b632fc54684c3e0e1430fd89a67db5.tar.bz2
build-f16f239688b632fc54684c3e0e1430fd89a67db5.tar.xz
build-f16f239688b632fc54684c3e0e1430fd89a67db5.zip
I added basic support for "opaque" type variables. I think there's one more
tweak to it that I would like to make, but it's fine for now. I also added open, close, read, and write functions. They work just fine, but I'll also add a readLine function, and maybe even a readToken function later.
Diffstat (limited to '')
-rw-r--r--src/filemgr.cpp46
-rw-r--r--src/filemgr.h26
-rw-r--r--src/functionclose.cpp26
-rw-r--r--src/functionclose.h17
-rw-r--r--src/functionopen.cpp42
-rw-r--r--src/functionopen.h17
-rw-r--r--src/functionplugger.cpp9
-rw-r--r--src/functionread.cpp40
-rw-r--r--src/functionread.h17
-rw-r--r--src/functionwrite.cpp34
-rw-r--r--src/functionwrite.h17
-rw-r--r--src/variable.cpp67
-rw-r--r--src/variable.h7
13 files changed, 363 insertions, 2 deletions
diff --git a/src/filemgr.cpp b/src/filemgr.cpp
new file mode 100644
index 0000000..00f0410
--- /dev/null
+++ b/src/filemgr.cpp
@@ -0,0 +1,46 @@
1#include "filemgr.h"
2
3FileMgr::FileMgr() :
4 iNextId( 1 )
5{
6}
7
8FileMgr::~FileMgr()
9{
10 for( FileHash::iterator i = hFile.begin(); i; i++ )
11 {
12 delete *i;
13 }
14}
15
16int FileMgr::open( const Bu::String &sPath, int iMode )
17{
18 hFile.insert( iNextId, new Bu::File( sPath, iMode ) );
19 return iNextId++;
20}
21
22Bu::File &FileMgr::get( int iId )
23{
24 try
25 {
26 return *hFile.get( iId );
27 }
28 catch(...)
29 {
30 throw Bu::ExceptionBase("Invalid file handle accessed.");
31 }
32}
33
34void FileMgr::close( int iId )
35{
36 try
37 {
38 delete hFile.get( iId );
39 hFile.erase( iId );
40 }
41 catch(...)
42 {
43 throw Bu::ExceptionBase("Invalid file handle accessed.");
44 }
45}
46
diff --git a/src/filemgr.h b/src/filemgr.h
new file mode 100644
index 0000000..517e784
--- /dev/null
+++ b/src/filemgr.h
@@ -0,0 +1,26 @@
1#ifndef FILE_MGR_H
2#define FILE_MGR_H
3
4#include <bu/singleton.h>
5#include <bu/hash.h>
6#include <bu/file.h>
7
8class FileMgr : public Bu::Singleton<FileMgr>
9{
10friend class Bu::Singleton<FileMgr>;
11private:
12 FileMgr();
13 virtual ~FileMgr();
14
15public:
16 int open( const Bu::String &sPath, int iMode );
17 Bu::File &get( int iId );
18 void close( int iId );
19
20private:
21 typedef Bu::Hash<int, Bu::File *> FileHash;
22 FileHash hFile;
23 int iNextId;
24};
25
26#endif
diff --git a/src/functionclose.cpp b/src/functionclose.cpp
new file mode 100644
index 0000000..ac9c6f6
--- /dev/null
+++ b/src/functionclose.cpp
@@ -0,0 +1,26 @@
1#include "functionclose.h"
2#include "filemgr.h"
3
4#include <bu/plugger.h>
5PluginInterface3( pluginFunctionClose, close, FunctionClose, Function,
6 "Mike Buland", 0, 1 );
7
8FunctionClose::FunctionClose()
9{
10}
11
12FunctionClose::~FunctionClose()
13{
14}
15
16Bu::String FunctionClose::getName() const
17{
18 return "close";
19}
20
21Variable FunctionClose::call( Variable &input, VarList )
22{
23 FileMgr::getInstance().close( (int)input.getOpaque() );
24 return Variable();
25}
26
diff --git a/src/functionclose.h b/src/functionclose.h
new file mode 100644
index 0000000..0a30427
--- /dev/null
+++ b/src/functionclose.h
@@ -0,0 +1,17 @@
1#ifndef FUNCTION_CLOSE_H
2#define FUNCTION_CLOSE_H
3
4#include "function.h"
5
6class FunctionClose : public Function
7{
8public:
9 FunctionClose();
10 virtual ~FunctionClose();
11
12 virtual Bu::String getName() const;
13 virtual Variable call( Variable &input, VarList lParams );
14
15};
16
17#endif
diff --git a/src/functionopen.cpp b/src/functionopen.cpp
new file mode 100644
index 0000000..4026a8c
--- /dev/null
+++ b/src/functionopen.cpp
@@ -0,0 +1,42 @@
1#include "functionopen.h"
2#include "filemgr.h"
3
4#include <bu/plugger.h>
5PluginInterface3( pluginFunctionOpen, open, FunctionOpen, Function,
6 "Mike Buland", 0, 1 );
7
8FunctionOpen::FunctionOpen()
9{
10}
11
12FunctionOpen::~FunctionOpen()
13{
14}
15
16Bu::String FunctionOpen::getName() const
17{
18 return "open";
19}
20
21Variable FunctionOpen::call( Variable &input, VarList lParams )
22{
23 if( lParams.getSize() != 2 )
24 {
25 throw Bu::ExceptionBase(
26 "open takes two parameters, filename and mode."
27 );
28 }
29 Bu::String sMode = lParams.last().toString().toLower();
30 int iMode = Bu::File::Create;
31 if( sMode.find('w') )
32 iMode |= Bu::File::Write;
33 if( sMode.find('r') )
34 iMode |= Bu::File::Read;
35 Variable vRet(
36 (void *)FileMgr::getInstance().open(
37 lParams.first().toString(), iMode
38 )
39 );
40 return vRet;
41}
42
diff --git a/src/functionopen.h b/src/functionopen.h
new file mode 100644
index 0000000..5ab3cab
--- /dev/null
+++ b/src/functionopen.h
@@ -0,0 +1,17 @@
1#ifndef FUNCTION_OPEN_H
2#define FUNCTION_OPEN_H
3
4#include "function.h"
5
6class FunctionOpen : public Function
7{
8public:
9 FunctionOpen();
10 virtual ~FunctionOpen();
11
12 virtual Bu::String getName() const;
13 virtual Variable call( Variable &input, VarList lParams );
14
15};
16
17#endif
diff --git a/src/functionplugger.cpp b/src/functionplugger.cpp
index c7b270c..a7b4cf5 100644
--- a/src/functionplugger.cpp
+++ b/src/functionplugger.cpp
@@ -17,6 +17,10 @@ extern Bu::PluginInfo pluginFunctionToString;
17extern Bu::PluginInfo pluginFunctionUnlink; 17extern Bu::PluginInfo pluginFunctionUnlink;
18extern Bu::PluginInfo pluginFunctionRegEx; 18extern Bu::PluginInfo pluginFunctionRegEx;
19extern Bu::PluginInfo pluginFunctionRange; 19extern Bu::PluginInfo pluginFunctionRange;
20extern Bu::PluginInfo pluginFunctionOpen;
21extern Bu::PluginInfo pluginFunctionClose;
22extern Bu::PluginInfo pluginFunctionRead;
23extern Bu::PluginInfo pluginFunctionWrite;
20 24
21FunctionPlugger::FunctionPlugger() 25FunctionPlugger::FunctionPlugger()
22{ 26{
@@ -33,7 +37,10 @@ FunctionPlugger::FunctionPlugger()
33 registerBuiltinPlugin( &pluginFunctionToString ); 37 registerBuiltinPlugin( &pluginFunctionToString );
34 registerBuiltinPlugin( &pluginFunctionUnlink ); 38 registerBuiltinPlugin( &pluginFunctionUnlink );
35 registerBuiltinPlugin( &pluginFunctionRegEx ); 39 registerBuiltinPlugin( &pluginFunctionRegEx );
36 registerBuiltinPlugin( &pluginFunctionRange ); 40 registerBuiltinPlugin( &pluginFunctionOpen );
41 registerBuiltinPlugin( &pluginFunctionClose );
42 registerBuiltinPlugin( &pluginFunctionRead );
43 registerBuiltinPlugin( &pluginFunctionWrite );
37 44
38 DIR *dir = opendir("/usr/lib/build"); 45 DIR *dir = opendir("/usr/lib/build");
39 if( !dir ) 46 if( !dir )
diff --git a/src/functionread.cpp b/src/functionread.cpp
new file mode 100644
index 0000000..789e9e1
--- /dev/null
+++ b/src/functionread.cpp
@@ -0,0 +1,40 @@
1#include "functionread.h"
2#include "filemgr.h"
3
4#include <bu/plugger.h>
5PluginInterface3( pluginFunctionRead, read, FunctionRead, Function,
6 "Mike Buland", 0, 1 );
7
8FunctionRead::FunctionRead()
9{
10}
11
12FunctionRead::~FunctionRead()
13{
14}
15
16Bu::String FunctionRead::getName() const
17{
18 return "read";
19}
20
21Variable FunctionRead::call( Variable &input, VarList lParams )
22{
23 Variable vRet;
24 if( lParams.getSize() == 1 )
25 {
26 int iSize = lParams.first().toInt();
27 Bu::String sBuf( iSize );
28 sBuf.resize(
29 FileMgr::getInstance().get( (int)input.getOpaque() ).read(
30 sBuf.getStr(), iSize
31 )
32 );
33 vRet = sBuf;
34 return vRet;
35 }
36 throw Bu::ExceptionBase(
37 "read takes zero or one parameters."
38 );
39}
40
diff --git a/src/functionread.h b/src/functionread.h
new file mode 100644
index 0000000..39bf32e
--- /dev/null
+++ b/src/functionread.h
@@ -0,0 +1,17 @@
1#ifndef FUNCTION_READ_H
2#define FUNCTION_READ_H
3
4#include "function.h"
5
6class FunctionRead : public Function
7{
8public:
9 FunctionRead();
10 virtual ~FunctionRead();
11
12 virtual Bu::String getName() const;
13 virtual Variable call( Variable &input, VarList lParams );
14
15};
16
17#endif
diff --git a/src/functionwrite.cpp b/src/functionwrite.cpp
new file mode 100644
index 0000000..7abb661
--- /dev/null
+++ b/src/functionwrite.cpp
@@ -0,0 +1,34 @@
1#include "functionwrite.h"
2#include "filemgr.h"
3
4#include <bu/plugger.h>
5PluginInterface3( pluginFunctionWrite, write, FunctionWrite, Function,
6 "Mike Buland", 0, 1 );
7
8FunctionWrite::FunctionWrite()
9{
10}
11
12FunctionWrite::~FunctionWrite()
13{
14}
15
16Bu::String FunctionWrite::getName() const
17{
18 return "write";
19}
20
21Variable FunctionWrite::call( Variable &input, VarList lParams )
22{
23 if( lParams.getSize() != 1 )
24 {
25 throw Bu::ExceptionBase(
26 "write takes one parameter, the string to write."
27 );
28 }
29 FileMgr::getInstance().get( (int)input.getOpaque() ).write(
30 lParams.first().toString()
31 );
32 return Variable();
33}
34
diff --git a/src/functionwrite.h b/src/functionwrite.h
new file mode 100644
index 0000000..75b2283
--- /dev/null
+++ b/src/functionwrite.h
@@ -0,0 +1,17 @@
1#ifndef FUNCTION_WRITE_H
2#define FUNCTION_WRITE_H
3
4#include "function.h"
5
6class FunctionWrite : public Function
7{
8public:
9 FunctionWrite();
10 virtual ~FunctionWrite();
11
12 virtual Bu::String getName() const;
13 virtual Variable call( Variable &input, VarList lParams );
14
15};
16
17#endif
diff --git a/src/variable.cpp b/src/variable.cpp
index 6c9529c..f638dc9 100644
--- a/src/variable.cpp
+++ b/src/variable.cpp
@@ -137,6 +137,13 @@ Variable::Variable( const VarList &lst )
137 uVal.lVal = new VarList( lst ); 137 uVal.lVal = new VarList( lst );
138} 138}
139 139
140Variable::Variable( void *oVal ) :
141 eType( typeOpaque )
142{
143 memset( &uVal, 0, sizeof(uVal) );
144 uVal.oVal = oVal;
145}
146
140Variable::~Variable() 147Variable::~Variable()
141{ 148{
142 if( eType == typeString || eType == typeRef ) 149 if( eType == typeString || eType == typeRef )
@@ -191,6 +198,12 @@ const VarList &Variable::getList() const
191 return *uVal.lVal; 198 return *uVal.lVal;
192} 199}
193 200
201const void *Variable::getOpaque() const
202{
203 if( eType != typeOpaque ) throw Bu::ExceptionBase("Wrong variable type.");
204 return uVal.oVal;
205}
206
194int Variable::toInt() const 207int Variable::toInt() const
195{ 208{
196 switch( eType ) 209 switch( eType )
@@ -302,6 +315,10 @@ Bu::String Variable::toString() const
302 315
303 case typeVersion: 316 case typeVersion:
304 break; 317 break;
318
319 case typeOpaque:
320 sRet = Bu::String("<opaque:%1>").arg( uVal.oVal );
321 break;
305 } 322 }
306 323
307 return sRet; 324 return sRet;
@@ -341,6 +358,9 @@ Variable Variable::toType( Type eNewType ) const
341 358
342 case typeRef: 359 case typeRef:
343 return Variable::mkRef( toString() ); 360 return Variable::mkRef( toString() );
361
362 case typeOpaque:
363 throw Bu::ExceptionBase("Cannot convert opaque types.");
344 } 364 }
345 throw Bu::ExceptionBase("Unhandled case in Variable toType"); 365 throw Bu::ExceptionBase("Unhandled case in Variable toType");
346} 366}
@@ -402,6 +422,9 @@ void Variable::doNegate()
402 422
403 case typeRef: 423 case typeRef:
404 throw Bu::ExceptionBase("You cannot negate reference values."); 424 throw Bu::ExceptionBase("You cannot negate reference values.");
425
426 case typeOpaque:
427 throw Bu::ExceptionBase("You cannot negate opaque values.");
405 } 428 }
406} 429}
407 430
@@ -463,6 +486,14 @@ const Variable &Variable::operator=( const Bu::String &rhs )
463 return *this; 486 return *this;
464} 487}
465 488
489const Variable &Variable::operator=( void *rhs )
490{
491 reset( typeOpaque );
492 uVal.oVal = rhs;
493
494 return *this;
495}
496
466const Variable &Variable::operator+=( const Variable &rhs ) 497const Variable &Variable::operator+=( const Variable &rhs )
467{ 498{
468 switch( eType ) 499 switch( eType )
@@ -575,6 +606,9 @@ bool Variable::operator==( const Variable &rhs ) const
575 606
576 case typeVersion: 607 case typeVersion:
577 return false; 608 return false;
609
610 case typeOpaque:
611 return uVal.oVal == rhs.uVal.oVal;
578 } 612 }
579 613
580 return false; 614 return false;
@@ -613,6 +647,9 @@ bool Variable::operator<( const Variable &rhs ) const
613 647
614 case typeRef: 648 case typeRef:
615 throw Bu::ExceptionBase("You cannot < compare reference values."); 649 throw Bu::ExceptionBase("You cannot < compare reference values.");
650
651 case typeOpaque:
652 throw Bu::ExceptionBase("You cannot < compare opaque values.");
616 } 653 }
617 throw Bu::ExceptionBase("Unhandled case in Variable < compare"); 654 throw Bu::ExceptionBase("Unhandled case in Variable < compare");
618} 655}
@@ -645,6 +682,9 @@ bool Variable::operator>( const Variable &rhs ) const
645 682
646 case typeRef: 683 case typeRef:
647 throw Bu::ExceptionBase("You cannot > compare reference values."); 684 throw Bu::ExceptionBase("You cannot > compare reference values.");
685
686 case typeOpaque:
687 throw Bu::ExceptionBase("You cannot > compare opaque values.");
648 } 688 }
649 throw Bu::ExceptionBase("Unhandled case in Variable > compare"); 689 throw Bu::ExceptionBase("Unhandled case in Variable > compare");
650} 690}
@@ -677,6 +717,9 @@ bool Variable::operator<=( const Variable &rhs ) const
677 717
678 case typeRef: 718 case typeRef:
679 throw Bu::ExceptionBase("You cannot <= compare reference values."); 719 throw Bu::ExceptionBase("You cannot <= compare reference values.");
720
721 case typeOpaque:
722 throw Bu::ExceptionBase("You cannot <= compare opaque values.");
680 } 723 }
681 throw Bu::ExceptionBase("Unhandled case in Variable <= compare"); 724 throw Bu::ExceptionBase("Unhandled case in Variable <= compare");
682} 725}
@@ -709,6 +752,9 @@ bool Variable::operator>=( const Variable &rhs ) const
709 752
710 case typeRef: 753 case typeRef:
711 throw Bu::ExceptionBase("You cannot >= compare reference values."); 754 throw Bu::ExceptionBase("You cannot >= compare reference values.");
755
756 case typeOpaque:
757 throw Bu::ExceptionBase("You cannot >= compare opaque values.");
712 } 758 }
713 throw Bu::ExceptionBase("Unhandled case in Variable >= compare"); 759 throw Bu::ExceptionBase("Unhandled case in Variable >= compare");
714} 760}
@@ -741,6 +787,9 @@ Variable Variable::operator+( const Variable &rhs ) const
741 787
742 case typeRef: 788 case typeRef:
743 throw Bu::ExceptionBase("You cannot add reference values."); 789 throw Bu::ExceptionBase("You cannot add reference values.");
790
791 case typeOpaque:
792 throw Bu::ExceptionBase("You cannot add opaque values.");
744 } 793 }
745 throw Bu::ExceptionBase("Unhandled case in Variable add"); 794 throw Bu::ExceptionBase("Unhandled case in Variable add");
746} 795}
@@ -773,6 +822,9 @@ Variable Variable::operator-( const Variable &rhs ) const
773 822
774 case typeRef: 823 case typeRef:
775 throw Bu::ExceptionBase("You cannot subtract reference values."); 824 throw Bu::ExceptionBase("You cannot subtract reference values.");
825
826 case typeOpaque:
827 throw Bu::ExceptionBase("You cannot subtract opaque values.");
776 } 828 }
777 throw Bu::ExceptionBase("Unhandled case in Variable subtract"); 829 throw Bu::ExceptionBase("Unhandled case in Variable subtract");
778} 830}
@@ -805,6 +857,9 @@ Variable Variable::operator*( const Variable &rhs ) const
805 857
806 case typeRef: 858 case typeRef:
807 throw Bu::ExceptionBase("You cannot multiply reference values."); 859 throw Bu::ExceptionBase("You cannot multiply reference values.");
860
861 case typeOpaque:
862 throw Bu::ExceptionBase("You cannot multiply opaque values.");
808 } 863 }
809 throw Bu::ExceptionBase("Unhandled case in Variable multiply"); 864 throw Bu::ExceptionBase("Unhandled case in Variable multiply");
810} 865}
@@ -837,6 +892,9 @@ Variable Variable::operator/( const Variable &rhs ) const
837 892
838 case typeRef: 893 case typeRef:
839 throw Bu::ExceptionBase("You cannot divide reference values."); 894 throw Bu::ExceptionBase("You cannot divide reference values.");
895
896 case typeOpaque:
897 throw Bu::ExceptionBase("You cannot divide opaque values.");
840 } 898 }
841 throw Bu::ExceptionBase("Unhandled case in Variable divide"); 899 throw Bu::ExceptionBase("Unhandled case in Variable divide");
842} 900}
@@ -868,6 +926,7 @@ Bu::Formatter &operator<<( Bu::Formatter &f, const Variable::Type &t )
868 case Variable::typeList: f << "list"; break; 926 case Variable::typeList: f << "list"; break;
869 case Variable::typeVersion: f << "version"; break; 927 case Variable::typeVersion: f << "version"; break;
870 case Variable::typeRef: f << "ref"; break; 928 case Variable::typeRef: f << "ref"; break;
929 case Variable::typeOpaque: f << "opaque"; break;
871 } 930 }
872 return f; 931 return f;
873} 932}
@@ -885,6 +944,8 @@ Bu::Formatter &operator<<( Bu::Formatter &f, const Variable &v )
885 case Variable::typeList: f << v.getList(); break; 944 case Variable::typeList: f << v.getList(); break;
886 case Variable::typeVersion:/*f << v.getVersion();*/ break; 945 case Variable::typeVersion:/*f << v.getVersion();*/ break;
887 case Variable::typeRef: f << v.getString(); break; 946 case Variable::typeRef: f << v.getString(); break;
947 case Variable::typeOpaque: f << "<opaque:" << v.getOpaque() << ">";
948 break;
888 } 949 }
889 950
890 return f; 951 return f;
@@ -924,6 +985,9 @@ Bu::ArchiveBase &operator<<( Bu::ArchiveBase &ar, const Variable &v )
924 case Variable::typeRef: 985 case Variable::typeRef:
925 ar << *v.uVal.sVal; 986 ar << *v.uVal.sVal;
926 break; 987 break;
988
989 case Variable::typeOpaque:
990 break;
927 } 991 }
928 992
929 return ar; 993 return ar;
@@ -966,6 +1030,9 @@ Bu::ArchiveBase &operator>>( Bu::ArchiveBase &ar, Variable &v )
966 case Variable::typeRef: 1030 case Variable::typeRef:
967 ar >> *v.uVal.sVal; 1031 ar >> *v.uVal.sVal;
968 break; 1032 break;
1033
1034 case Variable::typeOpaque:
1035 break;
969 } 1036 }
970 1037
971 return ar; 1038 return ar;
diff --git a/src/variable.h b/src/variable.h
index 260c96a..241393e 100644
--- a/src/variable.h
+++ b/src/variable.h
@@ -21,7 +21,8 @@ public:
21 typeVersion, 21 typeVersion,
22 typeString, 22 typeString,
23 typeList, 23 typeList,
24 typeRef /**< Reference by name, it's just a string. */ 24 typeRef, /**< Reference by name, it's just a string. */
25 typeOpaque /**< Only useful to functions. */
25 }; 26 };
26 27
27public: 28public:
@@ -41,6 +42,7 @@ public:
41 */ 42 */
42 Variable( const StrList &lst ); 43 Variable( const StrList &lst );
43 Variable( const VarList &lst ); 44 Variable( const VarList &lst );
45 Variable( void *oVal );
44 virtual ~Variable(); 46 virtual ~Variable();
45 47
46 static Variable mkRef( const Bu::String &sVal ); 48 static Variable mkRef( const Bu::String &sVal );
@@ -54,6 +56,7 @@ public:
54 bool getBool() const; 56 bool getBool() const;
55 const Bu::String &getString() const; 57 const Bu::String &getString() const;
56 const VarList &getList() const; 58 const VarList &getList() const;
59 const void *getOpaque() const;
57 60
58 // Conversion functions, they'll return the requested type, maybe an error 61 // Conversion functions, they'll return the requested type, maybe an error
59 // if the source data is really bad 62 // if the source data is really bad
@@ -77,6 +80,7 @@ public:
77 const Variable &operator=( const double &rhs ); 80 const Variable &operator=( const double &rhs );
78 const Variable &operator=( const bool &rhs ); 81 const Variable &operator=( const bool &rhs );
79 const Variable &operator=( const Bu::String &rhs ); 82 const Variable &operator=( const Bu::String &rhs );
83 const Variable &operator=( void *rhs );
80 84
81 const Variable &operator+=( const Variable &rhs ); 85 const Variable &operator+=( const Variable &rhs );
82 const Variable &operator<<( const Variable &rhs ); 86 const Variable &operator<<( const Variable &rhs );
@@ -102,6 +106,7 @@ private:
102 bool bVal; 106 bool bVal;
103 Bu::String *sVal; 107 Bu::String *sVal;
104 VarList *lVal; 108 VarList *lVal;
109 void *oVal;
105 } uVal; 110 } uVal;
106 111
107 void reset( Type eType ); 112 void reset( Type eType );