1 /*-------------- Telecommunications & Signal Processing Lab ---------------
2                              McGill University
3 
4 Routine:
5   int STtrimNMax (const char Si[], char So[], int N, int Maxchar)
6 
7 Purpose:
8   Copy at most N characters, trimming trailing white-space
9 
10 Description:
11   This routine copies characters from the input string to the output string.
12   The length of the input string is considered to be equal to N less any
13   trailing white-space (as defined by isspace).  Characters are copied from
14   input string up to the minimum of the length of the input string and Maxchar.
15   A null character in the input string also terminates the transfer at that
16   point.  If the input string is longer than Maxchar, a string truncated
17   warning message is printed.
18 
19 Parameters:
20   <-  int STtrimNMax
21       Number of characters in the output string
22    -> const char Si[]
23       Input character string
24   <-  char So[]
25       Output character string.  This string is always null terminated, with
26       at most Maxchar characters not including the terminating null character.
27       If the input string is longer than Maxchar, only the first Maxchar
28       characters are copied and a warning message is printed.
29    -> int N
30       Number of characters to be transferred
31    -> int Maxchar
32       Maximum number of characters (not including the trailing null character)
33       to be placed in So.
34 
35 Author / revision:
36   P. Kabal  Copyright (C) 2003
37   $Revision: 1.13 $  $Date: 2003/05/09 03:06:42 $
38 
39 -------------------------------------------------------------------------*/
40 
41 #include <ctype.h>
42 
43 #include <libtsp.h>
44 #include <libtsp/nucleus.h>
45 
46 int
STtrimNMax(const char Si[],char So[],int N,int Maxchar)47 STtrimNMax (const char Si[], char So[], int N, int Maxchar)
48 
49 {
50   const char *p;
51   int n, nc;
52 
53   /* Determine the length of the input string */
54   p = Si;
55   for (n = 0; n < N; ++n, ++p)
56     if (*p == '\0')
57       break;
58 
59   /* Trim trailing white-space */
60   for (p = Si+(n-1); n > 0 ; --n, --p)	/* n is the number of characters */
61     if (! isspace ((int) *p))
62       break;
63 
64   /* Copy the trimmed string to the output string */
65   nc = STcopyNMax (Si, So, n, Maxchar);
66   return nc;
67 }
68