CodeSarang.Com
Home | 전체 메뉴 | 질문/답변 Join | Login | 검색   

 

TStringList 구현 (string, vector 이용)

등록자 : cpueblo (유광희), 2009-03-13

string 과 vector 를 이용해서 VCL 의 TStringList 과 흡사한 클래스를 구현합니다

std 의 string 과 vector 를 이용해서 TStringList 를 구현하였습니다

void Add(const string &String); void Clear(); void Delete(int Index) void Insert(int Index, const string &s) int IndexOf(const string &String) int IndexOfStrStr(const string &String) int IndexOfStrStrExchange(const string &String) int IndexOfNamesStrStrExchange(const string &String) void Exchange(int Index1, int Index2) void Explode(const string &Delimiter, const string &String); void Trace(const string &Title = ""); void LoadFromFile(const string &FileName); void SaveToFile(const string &FileName); 프로퍼티 string Strings[]; 프로퍼티 Count; ex) TStringList a; a.Add("하하"); TRACE(a.Strings[0]); a.Delete(0); for (int i = 0; i < a.Count; i++) TRACE ("문자열 = %s\n", a.Strings[i].c_str());

TStringList.h

#ifndef __CPUEBLO_TSTRINGLIST_H__ #define __CPUEBLO_TSTRINGLIST_H__ // // TStringList // // 최종 수정일 : 2009-02-25 // 작성 : 유광희. cpueblo@cpueblo.com // dependency : String.h // #include "String.h" namespace cpueblo { class TStringList { public: TStringList() {}; ~TStringList(); // // ADD // void Add(const string &String) { FList.push_back(String); } // // 전체 삭제 // void Clear() { FList.clear(); } // // 삭제 // void Delete(int Index) { FList.erase(FList.begin() + Index); } // // 추가 // void Insert(int Index, const string &s) { FList.insert(FList.begin() + Index, s); } // // 검색 = strcmp // int IndexOf(const string &String) { int Index = 0; for (vector<string>::iterator it = FList.begin(); it != FList.end(); it++, Index++ ) { if (String == *it) return Index; } return -1; } // // 검색 = strstr // int IndexOfStrStr(const string &String) { int Index = 0; char *pStr = (char *)String.c_str(); for (vector<string>::iterator it = FList.begin(); it != FList.end(); it++, Index++ ) { if (strstr(it->c_str(), pStr)) return Index; } return -1; } // // 검색 = strstr. 비교 대상을 바꿈 // int IndexOfStrStrExchange(const string &String) { int Index = 0; char *pStr = (char *)String.c_str(); for (vector<string>::iterator it = FList.begin(); it != FList.end(); it++, Index++ ) { if (strstr(pStr, it->c_str())) return Index; } return -1; } // // 검색 = strstr. 비교 대상을 바꿈 // int IndexOfNamesStrStrExchange(const string &String) { int Index = 0; char *pStr = (char *)String.c_str(); for (vector<string>::iterator it = FList.begin(); it != FList.end(); it++, Index++ ) { string sTarget = str_GetName(*it); if (strstr(pStr, sTarget.c_str())) return Index; } return -1; } int IndexOfNamesStrCmpExchange(const string &String) { int Index = 0; char *pStr = (char *)String.c_str(); for (vector<string>::iterator it = FList.begin(); it != FList.end(); it++, Index++ ) { string sTarget = str_GetName(*it); if (!strcmp(pStr, sTarget.c_str())) return Index; } return -1; } // // Swap 기능 // void Exchange(int Index1, int Index2) { string s1 = FList[Index1]; string s2 = FList[Index2]; FList[Index1] = s2; FList[Index2] = s1; } // // 문자열을 문자열로 나눔 list.explode(" ", "하하 히히 호호"); // void Explode(const string &Delimiter, const string &String); #ifdef _DEBUG void Trace(const string &Title = ""); #else void Trace(const string &Title = "") {}; #endif void LoadFromFile(const string &FileName); void SaveToFile(const string &FileName); vector<string> *Get() { return &FList; } public: __declspec(property(get=FGetStrings, put=FPutStrings)) string Strings[]; __declspec(property(get=FGetCount)) int Count; public: // // string String[Index]; // string FGetStrings(int Index) { return FList[Index]; } int FGetCount() { return (int)FList.size(); }; private: std::vector <string> FList; }; }; #endif

TStringList.cpp

#include "StdAfx.h" #include "TStringList.h" TStringList :: ~TStringList() { Clear(); } #ifdef _DEBUG void TStringList :: Trace(const string &Title) { TRACE("TStringList :: ShowDebug() ----- %s\n", Title.c_str()); int Index = 0; for (vector<string>::iterator it = FList.begin(); it != FList.end(); it++, Index++) { TRACE("TStringList :: ShowDebug() %d = %s\n", Index, it->c_str()); } TRACE("\n"); } #endif void TStringList :: Explode(const string &Delimiter, const string &String) { Clear(); string s = String.c_str(); char *p = (char *)s.c_str(); p = strtok(p, Delimiter.c_str()); while(1) { if (!p) break; Add(p); p = strtok(NULL, Delimiter.c_str()); } } void TStringList :: LoadFromFile(const string &FileName) { FList.clear(); // 목록 얻기 FILE *fp = fopen (FileName.c_str(), "r"); if (!fp) { TRACE("TStringList :: LoadFromFile(%s) WARNING! fopen fail!\n", FileName.c_str()); return; } while(1) { char str[4096]; string sStr; if (!fgets(str, sizeof(str), fp)) break; sStr = str; sStr = str_replace(sStr, "\r", ""); sStr = str_replace(sStr, "\n", ""); if (sStr == "") continue; FList.push_back(sStr); } fclose(fp); } void TStringList :: SaveToFile(const string &FileName) { FILE *fp = fopen (FileName.c_str(), "w"); if (!fp) return; for (vector<string>::iterator it = FList.begin(); it != FList.end(); it++) { fputs(it->c_str(), fp); fputs("\n", fp); } fclose(fp); }

댓글 달기 (로그인이 필요합니다)
제목
내용

http://codesarang.com. mail to cpueblocpueblo.com