Solved

c code for searching pascal-style strings

Posted on 1998-06-30
1
190 Views
Last Modified: 2013-12-26
I have a dialog text-edit box that the user will type in what he wants to search for. Then I need to search through about 100 STR# for any matches. I can retrieve the text for the text-edit box, but then I don't really know where to go. All the c functions for handeling strings expect a c-style string, so it'd be a pain to covert everything. I heard something about a PStrCmp function, but I can't get it to work. Does anyone have some code or nifty functions that can help me? Thanks!
0
Comment
Question by:anthonyz
1 Comment
 
LVL 2

Accepted Solution

by:
roov earned 50 total points
ID: 1291487
First, don't give so easily on conversion.
You might want to keep all your string resources in memory anyway, since loading them from the resource takes time, and if you keep them in memory, you might as well keep them as C strings, then get your user's string, convert it to C style, then use strstr or something similar.

Additionaly, the Mac Toolbox has EqualString, which compares two Pascal style strings.
The brute force of doing this search might look similar to this (without error checking/recovery):
short FindPattern(StringPtr inPattern, StringPtr inData)
{
      // return index of inPattern. ) if not found. Find last occurance...
      short location = 0;
      if (inData[0] < inPattern[0])
            return 0;
      if ((inData[0] == inPattern[0]) &&
            (EqualString(inData, inPattern, true, false))
      {
            return 1;
      }
      for (short ind = inData[0] - inPattern[0]; ind > inPattern[0]; ind--) {
            unsigned char storage = inData[ind];
            Boolean foundPattern = false;
            inData[ind] = inPattern[0];
            foundPattern = EqualString(&(inData[ind]), inPattern, true, false);
            inData[ind] = storage;
            if (foundPattern) {
                  location = ind;
                  break;
            }
      }
      return location
}

Sorry if this has minor bugs...

Enjoy...
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Need to retreive data from .TFS file exported by DVR 8 200
ADO Memory leak with DELPHI 2007 37 182
fizzArray  challenge 1 84
fix34  challenge 9 121
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question