GME  13
Helper.cpp
Go to the documentation of this file.
00001 #include "stdafx.h"
00002 #include "helper.h"
00003 
00004 //
00005 // bool include_separator:
00006 //      true : includes the separator in the token
00007 //      false: does not include the separator character in the token
00008 std::list<std::tstring> tokenizer( std::tstring m, TCHAR separator /*= '/' */, bool include_separator /* = true */)
00009 {
00010         int offs = include_separator?0:1;
00011         std::list<std::tstring> r;
00012         std::tstring last;
00013         while( !m.empty())
00014         {
00015                 int i = m.find(separator, 1);
00016                 if( i == -1)
00017                 {
00018                         r.push_back( m);
00019                         m = _T("");
00020                 }
00021                 else if( i != 0)
00022                 {
00023                         r.push_back( m.substr( 0, i));
00024                         m = m.substr( i + offs);
00025                 }
00026         }
00027 
00028         return r;
00029 }
00030 
00031 //
00032 // does not include the separator character in the token
00033 bool findExact( const std::tstring& m, const std::tstring& to_find)
00034 {
00035         std::list< std::tstring> tokens = tokenizer( m, _T(' '), false);
00036         std::list< std::tstring>::iterator i = tokens.begin();
00037         std::list< std::tstring>::iterator e = tokens.end();
00038         while( i != e && (*i).compare( to_find) != 0)
00039                 ++i;
00040 
00041         return ( i != e); // false if not found
00042 }
00043 
00044 // it calculates nm1's name relatively to nm2
00045 std::tstring makeRel( std::tstring& nm1, std::tstring& nm2)
00046 {
00047         std::list<std::tstring> r1 = tokenizer( nm1);
00048         std::list<std::tstring> r2 = tokenizer( nm2);
00049 
00050         std::list<std::tstring>::iterator i1 = r1.begin();
00051         std::list<std::tstring>::iterator i2 = r2.begin();
00052         while( i1 != r1.end() && i2 != r2.end() && *i1 == *i2)
00053         {
00054                 ++i1;
00055                 ++i2;
00056         }
00057 
00058         std::tstring relpath;
00059         int count = 0; // how many levels in nm2
00060         while( i2 != r2.end()) ++i2, ++count, relpath +=_T("/@..");
00061         while( i1 != r1.end()) relpath +=*i1, ++i1;
00062 
00063         if( relpath == _T("")) // same name nm1 and nm2
00064         {
00065                 ASSERT( nm1 == nm2);
00066                 //#1 solution: to give back the path as it goes back to the topmost level
00067                 // and then goes down again
00068                 /*for( i2 = r2.begin(); i2 != r2.end(); ++i2) 
00069                         relpath +="/@..";
00070                 relpath += nm1;*/
00071 
00072                 //#2 solution: to use a shorter form (only one step up): /@../name
00073                 relpath = _T("/@..") + *r2.rbegin();
00074         }
00075         return relpath;
00076 }
00077 
00078 std::tstring makeViewable( const std::tstring& m)
00079 {
00080         std::tstring res;
00081         std::list<std::tstring> r1 = tokenizer( m);
00082         std::list<std::tstring>::iterator i1 = r1.begin();
00083         while( i1 != r1.end())
00084         {
00085                 std::tstring p = *i1;
00086                 if( p.substr(0, 2) == _T("/@"))
00087                         p = p.substr(2); // _T("/@")
00088                 int pos = p.find(_T("|kind="));
00089                 if( pos != -1) p = p.substr( 0, pos);
00090 
00091                 res += _T('/') + p;
00092                 ++i1;
00093         }
00094         
00095         return res;
00096 }
00097 
00098 
00099 std::tstring makeNameViewable( const std::tstring& m)
00100 {
00101         std::tstring res = m;
00102         if( res.substr(0, 2) == _T("/@"))
00103                 res = res.substr(2);
00104 
00105         ASSERT( res.find( _T("/@")) == -1); //no more than one token
00106 
00107         int pos = res.find(_T("|kind="));
00108         if( pos != -1)
00109                 res = res.substr( 0, pos);
00110 
00111         return res;
00112 }
00113 
00114 CComBSTR makeLink( CComObjPtr<IMgaObject> p_obj, const std::tstring& nm2, bool use_anyway_nm_2)
00115 {
00116         CComBSTR bstr, id, nm;
00117         COMTHROW( p_obj->get_ID( &id));
00118         COMTHROW( p_obj->get_Name( &nm));
00119         if( nm == 0 || nm == _T("") || use_anyway_nm_2) // if called from the Start... (or just derived) then the name its not yet parsed
00120                 nm = nm2.c_str();
00121         COMTHROW(bstr.Append(L"<A HREF=\"mga:"));
00122         COMTHROW(bstr.AppendBSTR( id));
00123         COMTHROW(bstr.Append(L"\">"));
00124         if( nm.Length() != 0)
00125                 COMTHROW(bstr.AppendBSTR( nm));
00126         else
00127                 COMTHROW(bstr.Append(L"emptyname"));
00128         COMTHROW(bstr.Append(L"</A>"));
00129 
00130         return bstr;
00131 }
00132 
00133 CComBSTR makeLink( CComObjPtr<IMgaFCO> p_fco, const std::tstring& nm_2, bool use_anyway_nm_2)
00134 {
00135         return makeLink( CComObjPtr<IMgaObject>( p_fco), nm_2, use_anyway_nm_2);
00136 }
00137 
00138 CComBSTR makeLink( CComObjPtr<IMgaSet> p_fco, const std::tstring& nm_2, bool use_anyway_nm_2)
00139 {
00140         return makeLink( CComObjPtr<IMgaObject>( p_fco), nm_2, use_anyway_nm_2);
00141 }
00142 
00143 CComBSTR makeLink( CComObjPtr<IMgaReference> p_fco, const std::tstring& nm_2, bool use_anyway_nm_2)
00144 {
00145         return makeLink( CComObjPtr<IMgaObject>( p_fco), nm_2, use_anyway_nm_2);
00146 }