Solved

c code for searching pascal-style strings

Posted on 1998-06-30
1
191 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

809 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