home *** CD-ROM | disk | FTP | other *** search
/ Collection of Internet / Collection of Internet.iso / msdos / lynx / source / doslynx / src / turlvi24.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-25  |  6.1 KB  |  220 lines

  1. //    Copyright (c) 1994, University of Kansas, All Rights Reserved
  2. //
  3. //    Class:        TURLView
  4. //    Include File:    turlview.h
  5. //    Purpose:    Provide a view for a URL
  6. //    Remarks/Portability/Dependencies/Restrictions:
  7. //    Revision History:
  8. //        03-28-94    created
  9. #define Uses_TProgram
  10. #define Uses_TDeskTop
  11. #define Uses_TDialog
  12. #define Uses_TInputLine
  13. #define Uses_THistory
  14. #define Uses_TButton
  15. #define Uses_TLabel
  16. #include"turlview.h"
  17. #include<string.h>
  18.  
  19. void TURLView::search(const char *cp_searchFor)    {
  20. //    Purpose:    Search for a string in the view.
  21. //    Arguments:    cp_searchFor    The string to search for.
  22. //                    If NULL then the user is prompted to
  23. //                    enter a string.
  24. //    Return Value:    void
  25. //    Remarks/Portability/Dependencies/Restrictions:
  26. //    Revision History:
  27. //        03-28-94    created
  28.  
  29.     auto char *cp_searching = NULL;
  30.  
  31.     //    Prompt the user for the search string if need be.
  32.     if(cp_searchFor == NULL || *cp_searchFor == '\0')    {
  33.         //    A dialog must be created to ask the user for
  34.         //    the string to search for.
  35.  
  36.         //    General use rectangle.
  37.         auto TRect TR(0, 0, 50, 7);
  38.  
  39.         //    Create a dialog that asks for the search string.
  40.         TDialog *TDp_index = new TDialog(TR, "Search");
  41.         if(TDp_index == NULL)    {
  42.             doslynxmessage("Unable to create index dialog.");
  43.             return;
  44.         }
  45.  
  46.         //    Set some dialog options.
  47.         TDp_index->options |= ofCentered;
  48.  
  49.         //      Make the input line to enter the search string.
  50.         TR = TRect(6, 2, 44, 3);
  51.         TInputLine *TILp_searchString = new TInputLine(TR,
  52.             usi_TILURLSize - 1);
  53.         TDp_index->insert(TILp_searchString);
  54.  
  55.  
  56.         //    Make the input line's label.
  57.         TR = TRect(5, 1, 27, 2);
  58.         TLabel *TLp_prompt = new TLabel(TR, "~E~nter a Search String",
  59.             TILp_searchString);
  60.         TDp_index->insert(TLp_prompt);
  61.  
  62.         //    Make the input line's history.
  63.         TR = TRect(45, 2, 48, 3);
  64.         THistory *THp_indexHist = new THistory(TR, TILp_searchString,
  65.             usi_IndexHist);
  66.         TDp_index->insert(THp_indexHist);
  67.  
  68.         //    Insert the Ok and Cancel buttons.
  69.         auto TButton *TBp_button;
  70.         TR = TRect(9, 4, 21, 6);
  71.         TBp_button = new TButton(TR, "~O~K", cmOK, bfDefault);
  72.         TDp_index->insert(TBp_button);
  73.         TR = TRect(24, 4, 36, 6);
  74.         TBp_button = new TButton(TR, "~C~ancel", cmCancel, bfNormal);
  75.         TDp_index->insert(TBp_button);
  76.  
  77.         //    Stay in the search string field.
  78.         TDp_index->selectNext(False);
  79.  
  80.         //    Draw the dialog.
  81.         TDp_index->drawView();
  82.  
  83.         //    Execute the dialog.
  84.         if(TProgram::deskTop->execView(TDp_index) != cmCancel)    {
  85.             //    User didn't cancel, save what we got.
  86.             auto char ca_buffer[usi_TILURLSize];
  87.             TILp_searchString->getData(ca_buffer);
  88.             cp_searching = newStr(ca_buffer);
  89.         }
  90.         else    {
  91.             //    User canceled.
  92.             return;
  93.         }
  94.  
  95.         //    Done with dialog.
  96.         destroy(TDp_index);
  97.  
  98.         //    Reset from where we last searched.
  99.         usi_lastSearchLine = usi_lastSearchOffset = 0U;
  100.     }
  101.     else    {
  102.         //    Copy over the already entered search string.
  103.         cp_searching = newStr(cp_searchFor);
  104.     }
  105.  
  106.     //    If there exists no search string, do nothing.
  107.     if(cp_searching == NULL || *cp_searching == '\0')    {
  108.         if(cp_searching != NULL)    {
  109.             delete(cp_searching);
  110.         }
  111.         return;
  112.     }
  113.  
  114.     //    Now, search for the string from the last line and offset
  115.     //    searched forward.
  116.     auto Line L_search;
  117.     auto char ca_linebuf[usi_TILURLSize];
  118.     auto char *cp_foundat;
  119.     while(fsp_temp->eof() == 0 && fsp_temp->bad() == 0)    {
  120.         //    Check to make sure we have not gone past the last
  121.         //    line.
  122.         if(usi_lastSearchLine >= TNSCp_lines->getCount())    {
  123.             doslynxmessage(cp_searching << " not found.");
  124.             if(TAp_search != NULL)    {
  125.                 delete(TAp_search);
  126.                 TAp_search = NULL;
  127.             }
  128.             drawView();
  129.             break;
  130.         }
  131.  
  132.         //     Seek the line offset.
  133.         fsp_temp->seekg((signed long int)(TNSCp_lines->at(
  134.             usi_lastSearchLine)));
  135.  
  136.         //    Get the line information.
  137.         fsp_temp->read((char *)(&L_search), sizeof(Line));
  138.         if(fsp_temp->gcount() != sizeof(Line))    {
  139.             doslynxmessage("Error in reading temporary file.");
  140.             break;
  141.         }
  142.  
  143.         //    If our last search offset is greater than the line
  144.         //    length, reset the offset and go on to the next line.
  145.         if(usi_lastSearchOffset >= L_search.ssi_length)    {
  146.             usi_lastSearchOffset = 0U;
  147.             usi_lastSearchLine++;
  148.             continue;
  149.         }
  150.  
  151.         //    Seek out to the last search offset.
  152.         fsp_temp->seekg((signed long int)usi_lastSearchOffset,
  153.             ios::cur);
  154.  
  155.         //    Read the remainder of the line.
  156.         fsp_temp->read(ca_linebuf, L_search.ssi_length -
  157.             usi_lastSearchOffset);
  158.         if(fsp_temp->gcount() != L_search.ssi_length -
  159.             usi_lastSearchOffset)    {
  160.             doslynxmessage("Error in reading temporary file.");
  161.             break;
  162.         }
  163.  
  164.         //    End the read information line.
  165.         ca_linebuf[L_search.ssi_length - usi_lastSearchOffset] = '\0';
  166.  
  167.         //    Search for the string.
  168.         if((cp_foundat = strstr(ca_linebuf, cp_searching)) == NULL)
  169.         {
  170.             //    Not found, go on to the next line.
  171.             usi_lastSearchOffset = 0U;
  172.             usi_lastSearchLine++;
  173.             continue;
  174.         }
  175.  
  176.         //    String was found in the line.
  177.  
  178.         //    If we don't have one already, create the search
  179.         //        text attribute.
  180.         if(TAp_search == NULL)    {
  181.             TAp_search = new TextAttribute(NULL);
  182.         }
  183.         TAp_search->Begin((signed long int)(TNSCp_lines->
  184.             at(usi_lastSearchLine)) + (signed long int)
  185.             usi_lastSearchOffset + (signed long int)(cp_foundat -
  186.             ca_linebuf) + (signed long int)sizeof(Line));
  187.         TAp_search->End(TAp_search->getBegin() + (signed long int)
  188.             strlen(cp_searching));
  189.  
  190.         //    Set our offset past the find.
  191.         usi_lastSearchOffset += (unsigned short int)(cp_foundat -
  192.             ca_linebuf) + strlen(cp_searching);
  193.  
  194.         //    Save our deltas so that we know if the screen was
  195.         //    redrawn.  This is a kludge to have our searched for
  196.         //    string light up.
  197.         auto TPoint TP_old = delta;
  198.  
  199.         //    Don't draw if already on the screen.
  200.         if(usi_lastSearchLine >= delta.y + size.y ||
  201.             usi_lastSearchLine < delta.y)    {
  202.             scrollTo(delta.x, usi_lastSearchLine);
  203.         }
  204.  
  205.         //    Check to see if screen moved, if not we will redraw.
  206.         if(TP_old == delta)    {
  207.             drawView();
  208.         }
  209.  
  210.         break;
  211.     }
  212.  
  213.     //    Clear the temp file status.
  214.     fsp_temp->clear();
  215.  
  216.     //    Done, release the search string after copying it into
  217.     //    the global last search variable.
  218.     strcpy(ca_searchLast, cp_searching);
  219.     delete(cp_searching);
  220. }