Solved

c code for searching pascal-style strings

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
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.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

911 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

19 Experts available now in Live!

Get 1:1 Help Now