Files
secondo/apis/api_cpp/cs/example/Example.cpp

131 lines
3.4 KiB
C++
Raw Normal View History

2026-01-23 17:03:45 +08:00
/*
----
This file is part of SECONDO.
Copyright (C) 2007,
Faculty of Mathematics and Computer Science,
Database Systems for New Applications.
SECONDO is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
SECONDO is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with SECONDO; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
----
1 How to write your own Secondo Application in C++
This is a very small example describing how, an C++ programm can
communicate with an Secondo Server.
1.1 Includes
*/
#include <string>
#include <iostream>
#include <stdlib.h>
#include "SecondoInterface.h"
#include "SecondoInterfaceCS.h"
#include "NestedList.h"
#include "NList.h"
using namespace std;
int main(int argc, char** argv){
// create an interface to the secondo server
// the paramneter must be true to communicate as client
SecondoInterface* si = new SecondoInterfaceCS(true,0,false);
// define the name of the configuration file
string config = "Config.ini";
// read in runtime flags from the config file
si->InitRTFlags(config);
// set some variables needed to connect with the server
string user="";
string passwd="";
string host="localhost";
string port ="1234";
string errMsg; // return param
bool multiUser=true;
// try to connect
if(!si->Initialize(user,passwd,host,port,config,"",errMsg,multiUser)){
// connection failed, handle error
cerr << "Cannot initialize secondo system" << endl;
cerr << "Error message = " << errMsg << endl;
return 1;
}
// connected
cout << "SecondoInterface successfully initialized" << endl;
string command = "";
NestedList* nl = si->GetNestedList();
NList::setNLRef(nl);
// let the user enter secondo commands until he/she eters quit
while(command!="quit") {
cout << "Enter Command >";
getline(cin,command);
if((command!="quit") && (command.length()>0)){
cout << "send command " << command << endl;
ListExpr res = nl->TheEmptyList(); // will contain the result
SecErrInfo err; // will contain error information
// this is the command to communicate with secondo
si->Secondo(command, res, err);
// check whether command was successfully
if(err.code!=0){
// if the error code is different to zero, an error is occurred
cout << "Error during command. Error code :" << err.code << endl;
cout << "Error message = " << err.msg << endl;
} else {
// command was successfully
// do what ever you want to de with the result list
// in this little example, the result is just printed out
cout << "Command successfully processed" << endl;
cout << "Result is:" << endl;
cout << nl->ToString(res) << endl << endl;
}
}
}
// close connection
si->Terminate();
// destroy the interface
delete si;
return 0;
}