Solved

c code for searching pascal-style strings

Posted on 1998-06-30
1
188 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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

744 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now