home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / top2src.zip / WORDS.C < prev   
C/C++ Source or Header  |  2000-07-13  |  3KB  |  96 lines

  1. /******************************************************************************
  2. WORDS.C      Word splitter.
  3.  
  4.     Copyright 1993 - 2000 Paul J. Sidorsky
  5.  
  6.     This program is free software; you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License, version 2, as
  8.     published by the Free Software Foundation.
  9.  
  10.     This program is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU General Public License
  16.     along with this program; if not, write to the Free Software
  17.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18.  
  19. This module contains the word splitter, which splits a string into words
  20. based on whitespace separation.
  21. ******************************************************************************/
  22.  
  23. #include "top.h"
  24.  
  25. /* split_string() - Splits a string into words.
  26.    Parameters:  lin - String (or line) of text to separate.
  27.    Returns:  Number of words in the separated string.
  28.    Notes:  Although this function is little more than a simple string-to-
  29.            word splitter, it forms the core of the TOP command processor so
  30.            it should be handled with care.  The split string (that is, lin)
  31.            is copied to the global variable word_str for storage.  The
  32.            word_pos and word_len global variables are also loaded with
  33.            data related to the split string.
  34. */
  35. XINT split_string(unsigned char *lin)
  36. {
  37. /* Word counter, string position counter, word length counter. */
  38. XINT wc = 0, pc = 0, lc = 0;
  39.  
  40. do
  41.     {
  42.     /* Skip extra whitespace that might precede the word. */
  43.     if (iswhite(lin[pc]))
  44.         {
  45.         pc++;
  46.         }
  47.     else
  48.         {
  49.         lc = 0;
  50.         /* Note the start of the word. */
  51.         word_pos[wc] = pc;
  52.         /* Count off letters until whitespace is encountered. */
  53.         do
  54.             {
  55.             lc++; pc++;
  56.             }
  57.         while(!iswhite(lin[pc]));
  58.         /* Note the length of the word. */
  59.         word_len[wc] = lc;
  60.         wc++;
  61.         }
  62.     }
  63. while(pc < strlen(lin));
  64.  
  65. /* The full text of the string is stored in the global variable
  66.    word_str, which is later used by the get_word() function to actually
  67.    get the word.  This storage method has the additional benefit of
  68.    allowing direct access to all the text starting at a particular word,
  69.    which is ideal for whispers, talktypes, and similar commands. */
  70. memset(word_str, 0, MAXSTRLEN + 1);
  71. strncpy(word_str, lin, MAXSTRLEN);
  72.  
  73. /* Zero out the lengths for unused words. */
  74. for (pc = wc; pc < MAXWORDS; pc++)
  75.     {
  76.     word_pos[pc] = strlen(lin);
  77.     word_len[pc] = 0;
  78.     }
  79.  
  80. return wc;
  81. }
  82.  
  83. /* get_word() - Gets a word from a previously split string.
  84.    Parameters:  wordnum - Number of word to get (0-based).
  85.    Returns:  Pointer to the global variable wordret, which contains the
  86.              retreived word.
  87. */
  88. char XFAR *get_word(XINT wordnum)
  89. {
  90.  
  91. memset(wordret, 0, word_len[wordnum] + 1L);
  92. strncpy(wordret, &word_str[word_pos[wordnum]], word_len[wordnum]);
  93.  
  94. return wordret;
  95. }
  96.