almost complete all var types

This commit is contained in:
Valk Richard Li 2019-07-26 15:28:13 +08:00 committed by GitHub
parent 7603ae21b6
commit c582269683
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 815 additions and 459 deletions

36
lab.cpp
View File

@ -1,6 +1,7 @@
#include <iostream>
#include "var.h"
using namespace nasal;
using namespace std;
int main()
{
@ -33,11 +34,44 @@ int main()
n.PrintList();
std::cout<<std::endl;
//n.Append(&n,"array","n");//cause infinite loop
n.Append(&n,"array");
n.Append(&n,"array");
n.PrintList();
std::cout<<std::endl;
NasalList rm;
rm.Append(&a,"int");
rm.Append(&a,"int");
rm.Append(&a,"int");
rm.PrintList();
std::cout<<std::endl;
m.Append("fifth",&rm,"array");
m.Append("sixth",&m,"hash");
m.PrintHash();
std::cout<<std::endl;
NasalHash test1(m);
test1.PrintHash();
NasalList test2(n);
std::cout<<std::endl;
test2.PrintList();
std::cout<<std::endl;
n.Append(&m,"hash");
n.PrintList();
std::cout<<std::endl<<std::endl;
NasalList qt;
qt.Append(&a,"int");
a++;
qt.Append(&a,"int");
NasalHash qthash;
qthash.Append("testlist",&qt,"array");
qthash.PrintHash();
std::cout<<std::endl;
qthash.Append("int",&a,"int");
qt.Append(&qthash,"hash");
qt.PrintList();
return 0;
}

398
nasal_hash.cpp Normal file
View File

