1 /*-------------- Telecommunications & Signal Processing Lab ---------------
2 McGill University
3
4 Routine:
5 int STcopyNMax (const char Si[], char So[], int N, int Maxchar)
6
7 Purpose:
8 Copy N characters characters to a string
9
10 Description:
11 This routine is used to copy min(N, strlen(Si)) characters from the input
12 input string to the output string. If min(N, strlen(Si) is greater than
13 Maxchar, a string is truncated at that point and a warning message is
14 printed.
15
16 Parameters:
17 <- int STcopyNMax
18 Number of characters in the output string
19 -> const char Si[]
20 Input character string
21 <- char So[]
22 Output character string. This string is always null terminated, with
23 at most Maxchar characters not including the terminating null character.
24 -> int N
25 Number of characters to be transferred
26 -> int Maxchar
27 Maximum number of characters (not including the terminating null
28 character) to be placed in So.
29
30 Author / revision:
31 P. Kabal Copyright (C) 2003
32 $Revision: 1.20 $ $Date: 2003/05/09 03:02:44 $
33
34 -------------------------------------------------------------------------*/
35
36 #include <libtsp.h>
37 #include <libtsp/STmsg.h>
38
39 #define MINV(a, b) (((a) < (b)) ? (a) : (b))
40
41
42 int
STcopyNMax(const char Si[],char So[],int N,int Maxchar)43 STcopyNMax (const char Si[], char So[], int N, int Maxchar)
44
45 {
46 const char *si;
47 int n;
48 int NM;
49
50 /* Save the initial input pointer */
51 si = Si;
52
53 /* Copy at most max(N, Maxchar) characters */
54 NM = MINV (Maxchar, N);
55 for (n = 0; n < NM && *si != '\0'; n++)
56 *So++ = *si++;
57
58 /* Add a trailing null */
59 *So = '\0';
60
61 /* Check for truncation */
62 if (N > Maxchar && n == NM && Si[NM-1] != '\0')
63 UTwarn ("STcopyNMax - %s: \"%.*s...\"", STM_StrTrunc, MINV (30, n), Si);
64
65 /* Return the number of characters in So */
66 return n;
67 }
68