GME  13
TokenEx.cpp
Go to the documentation of this file.
00001 // TokenEx.cpp: implementation of the CTokenEx class.
00002 //
00003 //      MFC Simple CString Tokenizer (Version 1)
00004 //      
00005 //      Written by Daniel Madden (daniel.madden@compaq.com)
00006 //  Copyright (c) 1999
00007 //
00008 // This code may be used in compiled form in any way you desire. This
00009 // file may be redistributed unmodified by any means PROVIDING it is 
00010 // not sold for profit without the authors written consent, and 
00011 // providing that this notice and the authors name and all copyright 
00012 // notices remains intact. 
00013 //
00014 // This file is provided "as is" with no expressed or implied warranty.
00015 // The author accepts no liability for any damage/loss of business that
00016 // this product may cause.
00017 //
00018 // Please use and enjoy. Please let me know of any bugs/mods/improvements 
00019 // that you have found/implemented and I will fix/incorporate them into this
00020 // file. 
00021 //
00022 //
00024 
00025 #include "stdafx.h"
00026 #include "TokenEx.h"
00027 
00028 #ifdef _DEBUG
00029 #undef THIS_FILE
00030 static char THIS_FILE[]=__FILE__;
00031 #define new DEBUG_NEW
00032 #endif
00033 
00035 // Construction/Destruction
00037 
00038 CTokenEx::CTokenEx()
00039 {
00040 }
00041 
00042 CTokenEx::~CTokenEx()
00043 {
00044 }
00045 
00046 CString CTokenEx::GetString(CString &tmp, CString Search)
00047 {
00048         int nFound;
00049         CString csRet;
00050 
00051         
00052         CString csTmp1 = tmp;
00053         if (tmp.Left(Search.GetLength()) == Search) {
00054                 do {
00055                         tmp = csTmp1.Mid(Search.GetLength());
00056                         csTmp1 = tmp;
00057                 } while (tmp.Left(Search.GetLength()) == Search);
00058                 tmp = csTmp1.Mid(Search.GetLength() - 1);
00059         }
00060 
00061         CString csTmp = tmp;
00062         nFound = tmp.Find(Search,0);
00063         if (nFound >= 0) {
00064                 csRet = csTmp.Left(nFound);
00065                 tmp = csTmp.Mid(nFound + (Search.GetLength()));
00066         }
00067         else {
00068                 csRet = csTmp;
00069                 tmp = "";
00070         }
00071 
00072         return csRet;
00073 }
00074 
00075 
00076 CString CTokenEx::Join(CString Deliminator, CStringArray& AddIt)
00077 {
00078         CString csReturn;
00079         CString csTmp;
00080 
00081         // Loop through the Array and Append the Deliminator
00082         for( int iNum = 0; iNum < AddIt.GetSize(); iNum++ ) {
00083                 csTmp += AddIt.GetAt(iNum);
00084                 csTmp += Deliminator;
00085         }
00086         csReturn = csTmp.Left(csTmp.GetLength() - 1);
00087         return csReturn;
00088 }
00089 
00090 
00091 void CTokenEx::Split(CString Source, CString Deliminator, CStringArray& AddIt)
00092 {
00093         CString          newCString;
00094         CString          tmpCString;
00095         CString          AddCString;
00096 
00097         int pos1 = 0;
00098         int pos = 0;
00099 
00100         newCString = Source;
00101         do {
00102                 pos1 = 0;
00103                 pos = newCString.Find(Deliminator, pos1);
00104                 if ( pos != -1 ) {
00105                         CString AddCString = newCString.Left(pos);
00106                         if (!AddCString.IsEmpty())
00107                                 AddIt.Add(AddCString);
00108 
00109                         tmpCString = newCString.Mid(pos + Deliminator.GetLength());
00110                         newCString = tmpCString;
00111                 }
00112         } while ( pos != -1 );
00113         
00114         if (!newCString.IsEmpty())
00115                 AddIt.Add(newCString);
00116 }
00117 
00118 
00119 void CTokenEx::SplitPath (BOOL UsingDirsOnly, CString Path, CString& Drive, CString& Dir, CString& FName, CString& Ext)
00120 {
00121 
00122         int nSecond;
00123 
00124         // Look for a UNC Name!
00125         if (Path.Left(2) == _T("\\\\")) {
00126                 int nFirst = Path.Find(_T("\\"),3);
00127                 nSecond = Path.Find(_T("\\"),nFirst + 1);
00128                 if (nSecond == -1) {
00129                         Drive = Path;
00130                         Dir = _T("");
00131                         FName = _T("");
00132                         Ext = _T("");
00133                 }
00134                 else if (nSecond > nFirst)
00135                         Drive = Path.Left(nSecond);
00136         }
00137         else { // Look for normal Drive Structure
00138                 nSecond = 2;
00139                 Drive = Path.Left(2);
00140         }
00141 
00142         if (UsingDirsOnly) {
00143                 Dir = Path.Right((Path.GetLength() - nSecond) - 1);
00144                 FName = _T("");
00145                 Ext = _T("");
00146         }
00147         else {
00148                 int nDirEnd = Path.ReverseFind('\\');
00149                 if (nDirEnd == Path.GetLength()) {
00150                         Dir = _T("");
00151                         FName = _T("");
00152                         Ext = _T("");
00153                 }
00154                 else {
00155 
00156                         Dir = Path.Mid(nSecond + 1, (nDirEnd - nSecond) - 1);
00157 
00158                         int nFileEnd = Path.ReverseFind('.');
00159                         if (nFileEnd != -1) {
00160                                 
00161                                 if (nDirEnd > nFileEnd) {
00162                                         FName = Path.Right(Path.GetLength() - nDirEnd);
00163                                         Ext = _T("");
00164                                 }
00165                                 else {
00166                                         FName = Path.Mid(nDirEnd + 1, (nFileEnd - nDirEnd) - 1);
00167                                         Ext = Path.Right((Path.GetLength() - nFileEnd) - 1);
00168                                 }
00169                         }
00170                         else {
00171                                 FName = Path.Right((Path.GetLength() - nDirEnd) - 1);
00172                                 Ext = _T("");
00173                         }
00174                 }
00175         }
00176 }