//============================================================================ // // Name : libqm++ // File : libqm++.cpp // Author : Andrew Hubl // Version : 1 // Copyright : (c) Copyright 2008 Andrew Hubl. All Rights Reserved. // Description : C++ class for OpenQM database connection // License : GNU GPLv2 // Notes : Other license terms may be available upon request. // // This program 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. // This program 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 this program. If not, see . // //============================================================================ #include "libqm++.h" //#include using namespace std; // This makes it easier to allocate memory for every argument. It simply // allocates space for the entire string (including terminating null // character) for the C++ string x, and then copies the data from x into a // non-const c string (char*). It should be used like this: // // pstr = CHARMALLOC(cppstr); #define CMALLOC(x) strcpy((char*)malloc((x.size()+1)*sizeof(char)), x.c_str()) Qm::Qm(const string& saccount) { // Convert the C++ strings to char* char *paccount = CMALLOC(saccount); QMConnectLocal(paccount); // Free all of the char* free(paccount); } Qm::Qm(const string& saccount, int& success) { // Convert the C++ strings to char* char *paccount = CMALLOC(saccount); success = QMConnectLocal(paccount); // Free all of the char* free(paccount); } Qm::Qm(const string& shost, \ const int& port, \ const string& susername, \ const string& spassword, \ const string& saccount) { char *phost = NULL, *pusername = NULL, *ppassword = NULL, *paccount = NULL; // Convert all of the C++ strings to char* phost = CMALLOC(shost); pusername = CMALLOC(susername); ppassword = CMALLOC(spassword); paccount = CMALLOC(saccount); QMConnect(phost, port, pusername, ppassword, paccount); // Free all of the char* free(phost); free(pusername); free(ppassword); free(paccount); } Qm::Qm(const string& shost, \ const int& port, \ const string& susername, \ const string& spassword, \ const string& saccount, \ int& success) { char *phost = NULL, *pusername = NULL, *ppassword = NULL, *paccount = NULL; // Convert all of the C++ strings to char* phost = CMALLOC(shost); pusername = CMALLOC(susername); ppassword = CMALLOC(spassword); paccount = CMALLOC(saccount); success = QMConnect(phost, port, pusername, ppassword, paccount); // Free all of the char* free(phost); free(pusername); free(ppassword); free(paccount); } Qm::~Qm() { QMEndCommand(); QMDisconnect(); } void Qm::qmCall(const string& ssubrname, const string& sarg1) { char *psubrname = NULL, *parg1 = NULL; // Convert the C++ strings to char* psubrname = CMALLOC(ssubrname); parg1 = CMALLOC(sarg1); QMCall(psubrname, 1, parg1); // Free all of the char* free(psubrname); free(parg1); } string Qm::qmChange(const string& ssrc, const string& sold, const string& snew, const int& occurences, const int& start) { char *psrc = NULL, *pold = NULL, *pnew = NULL, *pans = NULL; // Convert the C++ strings to char* psrc = CMALLOC(ssrc); pold = CMALLOC(sold); pnew = CMALLOC(snew); pans = QMChange(psrc, pold, pnew, occurences, start); string sans = (string)pans; QMFree(pans); // Free all of the char* free(psrc); free(pold); free(pnew); return sans; } void Qm::qmClearSelect(const int& listno) { QMClearSelect(listno); } void Qm::qmClose(const int& fileno) { QMClose(fileno); } bool Qm::qmConnected() { if(0 == QMConnected()) { return false; } else { return true; } } int Qm::qmDcount(const string& ssrc, const string& sdelim) { // Convert the C++ strings to char* char *psrc = CMALLOC(ssrc), *pdelim = CMALLOC(sdelim); int ans = QMDcount(psrc, pdelim); // Free all of the char* free(psrc); free(pdelim); return ans; } string Qm::qmDel(const string& ssrc, const int& fno, const int& vno, const int& svno) { // Convert the C++ strings to char* char *psrc = CMALLOC(ssrc); char *pans = QMDel(psrc, fno, vno, svno); string sans = (string)pans; QMFree(pans); // Free all of the char* free(psrc); return sans; } void Qm::qmDelete(const int& fileno, const string& sid) { // Convert the C++ strings to char* char *pid = CMALLOC(sid); QMDelete(fileno, pid); // Free all of the char* free(pid); } void Qm::qmDeleteu(const int& fileno, const string& sid) { // Convert the C++ strings to char* char *pid = CMALLOC(sid); QMDeleteu(fileno, pid); // Free all of the char* free(pid); } /* void qmDisconnect() { QMDisconnect(); } void qmDisconnectAll() { cout << "ASDFDisconnecting..." << endl; QMDisconnectAll(); cout << "Disconnected." << endl; } void qmEndCommand() { QMEndCommand(); } */ string Qm::qmError(void) { // Do NOT attempt to free this memory! return (string)QMError(); } string Qm::qmExecute(const string& scmnd, int& errno) { // Convert the C++ strings to char* char *pcmnd = CMALLOC(scmnd); char *pans = NULL; pans = QMExecute(pcmnd, &errno); string ans = (string)pans; QMFree(pans); // Free all of the char* free(pcmnd); return ans; } string Qm::qmExtract(const string& ssrcstr, const int& fno, const int& vno, const int& svno) { // Convert the C++ strings to char* char *psrcstr = CMALLOC(ssrcstr); char *pans = NULL; pans = QMExtract(psrcstr, fno, vno, svno); string ans = (string)pans; QMFree(pans); // Free all of the char* free(psrcstr); return ans; } string Qm::qmField(const string& ssrcstr, const string& sdelimiter, const int& start, const int& occurences) { // Convert the C++ strings to char* char *psrcstr = CMALLOC(ssrcstr), *pdelimiter = CMALLOC(sdelimiter); char* pans = NULL; pans = QMField(psrcstr, pdelimiter, start, occurences); string ans = (string)pans; free(pans); // Free all of the char* free(psrcstr); free(pdelimiter); return ans; } int Qm::qmGetSession() { return QMGetSession(); } string Qm::qmIns(const string& ssrcstr, const int& fno, const int& vno, const int& svno, const string& snewdata) { // Convert the C++ strings to char* char *psrcstr = CMALLOC(ssrcstr), *pnewdata = CMALLOC(snewdata); char* pans = NULL; pans = QMIns(psrcstr, fno, vno, svno, pnewdata); string ans = (string)pans; QMFree(pans); // Free all of the char* free(psrcstr); free(pnewdata); return ans; } int Qm::qmLocate(const string& sitem, const string& sdynarray, const int& fno, const int& vno, const int& svno, int* ppos, string sorder) { // Convert the C++ strings to char* char *pitem = CMALLOC(sitem), *pdynarray = CMALLOC(sdynarray), *porder = CMALLOC(sorder); int ans = QMLocate(pitem, pdynarray, fno, vno, svno, ppos, porder); // Free all of the char* free(pitem); free(pdynarray); free(porder); return ans; } int Qm::qmLogto(const string& saccount) { // Convert the C++ strings to char* char *paccount = CMALLOC(saccount); int ans = QMLogto(paccount); // Free all of the char* free(paccount); return ans; } void Qm::qmMarkMapping(const int& fileno, const int& state) { QMMarkMapping(fileno, state); } int Qm::qmMatch(const string& ssrcstr, const string& spattern) { // Convert the C++ strings to char* char *psrcstr = CMALLOC(ssrcstr), *ppattern = CMALLOC(spattern); int ans = QMMatch(psrcstr, ppattern); // Free all of the char* free(psrcstr); free(ppattern); return ans; } string Qm::qmMatchfield(const string& ssrcstr, const string& spattern, const int& component) { // Convert the C++ strings to char* char *psrcstr = CMALLOC(ssrcstr), *ppattern = CMALLOC(spattern); char *pans = NULL; pans = QMMatchfield(psrcstr, ppattern, component); string sans = (string)pans; QMFree(pans); // Free all of the char* free(psrcstr); free(ppattern); return sans; } int Qm::qmOpen(const string& sfilename) { // Convert the C++ strings to char* char *pfilename = CMALLOC(sfilename); int ans = QMOpen(pfilename); // Free all of the char* free(pfilename); return ans; } string Qm::qmRead(const int& fileno, const string& sid, int& errno) { // Convert the C++ strings to char* char *pid = CMALLOC(sid); char *pans = NULL; pans = QMRead(fileno, pid, &errno); string sans = (string)pans; QMFree(pans); // Free all of the char* free(pid); return sans; } string Qm::qmReadl(const int& fileno, const string& sid, const int& wait, int& errno) { // Convert the C++ strings to char* char *pid = CMALLOC(sid); char *pans = NULL; pans = QMReadl(fileno, pid, wait, &errno); string sans = (string)pans; QMFree(pans); // Free all of the char* free(pid); return sans; } string Qm::qmReadList(const int& listno) { char *pans = QMReadList(listno); string sans = (string)pans; QMFree(pans); return sans; } string Qm::qmReadNext(const int& listno) { char *pans = QMReadNext(listno); string sans = (string)pans; QMFree(pans); return sans; } string Qm::qmReadu(const int& fileno, const string& sid, const int& wait, int& errno) { // Convert the C++ strings to char* char *pid = CMALLOC(sid); char *pans = NULL; pans = QMReadu(fileno, pid, wait, &errno); string sans = (string)pans; QMFree(pans); // Free all of the char* free(pid); return sans; } void Qm::qmRecordlock(const int& fileno, const string& sid, const int& update, const int& wait) { // Convert the C++ strings to char* char *pid = CMALLOC(sid); QMRecordlock(fileno, pid, update, wait); // Free all of the char* free(pid); } void Qm::qmRelease(const int& fileno, const string& sid) { // Convert the C++ strings to char* char *pid = CMALLOC(sid); QMRelease(fileno, pid); // Free all of the char* free(pid); } string Qm::qmReplace(const string& ssrcstr, const int& fno, const int& vno, const int& svno, const string& snewdata) { // Convert the C++ strings to char* char *psrcstr = CMALLOC(ssrcstr), *pnewdata = CMALLOC(snewdata); char *pans = NULL; pans = QMReplace(psrcstr, fno, vno, svno, pnewdata); string sans = (string)pans; QMFree(pans); // Free all of the char* free(psrcstr); free(pnewdata); return sans; } string Qm::qmRespond(const string& sresponse, int& errno) { // Convert the C++ strings to char* char *presponse = CMALLOC(sresponse); char *pans = NULL; pans = QMRespond(presponse, &errno); string sans = (string)pans; QMFree(pans); // Free all of the char* free(presponse); return sans; } void Qm::qmSelect(const int& fileno, const int& listno) { QMSelect(fileno, listno); } void Qm::qmSelectIndex(const int& fileno, const string& sindexname, const string& sindexvalue, const int& listno) { // Convert the C++ strings to char* char *pindexname = CMALLOC(sindexname), *pindexvalue = CMALLOC(sindexvalue); QMSelectIndex(fileno, pindexname, pindexvalue, listno); // Free all of the char* free(pindexname); free(pindexvalue); } string Qm::qmSelectLeft(const int& fileno, const string& sindexname, const int& listno) { // Convert the C++ strings to char* char *pindexname = CMALLOC(sindexname); char *pans = NULL; pans = QMSelectLeft(fileno, pindexname, listno); string sans = (string)pans; QMFree(pans); // Free all of the char* free(pindexname); return sans; } string Qm::qmSelectRight(const int& fileno, const string& sindexname, const int& listno) { // Convert the C++ strings to char* char *pindexname = CMALLOC(sindexname); char *pans = NULL; pans = QMSelectRight(fileno, pindexname, listno); string sans = (string)pans; QMFree(pans); // Free all of the char* free(pindexname); return sans; } void Qm::qmSetLeft(const int& fileno, const string& sindexname) { // Convert the C++ strings to char* char *pindexname = CMALLOC(sindexname); QMSetLeft(fileno, pindexname); // Free all of the char* free(pindexname); } void Qm::qmSetRight(const int& fileno, const string& sindexname) { // Convert the C++ strings to char* char *pindexname = CMALLOC(sindexname); QMSetRight(fileno, pindexname); // Free all of the char* free(pindexname); } int Qm::qmSetSession(const int& session) { return QMSetSession(session); } int Qm::qmStatus() { return QMStatus(); } #if QMVERSION > 20606 void Qm::qmTrapCallAbort(const bool& mode) { TrapCallAbort(mode); } #endif void Qm::qmWrite(const int& fileno, const string& sid, const string& srec) { // Convert the C++ strings to char* char *pid = CMALLOC(sid), *prec = CMALLOC(srec); QMWrite(fileno, pid, prec); // Free all of the char* free(pid); free(prec); } void Qm::qmWriteu(const int& fileno, const string& sid, const string& srec) { // Convert the C++ strings to char* char *pid = CMALLOC(sid), *prec = CMALLOC(srec); QMWriteu(fileno, pid, prec); // Free all of the char* free(pid); free(prec); }