1: /* Cfour (C++ Common Console Classes)
     2:  * Copyright (C) 2001 Jeffrey Bakker
     3:  *
     4:  * This program is free software; you can redistribute it and/or modify
     5:  * it under the terms of the GNU General Public License as published by
     6:  * the Free Software Foundation; either version 2 of the License, or
     7:  * (at your option) any later version.
     8:  *
     9:  * This program is distributed in the hope that it will be useful,
    10:  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11:  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12:  * GNU General Public License for more details.
    13:  *
    14:  * You should have received a copy of the GNU General Public License
    15:  * along with this program; if not, write to the Free Software
    16:  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
    17:  *
    18:  *
    19:  *  Author:              Jeffrey Bakker
    20:  *  Filename:            cfdatapair.cpp
    21:  *  File version:        1.1
    22:  *
    23:  *  ===========================================================================
    24:  *
    25:  *  Created on: Sept 2nd, 2001
    26:  *
    27:  *  Modified on: April 1st, 2002
    28:  *
    29:  *             - Added translateML() which takes data markup format,
    30:  *               and modifies name and value.
    31:  *             - Added info_ml(), changed info() to info_nv().
    32:  *             - Added a constructor which takes a string in markup format.
    33:  *             - Overloaded insertion and extraction operators to convert
    34:  *               data between pairs and maarkup like a stream call.
    35:  *             - Made getName() and getValue() as const methods.
    36:  *
    37:  *  ===========================================================================
    38:  *
    39:  *  Remark:
    40:  *
    41:  *  This is the class implementaion for CFdataPair, a class which provides
    42:  *  methods and data members for storing pairs of data.
    43:  *
    44:  *  ===========================================================================
    45:  * _______. ..
    46:  */
    47: 
    48: 
    49: #include "cfdatapair.h"  
    50: 
    51: // overloaded stream operators ------------------------------------------------
    52: void CFdatapair::operator<<(string  mldata) {translateML(mldata);}
    53: void CFdatapair::operator>>(string& mldata) {mldata = info_ml();}
    54: // Constructors ---------------------------------------------------------------
    55: CFdatapair::CFdatapair()                   {name = "name"; value = "value";}
    56: CFdatapair::CFdatapair(string mldata)      {translateML(mldata);}
    57: CFdatapair::CFdatapair(string n, string v) {name = n; value = v;}
    58: // set methods ----------------------------------------------------------------
    59: void CFdatapair::setname(string n)  {name  = n;} // changes the name
    60: void CFdatapair::setvalue(string v) {value = v;} // changes the value
    61: // get methods ----------------------------------------------------------------
    62: string CFdatapair::getname()  const {return name;}  // returns the name
    63: string CFdatapair::getvalue() const {return value;} // returns the value
    64: //-----------------------------------------------------------------------------
    65: string CFdatapair::info_nv()  const { // return the name and value in a string
    66: 
    67:  return name + ": " + value;
    68: } //---------------------------------------------------------------------------
    69: string CFdatapair::info_ml()  const { // return name and value in markup format
    70: 
    71:  return ("<" + name + ">" + value + "</" + name + ">");
    72: } //---------------------------------------------------------------------------
    73: // swaps the name/value -------------------------------------------------------
    74: void CFdatapair::swap() {
    75:  string temp;
    76:  temp  = name;
    77:  name  = value;
    78:  value = temp;
    79: } //---------------------------------------------------------------------------
    80: // convert XML-Style string data into name/value pair ------------------------- 
    81: void CFdatapair::translateML(string mldata) {
    82: 
    83:  string ntmp, vtmp;
    84:  int    open, close;
    85:  
    86:  open  = mldata.find("<",0);
    87:  close = mldata.find(">",open);
    88:  ntmp  = mldata.substr(open+1,(close - open) -1);
    89: // ntmp  = mldata.substr(open+1,close-1);
    90: 
    91:  open  = mldata.find("</",close);
    92:  vtmp  = mldata.substr(close+1,(open - close -1));
    93: 
    94:  name  = ntmp;
    95:  value = vtmp;
    96: }
    97: //-----------------------------------------------------------------------------
    98: 
    99: 



w e b c p p
web c plus plus