@ -0,0 +1,398 @@
#ifndef __NASAL_HASH_CPP__
#define __NASAL_HASH_CPP__
#include "nasal_hash.h"
#include "nasal_list.h"
namespace nasal
{
NasalHash::NasalHash()
{
head=new HashUnit;
head->data=NULL;
head->next=NULL;
}
NasalHash::NasalHash(NasalHash &Source)
{
HashUnit *temp;
HashUnit *SourceTemp=Source.head;
head=new HashUnit;
head->next=NULL;
head->data=NULL;
temp=head;
while(SourceTemp->next)
{
SourceTemp=SourceTemp->next;
temp->next=new HashUnit;
temp=temp->next;
temp->Type=SourceTemp->Type;
temp->VarName=SourceTemp->VarName;
if(temp->Type=="int")
{
temp->data=new int;
*((int *)temp->data)=*((int *)SourceTemp->data);
}
if(temp->Type=="float")
{
temp->data=new float;
*((float *)temp->data)=*((float *)SourceTemp->data);
}
if(temp->Type=="double")
{
temp->data=new double;
*((double *)temp->data)=*((double *)SourceTemp->data);
}
if(temp->Type=="char")
{
temp->data=new char;
*((char *)temp->data)=*((char *)SourceTemp->data);
}
if(temp->Type=="long long int")
{
temp->data=new long long int;
*((long long int *)temp->data)=*((long long int *)SourceTemp->data);
}
if(temp->Type=="string")
{
temp->data=new std::string;
*((std::string *)temp->data)=*((std::string *)SourceTemp->data);
}
if(temp->Type=="array")
{
temp->data=new NasalList;
*((NasalList *)temp->data)=*((NasalList *)SourceTemp->data);
}
if(temp->Type=="hash")
{
temp->data=new NasalHash;
*((NasalHash *)temp->data)=*((NasalHash *)SourceTemp->data);
}
temp->next=NULL;
}
}
NasalHash::~NasalHash()
{
HashUnit *temp=head;
while(temp->next)
{
head=temp->next;
if(temp->data)
{
if(temp->Type=="int")
delete (int *)temp->data;
if(temp->Type=="float")
delete (float *)temp->data;
if(temp->Type=="double")
delete (double *)temp->data;
if(temp->Type=="char")
delete (char *)temp->data;
if(temp->Type=="long long int")
delete (long long int *)temp->data;
if(temp->Type=="string")
delete (std::string *)temp->data;
if(temp->Type=="array")
delete (NasalList *)temp->data;
if(temp->Type=="hash")
delete (NasalHash *)temp->data;
}
delete temp;
temp=head;
}
if(temp->data)
{
if(temp->Type=="int")
delete (int *)temp->data;
if(temp->Type=="float")
delete (float *)temp->data;
if(temp->Type=="double")
delete (double *)temp->data;
if(temp->Type=="char")
delete (char *)temp->data;
if(temp->Type=="long long int")
delete (long long int *)temp->data;
if(temp->Type=="string")
delete (std::string *)temp->data;
if(temp->Type=="array")
delete (NasalList *)temp->data;
if(temp->Type=="hash")
delete (NasalHash *)temp->data;
}
delete temp;
}
void NasalHash::PrintHash()
{
HashUnit *temp=head;
std::cout<<"{ ";
while(temp->next)
{
temp=temp->next;
std::cout<<temp->VarName<<":";
if(temp->next)
{
if(temp->Type=="int")
std::cout<<*((int *)temp->data)<<", ";
if(temp->Type=="float")
std::cout<<*((float *)temp->data)<<", ";
if(temp->Type=="double")
std::cout<<*((double *)temp->data)<<", ";
if(temp->Type=="char")
std::cout<<"\""<<*((char *)temp->data)<<"\", ";
if(temp->Type=="long long int")
std::cout<<*((long long int *)temp->data)<<", ";
if(temp->Type=="string")
std::cout<<"\""<<*((std::string *)temp->data)<<"\", ";
if(temp->Type=="array")
{
((NasalList *)temp->data)->PrintList();
std::cout<<", ";
}
if(temp->Type=="hash")
{
((NasalHash *)temp->data)->PrintHash();
std::cout<<", ";
}
}
else
{
if(temp->Type=="int")
std::cout<<*((int *)temp->data);
if(temp->Type=="float")
std::cout<<*((float *)temp->data);
if(temp->Type=="double")
std::cout<<*((double *)temp->data);
if(temp->Type=="char")
std::cout<<"\""<<*((char *)temp->data)<<"\"";
if(temp->Type=="long long int")
std::cout<<*((long long int *)temp->data);
if(temp->Type=="string")
std::cout<<"\""<<*((std::string *)temp->data)<<"\"";
if(temp->Type=="array")
((NasalList *)temp->data)->PrintList();
if(temp->Type=="hash")
((NasalHash *)temp->data)->PrintHash();
}
}
std::cout<<"}";
}
void NasalHash::Append(const char *VariaName,void *AppendData,const char *TypeName)
{
NasalHash TempHash;//sometimes user may use n.append(n)
if(TypeName=="hash")
TempHash=*((NasalHash *)AppendData);
HashUnit *temp=head;
while(temp->next)
{
temp=temp->next;
}
HashUnit *NewHashMember=new HashUnit;
NewHashMember->data=NULL;
temp->next=NewHashMember;
NewHashMember->VarName=VariaName;
NewHashMember->Type=TypeName;
if(TypeName=="int")
{
NewHashMember->data=new int;
*((int *)NewHashMember->data)=*((int *)AppendData);
}
else if(TypeName=="float")
{
NewHashMember->data=new float;
*((float *)NewHashMember->data)=*((float *)AppendData);
}
else if(TypeName=="double")
{
NewHashMember->data=new double;
*((double *)NewHashMember->data)=*((double *)AppendData);
}
else if(TypeName=="char")
{
NewHashMember->data=new char;
*((char *)NewHashMember->data)=*((char *)AppendData);
}
else if(TypeName=="long long int")
{
NewHashMember->data=new long long int;
*((long long int *)NewHashMember->data)=*((long long int *)AppendData);
}
else if(TypeName=="string")
{
NewHashMember->data=new std::string;
*((std::string *)NewHashMember->data)=*((std::string *)AppendData);
}
else if(TypeName=="array")
{
NewHashMember->data=new NasalList;
*((NasalList *)NewHashMember->data)=*((NasalList *)AppendData);
}
else if(TypeName=="hash")
{
NewHashMember->data=new NasalHash;
*((NasalHash *)NewHashMember->data)=TempHash;
}
NewHashMember->next=NULL;
}
int NasalHash::Contains(const char *VariaName)
{
HashUnit *temp=head;
while(temp->next)
{
temp=temp->next;
if(temp->VarName==VariaName)
return 1;
}
return 0;
}
NasalList NasalHash::Keys()
{
NasalList FeedBackList;
HashUnit *temp=head;
while(temp->next)
{
temp=temp->next;
FeedBackList.Append(&(temp->VarName),"string");
}
return FeedBackList;
}
void NasalHash::Delete(const char *VariaName)
{
HashUnit *temp=head;
HashUnit *LastNode;
while(temp->next)
{
LastNode=temp;
temp=temp->next;
if(temp->VarName==VariaName)
{
LastNode->next=temp->next;
if(temp->Type=="int")
delete (int *)temp->data;
if(temp->Type=="float")
delete (float *)temp->data;
if(temp->Type=="double")
delete (double *)temp->data;
if(temp->Type=="char")
delete (char *)temp->data;
if(temp->Type=="long long int")
delete (long long int *)temp->data;
if(temp->Type=="string")
delete (std::string *)temp->data;
if(temp->Type=="array")
delete (NasalList *)temp->data;
if(temp->Type=="hash")
delete (NasalHash *)temp->data;
delete temp;
return;
}
}
std::cout<<"[Error]: Could not find this element \""<<VariaName<<"\"."<<std::endl;
return;
}
NasalHash& NasalHash::operator=(const NasalHash &Source)
{
HashUnit *temp=head;
HashUnit *SourceTemp=Source.head;
while(temp->next)
{
head=temp->next;
if(temp->data)
{
if(temp->Type=="int")
delete (int *)temp->data;
if(temp->Type=="float")
delete (float *)temp->data;
if(temp->Type=="double")
delete (double *)temp->data;
if(temp->Type=="char")
delete (char *)temp->data;
if(temp->Type=="long long int")
delete (long long int *)temp->data;
if(temp->Type=="string")
delete (std::string *)temp->data;
if(temp->Type=="array")
delete (NasalList *)temp->data;
if(temp->Type=="hash")
delete (NasalHash *)temp->data;
}
delete temp;
temp=head;
}
if(temp->data)
{
if(temp->Type=="int")
delete (int *)temp->data;
if(temp->Type=="float")
delete (float *)temp->data;
if(temp->Type=="double")
delete (double *)temp->data;
if(temp->Type=="char")
delete (char *)temp->data;
if(temp->Type=="long long int")
delete (long long int *)temp->data;
if(temp->Type=="string")
delete (std::string *)temp->data;
if(temp->Type=="array")
delete (NasalList *)temp->data;
if(temp->Type=="hash")
delete (NasalHash *)temp->data;
}
delete temp;
head=new HashUnit;
head->next=NULL;
temp=head;
while(SourceTemp->next)
{
SourceTemp=SourceTemp->next;
temp->next=new HashUnit;
temp=temp->next;
temp->Type=SourceTemp->Type;
temp->VarName=SourceTemp->VarName;
if(temp->Type=="int")
{
temp->data=new int;
*((int *)temp->data)=*((int *)SourceTemp->data);
}
if(temp->Type=="float")
{
temp->data=new float;
*((float *)temp->data)=*((float *)SourceTemp->data);
}
if(temp->Type=="double")
{
temp->data=new double;
*((double *)temp->data)=*((double *)SourceTemp->data);
}
if(temp->Type=="char")
{
temp->data=new char;
*((char *)temp->data)=*((char *)SourceTemp->data);
}
if(temp->Type=="long long int")
{
temp->data=new long long int;
*((long long int *)temp->data)=*((long long int *)SourceTemp->data);
}
if(temp->Type=="string")
{
temp->data=new std::string;
*((std::string *)temp->data)=*((std::string *)SourceTemp->data);
}
if(temp->Type=="array")
{
temp->data=new NasalList;
*((NasalList *)temp->data)=*((NasalList *)SourceTemp->data);
}
if(temp->Type=="hash")
{
temp->data=new NasalHash;
*((NasalHash *)temp->data)=*((NasalHash *)SourceTemp->data);
}
temp->next=NULL;
}
return *this;
}
}
#endif

