root/trunk/txlib/txstring.c

Revision 1, 4.6 kB (checked in by jvw, 3 years ago)

Initial check-in for TxWin? version 1.02 sources

Line 
1//
2//                     TxWin, Textmode Windowing Library
3//
4//   Original code Copyright (c) 1995-2005 Fsys Software and Jan van Wijk
5//
6// ==========================================================================
7//
8// This file contains Original Code and/or Modifications of Original Code as
9// defined in and that are subject to the GNU Lesser General Public License.
10// You may not use this file except in compliance with the License.
11// BY USING THIS FILE YOU AGREE TO ALL TERMS AND CONDITIONS OF THE LICENSE.
12// A copy of the License is provided with the Original Code and Modifications,
13// and is also available at http://www.dfsee.com/txwin/lgpl.htm
14//
15// This library is free software; you can redistribute it and/or modify
16// it under the terms of the GNU Lesser General Public License as published
17// by the Free Software Foundation; either version 2.1 of the License,
18// or (at your option) any later version.
19//
20// This library is distributed in the hope that it will be useful,
21// but WITHOUT ANY WARRANTY; without even the implied warranty of
22// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
23// See the GNU Lesser General Public License for more details.
24//
25// You should have received a copy of the GNU Lesser General Public License
26// along with this library; (lgpl.htm) if not, write to the Free Software
27// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28//
29// Questions on TxWin licensing can be directed to: txwin@fsys.nl
30//
31// ==========================================================================
32//
33// Generic TX string functions
34//
35// Author: J. van Wijk
36//
37// JvW  24-07-2005 Initial version, split off from TXUTIL
38
39#include <txlib.h>
40
41/*****************************************************************************/
42// Replace specfied character by another in whole string, count replacements
43/*****************************************************************************/
44int TxRepl                                      // RET   nr of replacements
45(
46   char               *str,                     // INOUT string to convert
47   char                old,                     // IN    old char, to replace
48   char                new                      // IN    new char
49)
50{
51   int                 rc = 0;
52
53   while (*str)
54   {
55      if (*str == old)
56      {
57         *str = new;
58         rc++;
59      }
60      str++;
61   }
62   return (rc);
63}                                               // end 'TxRepl'
64/*---------------------------------------------------------------------------*/
65
66/*****************************************************************************/
67// Strip leading/trailing characters from a string, dest and source can be same
68/*****************************************************************************/
69int TxStrip                                     // RET   nr of stripped chars
70(
71   char               *dest,                    // OUT   destination string
72   char               *source,                  // IN    source string
73   char                lead,                    // IN    leading  chars to strip
74   char                trail                    // IN    trailing chars to strip
75)
76{
77   int                 rc = 0;
78   char               *s = dest;
79
80   while ((*source) && (*source == lead))
81   {
82      source++;
83      rc++;
84   }
85   while (*source)
86   {
87      *s++ = *source++;
88   }
89   *s = 0;
90   while ((s-- > dest) && (*s == trail))
91   {
92      *s = 0;
93      rc++;
94   }
95   return (rc);
96}                                               // end 'TxStrip'
97/*---------------------------------------------------------------------------*/
98
99
100/*****************************************************************************/
101// Pad/Clip a string to specified length, concatenating or removing characters
102/*****************************************************************************/
103int TxPClip                                     // RET   +:padded -:removed
104(
105   char               *str,                     // INOUT destination string
106   int                 size,                    // IN    requested length
107   char                pad                      // IN    trailing chars to pad
108)
109{
110   int                 pos = strlen(str);
111   int                 rc  = size - pos;
112
113   str[ size] = 0;                              // terminate at wanted length
114
115   while (pos < size)
116   {
117      str[pos] = pad;
118      pos++;
119   }
120   return (rc);
121}                                               // end 'TxPClip'
122/*---------------------------------------------------------------------------*/
Note: See TracBrowser for help on using the browser.