Click here to Skip to main content
15,884,986 members
Articles / Programming Languages / C++
Article

XMemString - Extended memory and string search functions

Rate me:
Please Sign up or sign in to vote.
4.68/5 (16 votes)
11 May 2003CPOL1 min read 67.3K   802   18   11
Extended string and memory search functions: memmem(), memimem(), memichr(), _tcsistr()
  • Download demo project - 21 Kb

    Introduction

    XMemString is a collection of memory and string search functions I have been using for many years. They are all modelled on the functions available in the standard C runtime, so they are fairly self-explanatory.

    Function Descriptions

    • memichr() - Find character in a buffer (case insensitive).
      ///////////////////////////////////////////////////////////////////////////////
      //
      // memichr()
      //
      // Purpose:     Find character in a buffer (case insensitive)
      //
      // Parameters:  buf      - pointer to buffer
      //              c        - character to look for
      //              buf_len  - size of buffer in bytes
      //
      // Returns:     void *   - if successful, returns a pointer to the first
      //                         occurrence of c in buf;  otherwise, returns NULL
      //
      // Notes;       memichr() will search by ignoring the case of those characters
      //              that fall in the ANSI range a-z and A-Z.
      // 
    • memimem() - Find a byte sequence within a memory buffer (case insensitive)
      ///////////////////////////////////////////////////////////////////////////////
      //
      // memimem()
      //
      // Purpose:     Find a byte sequence within a memory buffer (case insensitive)
      //
      // Parameters:  buf               - pointer to buffer
      //              buf_len           - size of buffer in bytes
      //              byte_sequence     - byte sequence to look for
      //              byte_sequence_len - size of byte sequence in bytes
      //
      // Returns:     void * - if successful, returns a pointer to the first
      //                       occurrence of byte_sequence in buf;  otherwise,
      //                       returns NULL
      //
      // Notes;       memimem() will search by ignoring the case of those characters
      //              that fall in the ANSI range a-z and A-Z.
      //
    • memmem() - Find a byte sequence within a memory buffer
      ///////////////////////////////////////////////////////////////////////////////
      //
      // memmem()
      //
      // Purpose:     Find a byte sequence within a memory buffer
      //
      // Parameters:  buf               - pointer to buffer
      //              buf_len           - size of buffer in bytes
      //              byte_sequence     - byte sequence to look for
      //              byte_sequence_len - size of byte sequence in bytes
      //
      // Returns:     void * - if successful, returns a pointer to the first
      //                       occurrence of byte_sequence in buf;  otherwise,
      //                       returns NULL
      //
      // Notes;       Characters in byte_sequence and characters in buf will be
      //              compared "as is", with no case conversion.
      // 
    • _tcsistr() - Find a substring within a string (case insensitive)
      ///////////////////////////////////////////////////////////////////////////////
      //
      // _tcsistr()
      //
      // Purpose:     Find a substring within a string (case insensitive)
      //
      // Parameters:  string     - nul-terminated string to search
      //              strCharSet - nul-terminated string to search for
      //
      // Returns:     void * - if successful, returns a pointer to the first
      //                       occurrence of strCharSet in string;  otherwise,
      //                       returns NULL
      //
      // Notes;       _tcsistr() will search by ignoring the case of those characters
      //              that fall in the ANSI range a-z and A-Z.
      // 

    How To Use

    To integrate XMemString functions into your app, you first need to add following files to your project:

    • XMemString.cpp
    • XMemString.h

    If you include XMemString in project that uses precompiled headers, you must change C/C++ Precompiled Headers settings to Not using precompiled headers for XMemString.cpp.

    Next, include the header file XMemString.h in appropriate project files. Now you are ready to start using XMemString. Please see XMemStringTest.cpp for examples.

    Revision History

    Version 1.0 - 2003 May 11

    • Initial public release

    Usage

    This software is released into the public domain. You are free to use it in any way you like. If you modify it or extend it, please to consider posting new code here for everyone to share. This software is provided "as is" with no expressed or implied warranty. I accept no liability for any damage or loss of business that this software may cause.


  • License

    This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


    Written By
    Software Developer (Senior) Hans Dietrich Software
    United States United States
    I attended St. Michael's College of the University of Toronto, with the intention of becoming a priest. A friend in the University's Computer Science Department got me interested in programming, and I have been hooked ever since.

    Recently, I have moved to Los Angeles where I am doing consulting and development work.

    For consulting and custom software development, please see www.hdsoft.org.






    Comments and Discussions

     
    QuestionThank you! Pin
    OmidOnTop27-Jan-12 2:06
    OmidOnTop27-Jan-12 2:06 
    Generalmultibyte woes Pin
    umeca7419-May-03 22:08
    umeca7419-May-03 22:08 
    GeneralRe: multibyte woes Pin
    Hans Dietrich21-May-03 8:00
    mentorHans Dietrich21-May-03 8:00 
    QuestionWhy should I use it? Pin
    René Greiner11-May-03 22:22
    René Greiner11-May-03 22:22 
    AnswerRe: Why should I use it? Pin
    dog_spawn12-May-03 4:10
    dog_spawn12-May-03 4:10 
    AnswerRe: Why should I use it? Pin
    Sam Levy12-May-03 7:31
    Sam Levy12-May-03 7:31 
    GeneralRe: Why should I use it? Pin
    dog_spawn12-May-03 9:36
    dog_spawn12-May-03 9:36 
    GeneralRe: Why should I use it? Pin
    Sam Levy12-May-03 10:56
    Sam Levy12-May-03 10:56 
    GeneralRe: Why should I use it? Pin
    dog_spawn12-May-03 14:28
    dog_spawn12-May-03 14:28 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.