View File

@ -8,7 +8,6 @@
namespace nasal
{
struct HashUnit
{
std::string VarName;
@ -22,199 +21,15 @@ class NasalHash
private:
HashUnit *head;
public:
NasalHash()
{
head=new HashUnit;
head->next=NULL;
}
~NasalHash()
{
HashUnit *temp=head;
while(temp->next)
{
head=temp->next;
if(temp->data)
{
if(temp->Type=="int")
delete (int *)temp->data;
if(temp->Type=="float")
delete (float *)temp->data;
if(temp->Type=="double")
delete (double *)temp->data;
if(temp->Type=="char")
delete (char *)temp->data;
if(temp->Type=="long long int")
delete (long long int *)temp->data;
if(temp->Type=="string")
delete (std::string *)temp->data;
if(temp->Type=="array")
delete (NasalList *)temp->data;
if(temp->Type=="hash")
delete (NasalHash *)temp->data;
}
delete temp;
temp=head;
}
delete temp;
}
void PrintHash()
{
HashUnit *temp=head;
std::cout<<"{ ";
while(temp->next)
{
temp=temp->next;
std::cout<<temp->VarName<<":";
if(temp->next)
{
if(temp->Type=="int")
std::cout<<*((int *)temp->data)<<", ";
if(temp->Type=="float")
std::cout<<*((float *)temp->data)<<", ";
if(temp->Type=="double")
std::cout<<*((double *)temp->data)<<", ";
if(temp->Type=="char")
std::cout<<"\""<<*((char *)temp->data)<<"\", ";
if(temp->Type=="long long int")
std::cout<<*((long long int *)temp->data)<<", ";
if(temp->Type=="string")
std::cout<<"\""<<*((std::string *)temp->data)<<"\", ";
if(temp->Type=="array")
{
((NasalList *)temp->data)->PrintList();
std::cout<<", ";
}
if(temp->Type=="hash")
{
((NasalHash *)temp->data)->PrintHash();
std::cout<<", ";
}
}
else
{
if(temp->Type=="int")
std::cout<<*((int *)temp->data);
if(temp->Type=="float")
std::cout<<*((float *)temp->data);
if(temp->Type=="double")
std::cout<<*((double *)temp->data);
if(temp->Type=="char")
std::cout<<"\""<<*((char *)temp->data)<<"\"";
if(temp->Type=="long long int")
std::cout<<*((long long int *)temp->data);
if(temp->Type=="string")
std::cout<<"\""<<*((std::string *)temp->data)<<"\"";
if(temp->Type=="array")
((NasalList *)temp->data)->PrintList();
if(temp->Type=="hash")
((NasalHash *)temp->data)->PrintHash();
}
}
std::cout<<"}";
}
void Append(const char *VariaName,void *AppendData,const char *TypeName)
{
HashUnit *temp=head;
while(temp->next)
{
temp=temp->next;
}
HashUnit *NewHashMember=new HashUnit;
temp->next=NewHashMember;
NewHashMember->VarName=VariaName;
NewHashMember->Type=TypeName;
if(TypeName=="int")
{
NewHashMember->data=new int;
*((int *)NewHashMember->data)=*((int *)AppendData);
}
if(TypeName=="float")
{
NewHashMember->data=new float;
*((float *)NewHashMember->data)=*((float *)AppendData);
}
if(TypeName=="double")
{
NewHashMember->data=new double;
*((double *)NewHashMember->data)=*((double *)AppendData);
}
if(TypeName=="char")
{
NewHashMember->data=new char;
*((char *)NewHashMember->data)=*((char *)AppendData);
}
if(TypeName=="long long int")
{
NewHashMember->data=new long long int;
*((long long int *)NewHashMember->data)=*((long long int *)AppendData);
}
if(TypeName=="string")
{
NewHashMember->data=new std::string;
*((std::string *)NewHashMember->data)=*((std::string *)AppendData);
}
if(temp->Type=="array")
;
if(temp->Type=="hash")
;
NewHashMember->next=NULL;
}
int Contains(const char *VariaName)
{
HashUnit *temp=head;
while(temp->next)
{
temp=temp->next;
if(temp->VarName==VariaName)
return 1;
}
return 0;
}
NasalList Keys()
{
NasalList FeedBackList;
HashUnit *temp=head;
while(temp->next)
{
temp=temp->next;
FeedBackList.Append(&(temp->VarName),"string");
}
return FeedBackList;
}
void Delete(const char *VariaName)
{
HashUnit *temp=head;
HashUnit *LastNode;
while(temp->next)
{
LastNode=temp;
temp=temp->next;
if(temp->VarName==VariaName)
{
LastNode->next=temp->next;
if(temp->Type=="int")
delete (int *)temp->data;
if(temp->Type=="float")
delete (float *)temp->data;
if(temp->Type=="double")
delete (double *)temp->data;
if(temp->Type=="char")
delete (char *)temp->data;
if(temp->Type=="long long int")
delete (long long int *)temp->data;
if(temp->Type=="string")
delete (std::string *)temp->data;
if(temp->Type=="array")
delete (NasalList *)temp->data;
if(temp->Type=="hash")
delete (NasalHash *)temp->data;
delete temp;
return;
}
}
std::cout<<"[Error]: Could not find this element \""<<VariaName<<"\"."<<std::endl;
return;
}
NasalHash();
NasalHash(NasalHash&);
~NasalHash();
void PrintHash();
void Append(const char *,void *,const char *);
int Contains(const char *);
NasalList Keys();
void Delete(const char *);
NasalHash& operator=(const NasalHash&);
};
}

