]>
git.saurik.com Git - apple/security.git/blob - SecuritySNACCRuntime/compiler/back-ends/str-util.c
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
20 * compiler/back_ends/c_gen/str_util.c - bunch of ASN.1/C string utilities
25 * Copyright (C) 1991, 1992 Michael Sample
26 * and the University of British Columbia
28 * This program is free software; you can redistribute it and/or modify
29 * it under the terms of the GNU General Public License as published by
30 * the Free Software Foundation; either version 2 of the License, or
31 * (at your option) any later version.
33 * $Header: /cvs/root/Security/SecuritySNACCRuntime/compiler/back-ends/Attic/str-util.c,v 1.1.1.1 2001/05/18 23:14:08 mb Exp $
34 * $Log: str-util.c,v $
35 * Revision 1.1.1.1 2001/05/18 23:14:08 mb
36 * Move from private repository to open source repository
38 * Revision 1.3 2001/05/05 00:59:27 rmurphy
39 * Adding darwin license headers
41 * Revision 1.2 2000/05/10 21:36:43 rmurphy
42 * changing the suffix for c++ output files to .cpp - requires -DMACOS on the compilation line
44 * Revision 1.1.1.1 1999/03/16 18:06:39 aram
45 * Originals from SMIME Free Library.
47 * Revision 1.4 1995/07/25 18:13:31 rj
50 * by default, snacc now derives output file names from the .asn1 input file name instead of the module name.
51 * the global keepbaseG variable switches between the two behaviours.
53 * additional filename generator for idl backend.
55 * changed `_' to `-' in file names.
57 * Revision 1.3 1994/10/08 03:48:17 rj
58 * since i was still irritated by cpp standing for c++ and not the C preprocessor, i renamed them to cxx (which is one known suffix for C++ source files). since the standard #define is __cplusplus, cplusplus would have been the more obvious choice, but it is a little too long.
60 * Revision 1.2 1994/09/01 00:25:31 rj
61 * snacc_config.h removed; more portable .h file inclusion.
63 * Revision 1.1 1994/08/28 09:48:37 rj
64 * first check-in. for a list of changes to the snacc-1.1 distribution please refer to the ChangeLog.
72 #include <unistd.h> /* for pathconf (..) */
74 #if STDC_HEADERS || HAVE_STRING_H
81 #include "asn1module.h"
84 #include "c-gen/rules.h"
85 #include "c-gen/type-info.h"
86 #include "c-gen/kwd.h"
87 #include "c++-gen/kwd.h"
91 #define DIGIT_TO_ASCII( d) (((d) % 10) + '0' )
96 * allocates new and returns a copy of the given
97 * string with '-'s (dashes) replaced by '_'s (underscores)
100 Asn1TypeName2CTypeName
PARAMS (( aName
),
107 retVal
= Malloc ( strlen ( aName
) + 1 );
108 strcpy ( retVal
, aName
);
109 Dash2Underscore ( retVal
, strlen ( retVal
));
112 } /* Asn1TypeName2CTypeName */
116 * allocates new str and returns a copy of the given
117 * string with '-'s (dashes) replaced by '_'s (underscores)
120 Asn1FieldName2CFieldName
PARAMS (( aName
),
127 retVal
= Malloc ( strlen ( aName
) + 1 );
128 strcpy ( retVal
, aName
);
129 Dash2Underscore ( retVal
, strlen ( retVal
));
132 } /* Asn1FieldName2CFieldName */
136 * allocates new str and returns a copy of the given
137 * string with '-'s (dashes) replaced by '_'s (underscores)
140 Asn1ValueName2CValueName
PARAMS (( aName
),
147 retVal
= Malloc ( strlen ( aName
) + 1 );
148 strcpy ( retVal
, aName
);
149 Dash2Underscore ( retVal
, strlen ( retVal
));
152 } /* Asn1FieldName2CFieldName */
156 * allocates and returns a string with all of
157 * the caps from the given string
160 GetCaps
PARAMS (( str
),
169 retVal
= Malloc ( strlen ( str
) + 1 );
171 for ( j
= 0 , i
= 0 ; i
< strlen ( str
); i
++)
173 if ( isupper ( str
[ i
]))
174 retVal
[ j
++] = str
[ i
];
177 retVal
[ j
] = '\0' ; /* null terminate */
185 * allocates and returns a string with all of
186 * the caps and digits from the given string
189 GetCapsAndDigits
PARAMS (( str
),
198 retVal
= Malloc ( strlen ( str
) + 1 );
200 for ( j
= 0 , i
= 0 ; i
< strlen ( str
); i
++)
202 if (( isupper ( str
[ i
])) || ( isdigit ( str
[ i
])))
203 retVal
[ j
++] = str
[ i
];
206 retVal
[ j
] = '\0' ; /* null terminate */
210 } /* GetCapsAndDigits */
214 * replaces lowercase chars in given str
215 * with upper case version
216 * NOTE: modifies given str
219 Str2UCase
PARAMS (( str
, len
),
224 for ( i
= 0 ; i
< len
; i
++)
226 if ( islower ( str
[ i
]))
227 str
[ i
] = toupper ( str
[ i
]);
233 * replaces uppercase chars in given str
234 * with lower case version
235 * NOTE: modifies given str
238 Str2LCase
PARAMS (( str
, len
),
243 for ( i
= 0 ; i
< len
; i
++)
245 if ( isupper ( str
[ i
]))
246 str
[ i
] = tolower ( str
[ i
]);
252 * replace dash chars in given str
254 * NOTE: modifies given str
257 Dash2Underscore
PARAMS (( str
, len
),
262 for ( i
= 0 ; i
< len
; i
++)
267 } /* Dash2Underscore */
271 * tacks on the ascii version of the given digit
272 * at the end of the given str.
273 * NOTE: make sure the str you give has enough space
277 AppendDigit
PARAMS (( str
, digit
),
281 int high
= 1000000000 ;
284 char digitStr
[ 20 ]; /* arbitrary length > max */
292 value
= digit
/ high
;
294 digitStr
[ currDigit
++]= DIGIT_TO_ASCII ( value
);
296 digit
= digit
% high
;
304 digitStr
[ currDigit
] = '\0' ; /* null terminate */
305 strcat ( str
, digitStr
);
313 * given a defined object list containing null termintated strs,
314 * a str to be made unique wrt to the list by adding digits to the
315 * end, the max number of digits to add and the digit to start
316 * at, str is modified to be unique. It is not added to the
317 * defined object list. The given str must have enough spare,
318 * allocated chars after it's null terminator to hold maxDigits
320 * Only appends digits if the string is not unique or is a C keyword.
322 * Eg MakeCStrUnique ({ "Foo", "Bar" }, "Foo\0 ", 3, 1)
323 * modifies the the Str "Foo" to "Foo1"
326 MakeCStrUnique
PARAMS (( nameList
, str
, maxDigits
, startingDigit
),
327 DefinedObj
* nameList _AND_
332 int digit
, len
, maxDigitVal
;
334 if ( ObjIsDefined ( nameList
, str
, StrObjCmp
) || IsCKeyWord ( str
))
336 for ( maxDigitVal
= 1 ; maxDigits
> 0 ; maxDigits
--)
340 digit
= startingDigit
;
344 AppendDigit ( str
, digit
++);
345 } while ( ObjIsDefined ( nameList
, str
, StrObjCmp
) && ( digit
< maxDigitVal
));
347 } /* MakeCStrUnique */
351 * same as MakeCStrUnique except checks against C++ keywords
354 MakeCxxStrUnique
PARAMS (( nameList
, str
, maxDigits
, startingDigit
),
355 DefinedObj
* nameList _AND_
360 int digit
, len
, maxDigitVal
;
362 if ( ObjIsDefined ( nameList
, str
, StrObjCmp
) || IsCxxKeyWord ( str
))
364 for ( maxDigitVal
= 1 ; maxDigits
> 0 ; maxDigits
--)
368 digit
= startingDigit
;
372 AppendDigit ( str
, digit
++);
373 } while ( ObjIsDefined ( nameList
, str
, StrObjCmp
) && ( digit
< maxDigitVal
));
375 } /* MakeCxxStrUnique */
381 * strip leading path and trailing suffix
385 * allocates and returns a base file name generated from
386 * the module's name. May shorten the name if the
387 * expected length exceed the systems max path component length
388 * (eg to support SYS V 14 char filename len limit)
390 * Base file name is used as the base name for the generated C source files.
393 MakeBaseFileName
PARAMS (( refName
),
402 if ( base
= strrchr ( refName
, '/' ))
407 if ( dot
= strrchr ( base
, '.' ))
408 stublen
= dot
- base
;
410 stublen
= strlen ( base
);
412 stub
= Malloc ( stublen
+ 1 );
413 memcpy ( stub
, base
, stublen
);
414 stub
[ stublen
] = '\0' ;
423 int maxPathComponentLen
;
425 # define MAX_SUFFIX_LEN 2 /* .c, .h, .C */
426 extern int maxFileNameLenG
; /* declared in snacc.c */
429 * if the user has not given the max file name len
430 * via the -mf option,
431 * find the max filename len (ala POSIX method)
432 * if possible. Otherwise hardwire it to 14
433 * to support underpowered OSes
435 if ( maxFileNameLenG
> 2 )
436 maxPathComponentLen
= maxFileNameLenG
;
439 maxPathComponentLen
= pathconf ( getcwd ( pathName
, 1024 ), _PC_NAME_MAX
);
441 maxPathComponentLen
= 14 ;
444 retVal
= ( char *) Malloc ( strlen ( refName
) + 1 );
445 fNameLen
= strlen ( refName
) + MAX_SUFFIX_LEN
;
446 if (( fNameLen
> maxPathComponentLen
) && ( maxPathComponentLen
!= - 1 ))
448 cpyLen
= maxPathComponentLen
- MAX_SUFFIX_LEN
;
450 /* don't allow trailing dash */
451 if ( refName
[ cpyLen
- 1 ] == '-' )
454 strncpy ( retVal
, refName
, cpyLen
);
455 retVal
[ cpyLen
] = '\0' ;
458 strcpy ( retVal
, refName
);
462 } /* MakeBaseFileName */
468 * given a module name and a suffix, the
469 * suffix is appended to the module name
470 * and the whole string is put into lower case
471 * and underscores are inserted in likely places
472 * (ie MTSAbstractSvc.h -> mts_abstract_svc.h)
475 MakeFileName
PARAMS (( refName
, suffix
),
476 const char * refName _AND_
481 size_t baselen
= strlen ( refName
),
482 sufflen
= strlen ( suffix
);
483 char * filename
= Malloc ( baselen
+ sufflen
+ 1 );
485 memcpy ( filename
, refName
, baselen
);
486 memcpy ( filename
+ baselen
, suffix
, sufflen
);
487 filename
[ baselen
+ sufflen
] = '\0' ;
493 int i
, cpyIndex
, len
;
497 #define MAX_UNDERSCORE 10
499 fName
= Malloc ( strlen ( refName
) + strlen ( suffix
) + 1 );
500 strcpy ( fName
, refName
);
501 strcat ( fName
, suffix
);
504 fNameLen
= strlen ( fName
);
507 * convert dashes to underscores, add spaces
509 Dash2Underscore ( fName
, fNameLen
);
513 * remove the next two lines if you uncomment the
514 * following underscore inserter
516 Str2LCase ( fName
, fNameLen
- strlen ( suffix
));
520 * NO LONGER DONE - LET THE USER MODIFY THE ASN.1 IF DESIRED
521 * add underscore between Lcase/Ucase of UCase/UcaseLcasce
522 * eg MTSAbstractSvc -> MTS_Abstract_Svc
524 len = strlen (fName) + MAX_UNDERSCORE + 1;
525 hdrCpy = (char *) Malloc (len);
527 hdrCpy[0] = fName[0];
528 for (i = 1, cpyIndex = 1; (cpyIndex < len) && (i < fNameLen); i++)
530 if (((islower (fName[i-1])) && (isupper (fName[i]))) ||
531 ((isupper (fName[i-1])) && (isupper (fName[i])) &&
532 ((i < (fNameLen-1)) && (islower (fName[i+1])))))
534 hdrCpy[cpyIndex++] = '_';
535 hdrCpy[cpyIndex++] = fName[i];
538 hdrCpy[cpyIndex++] = fName[i];
540 hdrCpy[cpyIndex++] = '\0';
542 Str2LCase (hdrCpy, cpyIndex - strlen (suffix));
552 MakeCHdrFileName
PARAMS (( refName
),
555 return MakeFileName ( refName
, ".h" );
559 MakeCSrcFileName
PARAMS (( refName
),
562 return MakeFileName ( refName
, ".c" );
566 MakeCxxHdrFileName
PARAMS (( refName
),
569 return MakeFileName ( refName
, ".h" );
573 MakeCxxSrcFileName
PARAMS (( refName
),
577 return MakeFileName ( refName
, ".C" );
579 return MakeFileName ( refName
, ".cpp" ); /* ignore cpp rant */
584 char * /* 19.8.93 IBM-ENC */
585 MakedbHdrFileName
PARAMS (( refName
),
588 return MakeFileName ( refName
, "db.h" );
591 char * /* 19.8.93 IBM-ENC */
592 MakedbSrcFileName
PARAMS (( refName
),
595 return MakeFileName ( refName
, "db.C" );
597 #endif /* _IBM_ENC_ */
601 MakeIDLFileName
PARAMS (( refName
),
604 return MakeFileName ( refName
, ".idl" );