//=========================================================================== // Tristram Shandy from the Gutenberg Library // Visual Components Library // AnsiString Class Demonstration // Nick Gessler - 26 July 2009 //=========================================================================== #include #pragma hdrstop #include "Unit2.h" #include // file streaming //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm2 *Form2; //================================================================= VARIABLES AnsiString tristram; // the entire text AnsiString toSearch; // diminishing text to search AnsiString target; // search target AnsiString line; // for line by line listing int times; // count of number of targets hit char chunk[100]; // to interface to file streaming //================================================================= FUNCTIONS //-------------------------------------------------------- OPEN THE TEXT FILE void openStuff (void) { if (Form2->OpenDialog1->Execute()) { ifstream infile(Form2->OpenDialog1->FileName.c_str()); for (int i = 0; i < 1000000; i++) { infile >> chunk; tristram += AnsiString(chunk) + " "; } infile.close(); } // delete the gutenberg beginning int beginning = tristram.Pos("To the Right Honourable Mr. Pitt. Sir"); tristram.Delete(1, beginning - 1); // delete the gutenberg ending int ending = tristram.Pos("End of the Project Gutenberg EBook"); int length = tristram.Length(); tristram.Delete(ending, length - ending); // display the text without gutenberg appended notes Form2->Memo1->Text = tristram; // compute and display the length of the text length = tristram.Length(); Form2->Edit1->Text = length; } //---------------------------------------------- SEARCH FOR THE TARGET STRING void search (AnsiString target) { Form2->Edit1->Text = ""; Form2->Memo2->Text = ""; toSearch = tristram; times = 0; while (toSearch.Pos(target) != 0) { times++; Form2->Memo2->Lines->Append(AnsiString(times) + ") " + toSearch.SubString(toSearch.Pos(target) - 60, 120)); toSearch.Delete(1, toSearch.Pos(target)); } Form2->Edit2->Text = target + " appears " + AnsiString(times) + " times"; Form2->Edit1->Text = ""; Application->ProcessMessages(); } //============================================================ EVENT HANDLERS //--------------------------------------------------------------------------- __fastcall TForm2::TForm2(TComponent* Owner) : TForm(Owner) { } //---------------------------------------------------------- OPEN FILE BUTTON void __fastcall TForm2::ButtonOpenClick(TObject *Sender) { openStuff(); } //--------------------------------------------------------------- FIND BUTTON void __fastcall TForm2::ButtonFindClick(TObject *Sender) { target = Form2->Edit1->Text; search(target); } //------------------------------------------------ ENTER KEY INITIATES SEARCH void __fastcall TForm2::Edit1KeyDown(TObject *Sender, WORD &Key, TShiftState Shift) { if (Key == VK_RETURN) { target = Form2->Edit1->Text; search(target); } } //------------------------------------------------------- that's all folks...