341
nasal_list.cpp Normal file
View File

@ -0,0 +1,341 @@
#ifndef __NASAL_LIST_CPP__
#define __NASAL_LIST_CPP__
#include "nasal_list.h"
#include "nasal_hash.h"
namespace nasal
{
NasalList::NasalList()
{
head=new ListUnit;
head->data=NULL;
head->next=NULL;
}
NasalList::NasalList(NasalList &Source)
{
ListUnit *temp;
ListUnit *SourceTemp=Source.head;
head=new ListUnit;
head->next=NULL;
temp=head;
while(SourceTemp->next)
{
SourceTemp=SourceTemp->next;
temp->next=new ListUnit;
temp=temp->next;
temp->Type=SourceTemp->Type;
if(temp->Type=="int")
{
temp->data=new int;
*((int *)temp->data)=*((int *)SourceTemp->data);
}
if(temp->Type=="float")
{
temp->data=new float;
*((float *)temp->data)=*((float *)SourceTemp->data);
}
if(temp->Type=="double")
{
temp->data=new double;
*((double *)temp->data)=*((double *)SourceTemp->data);
}
if(temp->Type=="char")
{
temp->data=new char;
*((char *)temp->data)=*((char *)SourceTemp->data);
}
if(temp->Type=="long long int")
{
temp->data=new long long int;
*((long long int *)temp->data)=*((long long int *)SourceTemp->data);
}
if(temp->Type=="string")
{
temp->data=new std::string;
*((std::string *)temp->data)=*((std::string *)SourceTemp->data);
}
if(temp->Type=="array")
{
temp->data=new NasalList;
*((NasalList *)temp->data)=*((NasalList *)SourceTemp->data);
}
if(temp->Type=="hash")
{
temp->data=new NasalHash;
*((NasalHash *)temp->data)=*((NasalHash *)SourceTemp->data);
}
temp->next=NULL;
}
}
NasalList::~NasalList()
{
ListUnit *temp=head;
while(temp->next)
{
head=temp->next;
if(temp->data)
{
if(temp->Type=="int")
delete (int *)temp->data;
if(temp->Type=="float")
delete (float *)temp->data;
if(temp->Type=="double")
delete (double *)temp->data;
if(temp->Type=="char")
delete (char *)temp->data;
if(temp->Type=="long long int")
delete (long long int *)temp->data;
if(temp->Type=="string")
delete (std::string *)temp->data;
if(temp->Type=="array")
delete (NasalList *)temp->data;
if(temp->Type=="hash")
delete (NasalHash *)temp->data;
}
delete temp;
temp=head;
}
if(temp->data)
{
if(temp->Type=="int")
delete (int *)temp->data;
if(temp->Type=="float")
delete (float *)temp->data;
if(temp->Type=="double")
delete (double *)temp->data;
if(temp->Type=="char")
delete (char *)temp->data;
if(temp->Type=="long long int")
delete (long long int *)temp->data;
if(temp->Type=="string")
delete (std::string *)temp->data;
if(temp->Type=="array")
delete (NasalList *)temp->data;
if(temp->Type=="hash")
delete (NasalHash *)temp->data;
}
delete temp;
}
void NasalList::PrintList()
{
ListUnit *temp=head;
std::cout<<"[ ";
while(temp->next)
{
temp=temp->next;
if(temp->next)
{
if(temp->Type=="int")
std::cout<<*((int *)temp->data)<<", ";
if(temp->Type=="float")
std::cout<<*((float *)temp->data)<<", ";
if(temp->Type=="double")
std::cout<<*((double *)temp->data)<<", ";
if(temp->Type=="char")
std::cout<<"\""<<*((char *)temp->data)<<"\", ";
if(temp->Type=="long long int")
std::cout<<*((long long int *)temp->data)<<", ";
if(temp->Type=="string")
std::cout<<"\""<<*((std::string *)temp->data)<<"\", ";
if(temp->Type=="array")
{
((NasalList *)temp->data)->PrintList();
std::cout<<", ";
}
if(temp->Type=="hash")
{
((NasalHash *)temp->data)->PrintHash();
std::cout<<", ";
}
}
else
{
if(temp->Type=="int")
std::cout<<*((int *)temp->data);
if(temp->Type=="float")
std::cout<<*((float *)temp->data);
if(temp->Type=="double")
std::cout<<*((double *)temp->data);
if(temp->Type=="char")
std::cout<<"\""<<*((char *)temp->data)<<"\"";
if(temp->Type=="long long int")
std::cout<<*((long long int *)temp->data);
if(temp->Type=="string")
std::cout<<"\""<<*((std::string *)temp->data)<<"\"";
if(temp->Type=="array")
((NasalList *)temp->data)->PrintList();
if(temp->Type=="hash")
((NasalHash *)temp->data)->PrintHash();
}
}
std::cout<<"]";
}
void NasalList::Append(void *AppendData,const char *TypeName)
{
NasalList TempList;//sometimes user may use n.append(n)
if(TypeName=="array")
TempList=*((NasalList *)AppendData);
ListUnit *temp=head;
while(temp->next)
{
temp=temp->next;
}
ListUnit *NewListMember=new ListUnit;
temp->next=NewListMember;
NewListMember->data=NULL;
NewListMember->Type=TypeName;
if(TypeName=="int")
{
NewListMember->data=new int;
*((int *)NewListMember->data)=*((int *)AppendData);
}
if(TypeName=="float")
{
NewListMember->data=new float;
*((float *)NewListMember->data)=*((float *)AppendData);
}
if(TypeName=="double")
{
NewListMember->data=new double;
*((double *)NewListMember->data)=*((double *)AppendData);
}
if(TypeName=="char")
{
NewListMember->data=new char;
*((char *)NewListMember->data)=*((char *)AppendData);
}
if(TypeName=="long long int")
{
NewListMember->data=new long long int;
*((long long int *)NewListMember->data)=*((long long int *)AppendData);
}
if(TypeName=="string")
{
NewListMember->data=new std::string;
*((std::string *)NewListMember->data)=*((std::string *)AppendData);
}
if(TypeName=="array")
{
NewListMember->data=new NasalList;
*((NasalList *)NewListMember->data)=TempList;
}
if(TypeName=="hash")
{
NewListMember->data=new NasalHash;
*((NasalHash *)NewListMember->data)=*((NasalHash *)AppendData);
}
NewListMember->next=NULL;
}
NasalList& NasalList::operator=(const NasalList &Source)
{
ListUnit *temp=head;
ListUnit *SourceTemp=Source.head;
while(temp->next)
{
head=temp->next;
if(temp->data)
{
if(temp->Type=="int")
delete (int *)temp->data;
if(temp->Type=="float")
delete (float *)temp->data;
if(temp->Type=="double")
delete (double *)temp->data;
if(temp->Type=="char")
delete (char *)temp->data;
if(temp->Type=="long long int")
delete (long long int *)temp->data;
if(temp->Type=="string")
delete (std::string *)temp->data;
if(temp->Type=="array")
delete (NasalList *)temp->data;
if(temp->Type=="hash")
delete (NasalHash *)temp->data;
}
delete temp;
temp=head;
}
if(temp->data)
{
if(temp->Type=="int")
delete (int *)temp->data;
if(temp->Type=="float")
delete (float *)temp->data;
if(temp->Type=="double")
delete (double *)temp->data;
if(temp->Type=="char")
delete (char *)temp->data;
if(temp->Type=="long long int")
delete (long long int *)temp->data;
if(temp->Type=="string")
delete (std::string *)temp->data;
if(temp->Type=="array")
delete (NasalList *)temp->data;
if(temp->Type=="hash")
delete (NasalHash *)temp->data;
}
delete temp;
head=new ListUnit;
head->next=NULL;
temp=head;
while(SourceTemp->next)
{
SourceTemp=SourceTemp->next;
temp->next=new ListUnit;
temp=temp->next;
temp->Type=SourceTemp->Type;
if(temp->Type=="int")
{
temp->data=new int;
*((int *)temp->data)=*((int *)SourceTemp->data);
}
if(temp->Type=="float")
{
temp->data=new float;
*((float *)temp->data)=*((float *)SourceTemp->data);
}
if(temp->Type=="double")
{
temp->data=new double;
*((double *)temp->data)=*((double *)SourceTemp->data);
}
if(temp->Type=="char")
{
temp->data=new char;
*((char *)temp->data)=*((char *)SourceTemp->data);
}
if(temp->Type=="long long int")
{
temp->data=new long long int;
*((long long int *)temp->data)=*((long long int *)SourceTemp->data);
}
if(temp->Type=="string")
{
temp->data=new std::string;
*((std::string *)temp->data)=*((std::string *)SourceTemp->data);
}
if(temp->Type=="array")
{
temp->data=new NasalList;
*((NasalList *)temp->data)=*((NasalList *)SourceTemp->data);
}
if(temp->Type=="hash")
{
temp->data=new NasalHash;
*((NasalHash *)temp->data)=*((NasalHash *)SourceTemp->data);
}
temp->next=NULL;
}
return *this;
}
}
#endif

