2003-08-26 02:36:57 +00:00
|
|
|
/*
|
2007-07-27 03:12:12 +00:00
|
|
|
*******************************************************************************
|
2014-01-10 21:41:03 +00:00
|
|
|
* Copyright (C) 2003-2014, International Business Machines
|
2007-07-27 03:12:12 +00:00
|
|
|
* Corporation and others. All Rights Reserved.
|
|
|
|
*******************************************************************************
|
|
|
|
*
|
|
|
|
* File prscmnts.cpp
|
|
|
|
*
|
|
|
|
* Modification History:
|
|
|
|
*
|
|
|
|
* Date Name Description
|
|
|
|
* 08/22/2003 ram Creation.
|
|
|
|
*******************************************************************************
|
|
|
|
*/
|
|
|
|
|
2014-01-10 21:41:03 +00:00
|
|
|
// Safer use of UnicodeString.
|
|
|
|
#ifndef UNISTR_FROM_CHAR_EXPLICIT
|
|
|
|
# define UNISTR_FROM_CHAR_EXPLICIT explicit
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Less important, but still a good idea.
|
|
|
|
#ifndef UNISTR_FROM_STRING_EXPLICIT
|
|
|
|
# define UNISTR_FROM_STRING_EXPLICIT explicit
|
|
|
|
#endif
|
|
|
|
|
2003-08-26 02:36:57 +00:00
|
|
|
#include "unicode/regex.h"
|
|
|
|
#include "unicode/unistr.h"
|
|
|
|
#include "unicode/parseerr.h"
|
|
|
|
#include "prscmnts.h"
|
2003-09-10 01:05:44 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2006-09-04 16:28:24 +00:00
|
|
|
U_NAMESPACE_USE
|
|
|
|
|
2003-12-09 20:07:47 +00:00
|
|
|
#if UCONFIG_NO_REGULAR_EXPRESSIONS==0 /* donot compile when RegularExpressions not available */
|
|
|
|
|
2003-09-10 01:05:44 +00:00
|
|
|
#define MAX_SPLIT_STRINGS 20
|
|
|
|
|
|
|
|
const char *patternStrings[UPC_LIMIT]={
|
2007-06-18 23:36:49 +00:00
|
|
|
"^translate\\s*(.*)",
|
|
|
|
"^note\\s*(.*)"
|
2003-09-10 01:05:44 +00:00
|
|
|
};
|
2003-08-26 02:36:57 +00:00
|
|
|
|
|
|
|
U_CFUNC int32_t
|
2003-10-16 22:58:05 +00:00
|
|
|
removeText(UChar *source, int32_t srcLen,
|
|
|
|
UnicodeString patString,uint32_t options,
|
|
|
|
UnicodeString replaceText, UErrorCode *status){
|
|
|
|
|
2003-08-26 02:36:57 +00:00
|
|
|
if(status == NULL || U_FAILURE(*status)){
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString src(source, srcLen);
|
|
|
|
|
|
|
|
RegexMatcher myMatcher(patString, src, options, *status);
|
|
|
|
if(U_FAILURE(*status)){
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
UnicodeString dest;
|
|
|
|
|
|
|
|
|
2003-10-16 22:58:05 +00:00
|
|
|
dest = myMatcher.replaceAll(replaceText,*status);
|
2003-08-26 02:36:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
return dest.extract(source, srcLen, *status);
|
|
|
|
|
|
|
|
}
|
|
|
|
U_CFUNC int32_t
|
|
|
|
trim(UChar *src, int32_t srcLen, UErrorCode *status){
|
2014-01-10 21:41:03 +00:00
|
|
|
srcLen = removeText(src, srcLen, UnicodeString("^[ \\r\\n]+ "), 0, UnicodeString(), status); // remove leading new lines
|
|
|
|
srcLen = removeText(src, srcLen, UnicodeString("^\\s+"), 0, UnicodeString(), status); // remove leading spaces
|
|
|
|
srcLen = removeText(src, srcLen, UnicodeString("\\s+$"), 0, UnicodeString(), status); // remvoe trailing spcaes
|
2003-08-26 02:36:57 +00:00
|
|
|
return srcLen;
|
|
|
|
}
|
|
|
|
|
|
|
|
U_CFUNC int32_t
|
|
|
|
removeCmtText(UChar* source, int32_t srcLen, UErrorCode* status){
|
|
|
|
srcLen = trim(source, srcLen, status);
|
2014-01-10 21:41:03 +00:00
|
|
|
UnicodeString patString("^\\s*?\\*\\s*?"); // remove pattern like " * " at the begining of the line
|
|
|
|
srcLen = removeText(source, srcLen, patString, UREGEX_MULTILINE, UnicodeString(), status);
|
|
|
|
return removeText(source, srcLen, UnicodeString("[ \\r\\n]+"), 0, UnicodeString(" "), status);// remove new lines;
|
2003-08-26 02:36:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
U_CFUNC int32_t
|
|
|
|
getText(const UChar* source, int32_t srcLen,
|
|
|
|
UChar** dest, int32_t destCapacity,
|
|
|
|
UnicodeString patternString,
|
|
|
|
UErrorCode* status){
|
|
|
|
|
|
|
|
if(status == NULL || U_FAILURE(*status)){
|
2003-08-28 21:04:53 +00:00
|
|
|
return 0;
|
2003-08-26 02:36:57 +00:00
|
|
|
}
|
|
|
|
|
2003-09-10 01:05:44 +00:00
|
|
|
UnicodeString stringArray[MAX_SPLIT_STRINGS];
|
2014-01-10 21:41:03 +00:00
|
|
|
RegexPattern *pattern = RegexPattern::compile(UnicodeString("@"), 0, *status);
|
2003-12-02 03:03:23 +00:00
|
|
|
UnicodeString src (source,srcLen);
|
2003-08-26 02:36:57 +00:00
|
|
|
|
|
|
|
if (U_FAILURE(*status)) {
|
|
|
|
return 0;
|
|
|
|
}
|
2003-09-10 01:05:44 +00:00
|
|
|
pattern->split(src, stringArray, MAX_SPLIT_STRINGS, *status);
|
2003-08-26 02:36:57 +00:00
|
|
|
|
2003-09-10 22:16:19 +00:00
|
|
|
RegexMatcher matcher(patternString, UREGEX_DOTALL, *status);
|
2003-08-26 02:36:57 +00:00
|
|
|
if (U_FAILURE(*status)) {
|
|
|
|
return 0;
|
|
|
|
}
|
2003-09-10 01:05:44 +00:00
|
|
|
for(int32_t i=0; i<MAX_SPLIT_STRINGS; i++){
|
2003-08-26 02:36:57 +00:00
|
|
|
matcher.reset(stringArray[i]);
|
|
|
|
if(matcher.lookingAt(*status)){
|
|
|
|
UnicodeString out = matcher.group(1, *status);
|
|
|
|
|
|
|
|
return out.extract(*dest, destCapacity,*status);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#define AT_SIGN 0x0040
|
|
|
|
|
|
|
|
U_CFUNC int32_t
|
|
|
|
getDescription( const UChar* source, int32_t srcLen,
|
|
|
|
UChar** dest, int32_t destCapacity,
|
|
|
|
UErrorCode* status){
|
|
|
|
if(status == NULL || U_FAILURE(*status)){
|
2003-08-28 21:04:53 +00:00
|
|
|
return 0;
|
2003-08-26 02:36:57 +00:00
|
|
|
}
|
|
|
|
|
2003-09-10 01:05:44 +00:00
|
|
|
UnicodeString stringArray[MAX_SPLIT_STRINGS];
|
2014-01-10 21:41:03 +00:00
|
|
|
RegexPattern *pattern = RegexPattern::compile(UnicodeString("@"), UREGEX_MULTILINE, *status);
|
2003-12-02 03:03:23 +00:00
|
|
|
UnicodeString src(source, srcLen);
|
2003-08-26 02:36:57 +00:00
|
|
|
|
|
|
|
if (U_FAILURE(*status)) {
|
|
|
|
return 0;
|
|
|
|
}
|
2003-09-10 01:05:44 +00:00
|
|
|
pattern->split(src, stringArray,MAX_SPLIT_STRINGS , *status);
|
2003-08-26 02:36:57 +00:00
|
|
|
|
|
|
|
if(stringArray[0].indexOf((UChar)AT_SIGN)==-1){
|
|
|
|
int32_t destLen = stringArray[0].extract(*dest, destCapacity, *status);
|
|
|
|
return trim(*dest, destLen, status);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-09-10 01:05:44 +00:00
|
|
|
U_CFUNC int32_t
|
|
|
|
getCount(const UChar* source, int32_t srcLen,
|
|
|
|
UParseCommentsOption option, UErrorCode *status){
|
|
|
|
|
|
|
|
if(status == NULL || U_FAILURE(*status)){
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString stringArray[MAX_SPLIT_STRINGS];
|
2014-01-10 21:41:03 +00:00
|
|
|
RegexPattern *pattern = RegexPattern::compile(UnicodeString("@"), UREGEX_MULTILINE, *status);
|
2003-12-02 03:03:23 +00:00
|
|
|
UnicodeString src (source, srcLen);
|
2003-09-10 01:05:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
if (U_FAILURE(*status)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
int32_t retLen = pattern->split(src, stringArray, MAX_SPLIT_STRINGS, *status);
|
|
|
|
|
2014-01-10 22:51:31 +00:00
|
|
|
UnicodeString patternString(patternStrings[option]);
|
|
|
|
RegexMatcher matcher(patternString, UREGEX_DOTALL, *status);
|
2003-09-10 01:05:44 +00:00
|
|
|
if (U_FAILURE(*status)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
int32_t count = 0;
|
2003-12-02 03:03:23 +00:00
|
|
|
for(int32_t i=0; i<retLen; i++){
|
2003-09-10 01:05:44 +00:00
|
|
|
matcher.reset(stringArray[i]);
|
|
|
|
if(matcher.lookingAt(*status)){
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(option == UPC_TRANSLATE && count > 1){
|
|
|
|
fprintf(stderr, "Multiple @translate tags cannot be supported.\n");
|
|
|
|
exit(U_UNSUPPORTED_ERROR);
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
U_CFUNC int32_t
|
|
|
|
getAt(const UChar* source, int32_t srcLen,
|
|
|
|
UChar** dest, int32_t destCapacity,
|
|
|
|
int32_t index,
|
|
|
|
UParseCommentsOption option,
|
|
|
|
UErrorCode* status){
|
|
|
|
|
|
|
|
if(status == NULL || U_FAILURE(*status)){
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString stringArray[MAX_SPLIT_STRINGS];
|
2014-01-10 21:41:03 +00:00
|
|
|
RegexPattern *pattern = RegexPattern::compile(UnicodeString("@"), UREGEX_MULTILINE, *status);
|
2003-12-02 03:03:23 +00:00
|
|
|
UnicodeString src (source, srcLen);
|
2003-09-10 01:05:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
if (U_FAILURE(*status)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
int32_t retLen = pattern->split(src, stringArray, MAX_SPLIT_STRINGS, *status);
|
|
|
|
|
2014-01-10 22:51:31 +00:00
|
|
|
UnicodeString patternString(patternStrings[option]);
|
|
|
|
RegexMatcher matcher(patternString, UREGEX_DOTALL, *status);
|
2003-09-10 01:05:44 +00:00
|
|
|
if (U_FAILURE(*status)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
int32_t count = 0;
|
2003-12-02 03:03:23 +00:00
|
|
|
for(int32_t i=0; i<retLen; i++){
|
2003-09-10 01:05:44 +00:00
|
|
|
matcher.reset(stringArray[i]);
|
|
|
|
if(matcher.lookingAt(*status)){
|
|
|
|
if(count == index){
|
|
|
|
UnicodeString out = matcher.group(1, *status);
|
|
|
|
return out.extract(*dest, destCapacity,*status);
|
|
|
|
}
|
|
|
|
count++;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2003-08-26 02:36:57 +00:00
|
|
|
U_CFUNC int32_t
|
|
|
|
getTranslate( const UChar* source, int32_t srcLen,
|
|
|
|
UChar** dest, int32_t destCapacity,
|
|
|
|
UErrorCode* status){
|
2014-01-10 21:41:03 +00:00
|
|
|
UnicodeString notePatternString("^translate\\s*?(.*)");
|
2003-08-26 02:36:57 +00:00
|
|
|
|
|
|
|
int32_t destLen = getText(source, srcLen, dest, destCapacity, notePatternString, status);
|
|
|
|
return trim(*dest, destLen, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
U_CFUNC int32_t
|
|
|
|
getNote(const UChar* source, int32_t srcLen,
|
|
|
|
UChar** dest, int32_t destCapacity,
|
|
|
|
UErrorCode* status){
|
|
|
|
|
2014-01-10 21:41:03 +00:00
|
|
|
UnicodeString notePatternString("^note\\s*?(.*)");
|
2003-08-26 02:36:57 +00:00
|
|
|
int32_t destLen = getText(source, srcLen, dest, destCapacity, notePatternString, status);
|
|
|
|
return trim(*dest, destLen, status);
|
|
|
|
|
2003-08-27 23:09:24 +00:00
|
|
|
}
|
2003-08-28 21:04:53 +00:00
|
|
|
|
2003-12-09 20:07:47 +00:00
|
|
|
#endif /* UCONFIG_NO_REGULAR_EXPRESSIONS */
|
|
|
|
|