Changeset 367 for trunk/Cocoa/Pester/Source/NJRDateFormatter.m
- Timestamp:
- 12/02/07 03:51:55 (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Cocoa/Pester/Source/NJRDateFormatter.m
r361 r367 8 8 9 9 #import "NJRDateFormatter.h" 10 11 // generated by perl -MExtUtils::Embed -e xsinit -- -o perlxsi.c 12 #include <EXTERN.h> 13 #include <perl.h> 14 15 EXTERN_C void xs_init (pTHX); 16 17 EXTERN_C void boot_DynaLoader (pTHX_ CV* cv); 18 19 EXTERN_C void 20 xs_init(pTHX) 21 { 22 char *file = __FILE__; 23 dXSUB_SYS; 24 25 /* DynaLoader is a special case */ 26 newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file); 27 } 28 // end generated code 29 30 static PerlInterpreter *my_perl; 31 static NSDateFormatter *dateManipFormatter; 32 33 static NSDate *parse_natural_language_date(NSString *input) { 34 if (my_perl == NULL) return nil; 35 36 if ([input rangeOfString: @"|"].length > 0) { 37 NSMutableString *sanitized = [[input mutableCopy] autorelease]; 38 [sanitized replaceOccurrencesOfString: @"|" withString: @"" 39 options: NSLiteralSearch 40 range: NSMakeRange(0, [sanitized length])]; 41 input = sanitized; 42 } 43 44 NSString *temp = [[NSString alloc] initWithFormat: @"UnixDate(q|%@|, '%%q')", input]; 45 // NSLog(@"%@", temp); 46 SV *d = eval_pv([temp UTF8String], TRUE); 47 [temp release]; 48 if (d == NULL) return nil; 49 50 STRLEN s_len; 51 char *s = SvPV(d, s_len); 52 if (s == NULL || s_len == 0) return nil; 53 54 NSDate *date = [dateManipFormatter dateFromString: [NSString stringWithUTF8String: s]]; 55 // NSLog(@"%@", date); 56 57 return date; 58 } 59 60 static void init_perl(void) { 61 const char *argv[] = {"", "-CSD", "-I", "", "-MDate::Manip", "-e", "0"}; 62 argv[3] = [[[NSBundle mainBundle] resourcePath] fileSystemRepresentation]; 63 PERL_SYS_INIT(0, NULL); 64 my_perl = perl_alloc(); 65 if (my_perl == NULL) return; 66 67 perl_construct(my_perl); 68 if (perl_parse(my_perl, xs_init, 7, (char **)argv, NULL) != 0) goto fail; 69 70 PL_exit_flags |= PERL_EXIT_DESTRUCT_END; 71 if (perl_run(my_perl) != 0) goto fail; 72 73 // XXX detect localization changes 74 eval_pv("Date_Init(\"Language=English\", \"DateFormat=non-US\", \"Internal=1\"", TRUE); 75 76 if (parse_natural_language_date(@"tomorrow") == nil) goto fail; 77 78 return; 79 80 fail: 81 perl_destruct(my_perl); 82 perl_free(my_perl); 83 PERL_SYS_TERM(); 84 my_perl = NULL; 85 } 10 #import "ParseDate.h" 11 #include <dlfcn.h> 86 12 87 13 // workaround for bug in Jaguar (and earlier?) NSCalendarDate dateWithNaturalLanguageString: … … 120 46 + (void)initialize; 121 47 { 122 dateManipFormatter = [[NSDateFormatter alloc] init]; 123 [dateManipFormatter setDateFormat: @"yyyyMMddHHmmss"]; // Date::Manip's "%q" 124 init_perl(); 48 long minorVersion, majorVersion; 49 Gestalt(gestaltSystemVersionMajor, &majorVersion); 50 Gestalt(gestaltSystemVersionMinor, &minorVersion); 51 if (majorVersion != 10) 52 return; 53 54 NSString *libName; 55 if (minorVersion == 4) { 56 libName = @"libParseDate-10.4"; 57 } else if (minorVersion == 5) { 58 libName = @"libParseDate-10.5"; 59 } else { 60 return; 61 } 62 63 NSString *libPath = [[NSBundle mainBundle] pathForResource: libName ofType: @"dylib"]; 64 if (libPath == nil) 65 return; 66 67 void *lib = dlopen([libPath fileSystemRepresentation], RTLD_LAZY | RTLD_GLOBAL); 68 const char *libError; 69 if ( (libError = dlerror()) != NULL) { 70 NSLog(@"failed to dlopen %@: %s", libPath, libError); 71 return; 72 } 73 74 parse_natural_language_date = dlsym(lib, "parse_natural_language_date"); 75 if ( (libError = dlerror()) != NULL) { 76 NSLog(@"failed to look up parse_natural_language_date in %@: %s", libPath, libError); 77 parse_natural_language_date = NULL; 78 return; 79 } 125 80 } 126 81 … … 130 85 NSMutableArray *tryFormatters = [[NSMutableArray alloc] init]; 131 86 132 for ( NSDateFormatterStyle *s = formatterStyles ; *s < NSDateFormatterNoStyle ; *s++) {87 for (const NSDateFormatterStyle *s = formatterStyles ; *s < NSDateFormatterNoStyle ; *s++) { 133 88 NSDateFormatter *tryFormatter = [[NSDateFormatter alloc] init]; 134 89 [tryFormatter setLenient: YES]; … … 149 104 NSMutableArray *tryFormatters = [[NSMutableArray alloc] init]; 150 105 151 for ( NSDateFormatterStyle *s = formatterStyles ; *s < NSDateFormatterNoStyle ; *s++) {106 for (const NSDateFormatterStyle *s = formatterStyles ; *s < NSDateFormatterNoStyle ; *s++) { 152 107 NSDateFormatter *tryFormatter = [[NSDateFormatter alloc] init]; 153 108 [tryFormatter setLenient: YES]; … … 196 151 } 197 152 153 if (parse_natural_language_date == NULL) return nil; 154 198 155 date = parse_natural_language_date(string); 199 156 if (date != nil) goto success; … … 211 168 + (BOOL)naturalLanguageParsingAvailable; 212 169 { 213 return ( my_perl!= NULL);170 return (parse_natural_language_date != NULL); 214 171 } 215 172 @end
Note:
See TracChangeset
for help on using the changeset viewer.