View File

@ -3,7 +3,7 @@
#include <iostream>
#include <cstring>
#include "nasal_hash.h"
namespace nasal
{
@ -20,267 +20,12 @@ class NasalList
private:
ListUnit *head;
public:
NasalList()
{
head=new ListUnit;
head->data=NULL;
head->next=NULL;
}
~NasalList()
{
ListUnit *temp=head;
while(temp->next)
{
head=temp->next;
if(temp->data)
{
if(temp->Type=="int")
delete (int *)temp->data;
if(temp->Type=="float")
delete (float *)temp->data;
if(temp->Type=="double")
delete (double *)temp->data;
if(temp->Type=="char")
delete (char *)temp->data;
if(temp->Type=="long long int")
delete (long long int *)temp->data;
if(temp->Type=="string")
delete (std::string *)temp->data;
if(temp->Type=="array")
delete (NasalList *)temp->data;
if(temp->Type=="hash");
//delete (NasalHash *)temp->data;
}
delete temp;
temp=head;
}
if(temp->data)
{
if(temp->Type=="int")
delete (int *)temp->data;
if(temp->Type=="float")
delete (float *)temp->data;
if(temp->Type=="double")
delete (double *)temp->data;
if(temp->Type=="char")
delete (char *)temp->data;
if(temp->Type=="long long int")
delete (long long int *)temp->data;
if(temp->Type=="string")
delete (std::string *)temp->data;
if(temp->Type=="array")
delete (NasalList *)temp->data;
if(temp->Type=="hash")
;
}
delete temp;
}
void PrintList()
{
ListUnit *temp=head;
std::cout<<"[ ";
while(temp->next)
{
temp=temp->next;
if(temp->next)
{
if(temp->Type=="int")
std::cout<<*((int *)temp->data)<<", ";
if(temp->Type=="float")
std::cout<<*((float *)temp->data)<<", ";
if(temp->Type=="double")
std::cout<<*((double *)temp->data)<<", ";
if(temp->Type=="char")
std::cout<<"\""<<*((char *)temp->data)<<"\", ";
if(temp->Type=="long long int")
std::cout<<*((long long int *)temp->data)<<", ";
if(temp->Type=="string")
std::cout<<"\""<<*((std::string *)temp->data)<<"\", ";
if(temp->Type=="array")
{
((NasalList *)temp->data)->PrintList();
std::cout<<", ";
}
if(temp->Type=="hash")
{
;//((NasalHash *)temp->data)->PrintHash();
std::cout<<", ";
}
}
else
{
if(temp->Type=="int")
std::cout<<*((int *)temp->data);
if(temp->Type=="float")
std::cout<<*((float *)temp->data);
if(temp->Type=="double")
std::cout<<*((double *)temp->data);
if(temp->Type=="char")
std::cout<<"\""<<*((char *)temp->data)<<"\"";
if(temp->Type=="long long int")
std::cout<<*((long long int *)temp->data);
if(temp->Type=="string")
std::cout<<"\""<<*((std::string *)temp->data)<<"\"";
if(temp->Type=="array")
((NasalList *)temp->data)->PrintList();
if(temp->Type=="hash")
;//((NasalHash *)temp->data)->PrintHash();
}
}
std::cout<<"]";
}
void Append(void *AppendData,const char *TypeName)
{
NasalList TempList;//sometimes user may use n.append(n)
if(TypeName=="array")
TempList=*((NasalList *)AppendData);
ListUnit *temp=head;
while(temp->next)
{
temp=temp->next;
}
ListUnit *NewListMember=new ListUnit;
temp->next=NewListMember;
NewListMember->Type=TypeName;
if(TypeName=="int")
{
NewListMember->data=new int;
*((int *)NewListMember->data)=*((int *)AppendData);
}
if(TypeName=="float")
{
NewListMember->data=new float;
*((float *)NewListMember->data)=*((float *)AppendData);
}
if(TypeName=="double")
{
NewListMember->data=new double;
*((double *)NewListMember->data)=*((double *)AppendData);
}
if(TypeName=="char")
{
NewListMember->data=new char;
*((char *)NewListMember->data)=*((char *)AppendData);
}
if(TypeName=="long long int")
{
NewListMember->data=new long long int;
*((long long int *)NewListMember->data)=*((long long int *)AppendData);
}
if(TypeName=="string")
{
NewListMember->data=new std::string;
*((std::string *)NewListMember->data)=*((std::string *)AppendData);
}
if(TypeName=="array")
{
NewListMember->data=new NasalList;
*((NasalList *)NewListMember->data)=TempList;
}
if(TypeName=="hash")
;
NewListMember->next=NULL;
}
NasalList& operator=(const NasalList &Source)
{
ListUnit *temp=head;
ListUnit *SourceTemp=Source.head;
while(temp->next)
{
head=temp->next;
if(temp->data)
{
if(temp->Type=="int")
delete (int *)temp->data;
if(temp->Type=="float")
delete (float *)temp->data;
if(temp->Type=="double")
delete (double *)temp->data;
if(temp->Type=="char")
delete (char *)temp->data;
if(temp->Type=="long long int")
delete (long long int *)temp->data;
if(temp->Type=="string")
delete (std::string *)temp->data;
if(temp->Type=="array")
delete (NasalList *)temp->data;
if(temp->Type=="hash")
;
}
delete temp;
temp=head;
}
if(temp->data)
{
if(temp->Type=="int")
delete (int *)temp->data;
if(temp->Type=="float")
delete (float *)temp->data;
if(temp->Type=="double")
delete (double *)temp->data;
if(temp->Type=="char")
delete (char *)temp->data;
if(temp->Type=="long long int")
delete (long long int *)temp->data;
if(temp->Type=="string")
delete (std::string *)temp->data;
if(temp->Type=="array")
delete (NasalList *)temp->data;
if(temp->Type=="hash")
;
}
delete temp;
head=new ListUnit;
head->next=NULL;
temp=head;
while(SourceTemp->next)
{
SourceTemp=SourceTemp->next;
temp->next=new ListUnit;
temp=temp->next;
temp->Type=SourceTemp->Type;
if(temp->Type=="int")
{
temp->data=new int;
*((int *)temp->data)=*((int *)SourceTemp->data);
}
if(temp->Type=="float")
{
temp->data=new float;
*((float *)temp->data)=*((float *)SourceTemp->data);
}
if(temp->Type=="double")
{
temp->data=new double;
*((double *)temp->data)=*((double *)SourceTemp->data);
}
if(temp->Type=="char")
{
temp->data=new char;
*((char *)temp->data)=*((char *)SourceTemp->data);
}
if(temp->Type=="long long int")
{
temp->data=new long long int;
*((long long int *)temp->data)=*((long long int *)SourceTemp->data);
}
if(temp->Type=="string")
{
temp->data=new std::string;
*((std::string *)temp->data)=*((std::string *)SourceTemp->data);
}
if(temp->Type=="array")
{
temp->data=new NasalList;
*((NasalList *)temp->data)=*((NasalList *)SourceTemp->data);
}
if(temp->Type=="hash")
;
temp->next=NULL;
}
return *this;
}
NasalList();
NasalList(NasalList &);
~NasalList();
void PrintList();
void Append(void *,const char *);
NasalList& operator=(const NasalList &);
};
}

View File

@ -3,8 +3,8 @@
#include<iostream>
#include<cstring>
#include "nasal_hash.h"
#include "nasal_list.h"
#include "nasal_hash.cpp"
#include "nasal_list.cpp"
namespace nasal
{

23
var.h Normal file
View File

@ -0,0 +1,23 @@
#ifndef __VAR_H__
#define __VAR_H__
#include "nasal_hash.cpp"
#include "nasal_list.cpp"
#include "nasal_print.h"
// int
// char
// long long int
// double
// float
// string
// const char *
// NasalHash
// NasalList
/*
NasalList->delete NasalList & NasalHash
NasalHash->delete NasalList & NasalHash
*/
#endif