File indexing completed on 2024-04-28 15:35:16

0001 /***************************************************************************
0002  *   Copyright (C) 2004 by Tomas Mecir                                     *
0003  *   kmuddy@kmuddy.org                                                     *
0004  *                                                                         *
0005  *   This program is free software you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU Library General Public License as       *
0007  *   published by the Free Software Foundation either version 2 of the    *
0008  *   License, or (at your option) any later version.                       *
0009  *                                                                         *
0010  *   This program is distributed in the hope that it will be useful,       *
0011  *   but WITHOUT ANY WARRANTY without even the implied warranty of        *
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0013  *   GNU Library General Public License for more details.                  *
0014  ***************************************************************************/
0015 
0016 #include "cresulthandler.h"
0017 
0018 #include <string.h>
0019 
0020 cResultHandler::cResultHandler ()
0021 {
0022   returnedResult = 0;
0023 }
0024 
0025 
0026 cResultHandler::~cResultHandler ()
0027 {
0028   reset ();
0029 }
0030 
0031 void cResultHandler::reset ()
0032 {
0033   deleteReturned ();
0034   list<mxpResult *>::iterator it;
0035   for (it = results.begin(); it != results.end(); ++it)
0036     deleteResult (*it);
0037   results.clear ();
0038 }
0039 
0040 mxpResult *cResultHandler::nextResult ()
0041 {
0042   if (returnedResult)
0043     deleteReturned ();
0044   returnedResult = results.front ();
0045   results.pop_front ();
0046   return returnedResult;
0047 }
0048 
0049 bool cResultHandler::haveResults ()
0050 {
0051   return results.empty() ? false : true;
0052 }
0053 
0054 void cResultHandler::deleteReturned ()
0055 {
0056   if (returnedResult)
0057     deleteResult (returnedResult);
0058   returnedResult = 0;
0059 }
0060 
0061 
0062 //result creation methods coming now
0063 //the string->char* conversion takes place here
0064 
0065 /** type 0 */
0066 mxpResult *cResultHandler::createNothing ()
0067 {
0068   mxpResult *res = new mxpResult;
0069   res->type = 0;
0070   res->data = 0;
0071 
0072   return res;
0073 }
0074 
0075 /** type 1 */
0076 mxpResult *cResultHandler::createText (const string &text)
0077 {
0078   mxpResult *res = new mxpResult;
0079   res->type = 1;
0080   char *str = 0;
0081   if (!text.empty())
0082   {
0083     str = new char[text.length() + 1];
0084     strcpy (str, text.c_str());
0085   }
0086 
0087   res->data = (void *) str;
0088 
0089   return res;
0090 }
0091 
0092 /** type 2 */
0093 mxpResult *cResultHandler::createLineTag (int tag)
0094 {
0095   mxpResult *res = new mxpResult;
0096   res->type = 2;
0097   int *tg = new int;
0098   *tg = tag;
0099 
0100   res->data = (void *) tg;
0101 
0102   return res;
0103 }
0104 
0105 /** type 3 */
0106 mxpResult *cResultHandler::createFlag (bool begin, const string &flag)
0107 {
0108   mxpResult *res = new mxpResult;
0109   res->type = 3;
0110   flagStruct *fs = new flagStruct;
0111   fs->begin = begin;
0112   fs->name = 0;
0113   if (!flag.empty())
0114   {
0115     fs->name = new char[flag.length() + 1];
0116     strcpy (fs->name, flag.c_str());
0117   }
0118 
0119   res->data = (void *) fs;
0120 
0121   return res;
0122 }
0123 
0124 /** type 4 */
0125 mxpResult *cResultHandler::createVariable (const string &name, const string &value, bool erase)
0126 {
0127   mxpResult *res = new mxpResult;
0128   res->type = 4;
0129   varStruct *vs = new varStruct;
0130   vs->name = 0;
0131   if (!name.empty())
0132   {
0133     vs->name = new char[name.length() + 1];
0134     strcpy (vs->name, name.c_str());
0135   }
0136   vs->value = 0;
0137   if (!value.empty())
0138   {
0139     vs->value = new char[value.length() + 1];
0140     strcpy (vs->value, value.c_str());
0141   }
0142   vs->erase = erase;
0143 
0144   res->data = (void *) vs;
0145 
0146   return res;
0147 }
0148 
0149 /** type 5 */
0150 mxpResult *cResultHandler::createFormatting (unsigned char mask, unsigned char attributes,
0151     RGB fg, RGB bg, const string &font, int size)
0152 {
0153   mxpResult *res = new mxpResult;
0154   res->type = 5;
0155   formatStruct *fs = new formatStruct;
0156   fs->usemask = mask;
0157   fs->attributes = attributes;
0158   fs->fg = fg;
0159   fs->bg = bg;
0160   fs->size = size;
0161   fs->font = 0;
0162   if (!font.empty())
0163   {
0164     fs->font = new char[font.length() + 1];
0165     strcpy (fs->font, font.c_str());
0166   }
0167 
0168   res->data = (void *) fs;
0169 
0170   return res;
0171 }
0172 
0173 /** type 6 */
0174 mxpResult *cResultHandler::createLink (const string &name, const string &url, const string &text,
0175     const string &hint)
0176 {
0177   mxpResult *res = new mxpResult;
0178   res->type = 6;
0179   linkStruct *ls = new linkStruct;
0180   ls->name = ls->hint = ls->text = ls->url = 0;
0181   if (!name.empty())
0182   {
0183     ls->name = new char[name.length() + 1];
0184     strcpy (ls->name, name.c_str());
0185   }
0186   if (!hint.empty())
0187   {
0188     ls->hint = new char[hint.length() + 1];
0189     strcpy (ls->hint, hint.c_str());
0190   }
0191   if (!text.empty())
0192   {
0193     ls->text = new char[text.length() + 1];
0194     strcpy (ls->text, text.c_str());
0195   }
0196   if (!url.empty())
0197   {
0198     ls->url = new char[url.length() + 1];
0199     strcpy (ls->url, url.c_str());
0200   }
0201 
0202   res->data = (void *) ls;
0203 
0204   return res;
0205 }
0206 
0207 /** type 7 */
0208 mxpResult *cResultHandler::createSendLink (const string &name, const string &command,
0209     const string &text, const string &hint, bool prompt, bool ismenu)
0210 {
0211   mxpResult *res = new mxpResult;
0212   res->type = 7;
0213   sendStruct *ss = new sendStruct;
0214   ss->name = ss->command = ss->hint = ss->text = 0;
0215   if (!name.empty())
0216   {
0217     ss->name = new char[name.length() + 1];
0218     strcpy (ss->name, name.c_str());
0219   }
0220   if (!command.empty())
0221   {
0222     ss->command = new char[command.length() + 1];
0223     strcpy (ss->command, command.c_str());
0224   }
0225   if (!hint.empty())
0226   {
0227     ss->hint = new char[hint.length() + 1];
0228     strcpy (ss->hint, hint.c_str());
0229   }
0230   if (!text.empty())
0231   {
0232     ss->text = new char[text.length() + 1];
0233     strcpy (ss->text, text.c_str());
0234   }
0235   ss->toprompt = prompt;
0236   ss->ismenu = ismenu;
0237 
0238   res->data = (void *) ss;
0239 
0240   return res;
0241 }
0242 
0243 /** type 8 */
0244 mxpResult *cResultHandler::createExpire (const string &name)
0245 {
0246   mxpResult *res = new mxpResult;
0247   res->type = 8;
0248   char *str = 0;
0249   if (!name.empty())
0250   {
0251     str = new char[name.length() + 1];
0252     strcpy (str, name.c_str());
0253   }
0254 
0255   res->data = (void *) str;
0256 
0257   return res;
0258 }
0259 
0260 /** type 9 */
0261 mxpResult *cResultHandler::createSendThis (const string &command)
0262 {
0263   mxpResult *res = new mxpResult;
0264   res->type = 9;
0265   char *str = 0;
0266   if (!command.empty())
0267   {
0268     str = new char[command.length() + 1];
0269     strcpy (str, command.c_str());
0270   }
0271 
0272   res->data = (void *) str;
0273 
0274   return res;
0275 }
0276 
0277 /** type 10 */
0278 mxpResult *cResultHandler::createHorizLine ()
0279 {
0280   mxpResult *res = new mxpResult;
0281   res->type = 10;
0282   res->data = 0;
0283 
0284   return res;
0285 }
0286 
0287 /** type 11 */
0288 mxpResult *cResultHandler::createSound (bool isSOUND, const string &fname, int vol, int count,
0289     int priority, bool contifrereq, const string &type, const string &url)
0290 {
0291   mxpResult *res = new mxpResult;
0292   res->type = 11;
0293   soundStruct *ss = new soundStruct;
0294   ss->fname = ss->type = ss->url = 0;
0295   if (!fname.empty())
0296   {
0297     ss->fname = new char[fname.length() + 1];
0298     strcpy (ss->fname, fname.c_str());
0299   }
0300   if (!type.empty())
0301   {
0302     ss->type = new char[type.length() + 1];
0303     strcpy (ss->type, type.c_str());
0304   }
0305   if (!url.empty())
0306   {
0307     ss->url = new char[url.length() + 1];
0308     strcpy (ss->url, url.c_str());
0309   }
0310   ss->isSOUND = isSOUND;
0311   ss->vol = vol;
0312   ss->repeats = count;
0313   ss->priority = priority;
0314   ss->continuemusic = contifrereq;
0315 
0316   res->data = (void *) ss;
0317 
0318   return res;
0319 }
0320 
0321 /** type 12 */
0322 mxpResult *cResultHandler::createWindow (const string &name, const string &title,
0323     int left, int top, int width, int height, bool scrolling, bool floating)
0324 {
0325   mxpResult *res = new mxpResult;
0326   res->type = 12;
0327   windowStruct *ws = new windowStruct;
0328   ws->name = ws->title = 0;
0329   if (!name.empty())
0330   {
0331     ws->name = new char[name.length() + 1];
0332     strcpy (ws->name, name.c_str());
0333   }
0334   if (!title.empty())
0335   {
0336     ws->title = new char[title.length() + 1];
0337     strcpy (ws->title, title.c_str());
0338   }
0339   ws->left = left;
0340   ws->top = top;
0341   ws->width = width;
0342   ws->height = height;
0343   ws->scrolling = scrolling;
0344   ws->floating = floating;
0345 
0346   res->data = (void *) ws;
0347 
0348   return res;
0349 }
0350 
0351 /** type 13 */
0352 mxpResult *cResultHandler::createInternalWindow (const string &name, const string &title,
0353     alignType align, bool scrolling)
0354 {
0355   mxpResult *res = new mxpResult;
0356   res->type = 13;
0357   internalWindowStruct *ws = new internalWindowStruct;
0358   ws->name = ws->title = 0;
0359   if (!name.empty())
0360   {
0361     ws->name = new char[name.length() + 1];
0362     strcpy (ws->name, name.c_str());
0363   }
0364   if (!title.empty())
0365   {
0366     ws->title = new char[title.length() + 1];
0367     strcpy (ws->title, title.c_str());
0368   }
0369   ws->align = align;
0370   ws->scrolling = scrolling;
0371 
0372   res->data = (void *) ws;
0373 
0374   return res;
0375 }
0376 
0377 /** type 14 */
0378 mxpResult *cResultHandler::createCloseWindow (const string &name)
0379 {
0380   mxpResult *res = new mxpResult;
0381   res->type = 14;
0382   char *str = 0;
0383   if (!name.empty())
0384   {
0385     str = new char[name.length() + 1];
0386     strcpy (str, name.c_str());
0387   }
0388   res->data = (void *) str;
0389 
0390   return res;
0391 }
0392 
0393 /** type 15 */
0394 mxpResult *cResultHandler::createSetWindow (const string &name)
0395 {
0396   mxpResult *res = new mxpResult;
0397   res->type = 15;
0398   char *str = 0;
0399   if (!name.empty())
0400   {
0401     str = new char[name.length() + 1];
0402     strcpy (str, name.c_str());
0403   }
0404 
0405   res->data = (void *) str;
0406 
0407   return res;
0408 }
0409 
0410 /** type 16 */
0411 mxpResult *cResultHandler::createMoveCursor (int x, int y)
0412 {
0413   mxpResult *res = new mxpResult;
0414   res->type = 16;
0415   moveStruct *ms = new moveStruct;
0416   ms->x = x;
0417   ms->y = y;
0418 
0419   res->data = (void *) ms;
0420 
0421   return res;
0422 }
0423 
0424 /** type 17 */
0425 mxpResult *cResultHandler::createEraseText (bool restofframe)
0426 {
0427   mxpResult *res = new mxpResult;
0428   res->type = 17;
0429   res->data = (void *) (restofframe ? 1 : 0);
0430 
0431   return res;
0432 }
0433 
0434 /** type 18 */
0435 mxpResult *cResultHandler::createRelocate (const string &server, int port)
0436 {
0437   mxpResult *res = new mxpResult;
0438   res->type = 18;
0439   relocateStruct *rs = new relocateStruct;
0440   rs->server = 0;
0441   if (!server.empty())
0442   {
0443     rs->server = new char[server.length() + 1];
0444     strcpy (rs->server, server.c_str());
0445   }
0446   rs->port = port;
0447 
0448   res->data = (void *) rs;
0449 
0450   return res;
0451 }
0452 
0453 /** type 19 */
0454 mxpResult *cResultHandler::createSendLogin (bool username)
0455 {
0456   mxpResult *res = new mxpResult;
0457   res->type = 19;
0458   res->data = (void *) (username ? 1 : 0);
0459 
0460   return res;
0461 }
0462 
0463 /** type 20 */
0464 mxpResult *cResultHandler::createImage (const string &fname, const string &url, const string &type,
0465     int height, int width, int hspace, int vspace, alignType align)
0466 {
0467   mxpResult *res = new mxpResult;
0468   res->type = 20;
0469   imageStruct *is = new imageStruct;
0470   is->fname = is->url = is->type = 0;
0471   if (!fname.empty())
0472   {
0473     is->fname = new char[fname.length() + 1];
0474     strcpy (is->fname, fname.c_str());
0475   }
0476   if (!url.empty())
0477   {
0478     is->url = new char[url.length() + 1];
0479     strcpy (is->url, url.c_str());
0480   }
0481   if (!type.empty())
0482   {
0483     is->type = new char[type.length() + 1];
0484     strcpy (is->type, type.c_str());
0485   }
0486   is->height = height;
0487   is->width = width;
0488   is->hspace = hspace;
0489   is->vspace = vspace;
0490   is->align = align;
0491 
0492   res->data = (void *) is;
0493 
0494   return res;
0495 }
0496 
0497 /** type 21 */
0498 mxpResult *cResultHandler::createImageMap (const string &name)
0499 {
0500   mxpResult *res = new mxpResult;
0501   res->type = 21;
0502   char *str = 0;
0503   if (!name.empty())
0504   {
0505     str = new char[name.length() + 1];
0506     strcpy (str, name.c_str());
0507   }
0508 
0509   res->data = (void *) str;
0510 
0511   return res;
0512 }
0513 
0514 /** type 22 */
0515 mxpResult *cResultHandler::createGauge (const string &variable, const string &maxvariable,
0516     const string &caption, RGB color)
0517 {
0518   mxpResult *res = new mxpResult;
0519   res->type = 22;
0520   gaugeStruct *gs = new gaugeStruct;
0521   gs->variable = gs->maxvariable = gs->caption = 0;
0522   if (!variable.empty())
0523   {
0524     gs->variable = new char[variable.length() + 1];
0525     strcpy (gs->variable, variable.c_str());
0526   }
0527   if (!maxvariable.empty())
0528   {
0529     gs->maxvariable = new char[maxvariable.length() + 1];
0530     strcpy (gs->maxvariable, maxvariable.c_str());
0531   }
0532   if (!caption.empty())
0533   {
0534     gs->caption = new char[caption.length() + 1];
0535     strcpy (gs->caption, caption.c_str());
0536   }
0537   gs->color = color;
0538 
0539   res->data = (void *) gs;
0540 
0541   return res;
0542 }
0543 
0544 /** type 23 */
0545 mxpResult *cResultHandler::createStat (const string &variable, const string &maxvariable,
0546     const string &caption)
0547 {
0548   mxpResult *res = new mxpResult;
0549   res->type = 23;
0550   statStruct *ss = new statStruct;
0551   ss->variable = ss->maxvariable = ss->caption = 0;
0552   if (!variable.empty())
0553   {
0554     ss->variable = new char[variable.length() + 1];
0555     strcpy (ss->variable, variable.c_str());
0556   }
0557   if (!maxvariable.empty())
0558   {
0559     ss->maxvariable = new char[maxvariable.length() + 1];
0560     strcpy (ss->maxvariable, maxvariable.c_str());
0561   }
0562   if (!caption.empty())
0563   {
0564     ss->caption = new char[caption.length() + 1];
0565     strcpy (ss->caption, caption.c_str());
0566   }
0567 
0568   res->data = (void *) ss;
0569 
0570   return res;
0571 }
0572 
0573 
0574 /** type -1 */
0575 mxpResult *cResultHandler::createError (const string &error)
0576 {
0577   mxpResult *res = new mxpResult;
0578   res->type = -1;
0579   char *str = 0;
0580   if (!error.empty())
0581   {
0582     str = new char[error.length() + 1];
0583     strcpy (str, error.c_str());
0584   }
0585 
0586   res->data = (void *) str;
0587 
0588   return res;
0589 }
0590 
0591 /** type -2 */
0592 mxpResult *cResultHandler::createWarning (const string &warning)
0593 {
0594   mxpResult *res = new mxpResult;
0595   res->type = -2;
0596   char *str = 0;
0597   if (!warning.empty())
0598   {
0599     str = new char[warning.length() + 1];
0600     strcpy (str, warning.c_str());
0601   }
0602 
0603   res->data = (void *) str;
0604 
0605   return res;
0606 }
0607 
0608 
0609 void cResultHandler::addToList (mxpResult *res)
0610 {
0611   if (res)
0612     results.push_back (res);
0613 }
0614 
0615 /** delete this result */
0616 void cResultHandler::deleteResult (mxpResult *res)
0617 {
0618   if (!res)
0619     return;
0620 
0621   switch (res->type) {
0622     case 1:
0623     case 8:
0624     case 9:
0625     case 14:
0626     case 15:
0627     case 21:
0628     case -1:
0629     case -2:
0630       delete[] (char *) res->data;
0631       break;
0632     case 2:
0633       delete[] (int *) res->data;
0634       break;
0635     case 3: {
0636       flagStruct *fs = (flagStruct *) res->data;
0637       delete[] fs->name;
0638       delete fs;
0639       break;
0640     }
0641     case 4: {
0642       varStruct *vs = (varStruct *) res->data;
0643       delete[] vs->name;
0644       delete[] vs->value;
0645       delete vs;
0646       break;
0647     }
0648     case 5: {
0649       formatStruct *fs = (formatStruct *) res->data;
0650       delete[] fs->font;
0651       delete fs;
0652       break;
0653     }
0654     case 6: {
0655       linkStruct *ls = (linkStruct *) res->data;
0656       delete[] ls->name;
0657       delete[] ls->hint;
0658       delete[] ls->text;
0659       delete[] ls->url;
0660       delete ls;
0661       break;
0662     }
0663     case 7: {
0664       sendStruct *ss = (sendStruct *) res->data;
0665       delete[] ss->name;
0666       delete[] ss->command;
0667       delete[] ss->hint;
0668       delete[] ss->text;
0669       delete ss;
0670       break;
0671     }
0672     //case 10: NOTHING
0673     case 11: {
0674       soundStruct *ss = (soundStruct *) res->data;
0675       delete[] ss->fname;
0676       delete[] ss->type;
0677       delete[] ss->url;
0678       delete ss;
0679       break;
0680     }
0681     case 12: {
0682       windowStruct *ws = (windowStruct *) res->data;
0683       delete[] ws->name;
0684       delete[] ws->title;
0685       delete ws;
0686       break;
0687     }
0688     case 13: {
0689       internalWindowStruct *iws = (internalWindowStruct *) res->data;
0690       delete[] iws->name;
0691       delete[] iws->title;
0692       delete iws;
0693       break;
0694     }
0695     case 16: {
0696       moveStruct *ms = (moveStruct *) res->data;
0697       delete ms;
0698       break;
0699     }
0700     //case 17: NOTHING
0701     case 18: {
0702       relocateStruct *rs = (relocateStruct *) res->data;
0703       delete[] rs->server;
0704       delete rs;
0705       break;
0706     }
0707     //case 19: NOTHING
0708     case 20: {
0709       imageStruct *is = (imageStruct *) res->data;
0710       delete[] is->fname;
0711       delete[] is->url;
0712       delete[] is->type;
0713       delete is;
0714       break;
0715     }
0716     case 22: {
0717       gaugeStruct *gs = (gaugeStruct *) res->data;
0718       delete[] gs->variable;
0719       delete[] gs->maxvariable;
0720       delete[] gs->caption;
0721       delete gs;
0722       break;
0723     };
0724     case 23: {
0725       statStruct *ss = (statStruct *) res->data;
0726       delete[] ss->variable;
0727       delete[] ss->maxvariable;
0728       delete[] ss->caption;
0729       delete ss;
0730       break;
0731     };
0732   };
0733   delete res;
0734 }