Ignore:
Timestamp:
12/02/07 03:51:55 (17 years ago)
Author:
Nicholas Riley
Message:

DynaLoader-10.[45].a: Tiger and Leopard versions, since Tiger's
DynaLoader.a crashes on Leopard ppc.

DynaLoader.a: Renamed to DynaLoader-10.4.a.

English.lproj/InfoPlist.strings: Updated for 1.1b7.

Info-Pester.plist: Updated for 1.1b7.

NJRDateFormatter.m: Conditionally load ParseDate dylib (see above).

ParseDate.[hm]: Split out from NJRDateFormatter.m.

Read Me.rtfd/TXT.rtf: Updated for 1.1b7.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Cocoa/Pester/Source/NJRDateFormatter.m

    r361 r367  
    88
    99#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>
    8612
    8713// workaround for bug in Jaguar (and earlier?) NSCalendarDate dateWithNaturalLanguageString:
     
    12046+ (void)initialize;
    12147{
    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    }
    12580}
    12681
     
    13085    NSMutableArray *tryFormatters = [[NSMutableArray alloc] init];
    13186   
    132     for (NSDateFormatterStyle *s = formatterStyles ; *s < NSDateFormatterNoStyle ; *s++) {
     87    for (const NSDateFormatterStyle *s = formatterStyles ; *s < NSDateFormatterNoStyle ; *s++) {
    13388        NSDateFormatter *tryFormatter = [[NSDateFormatter alloc] init];
    13489        [tryFormatter setLenient: YES];
     
    149104    NSMutableArray *tryFormatters = [[NSMutableArray alloc] init];
    150105   
    151     for (NSDateFormatterStyle *s = formatterStyles ; *s < NSDateFormatterNoStyle ; *s++) {
     106    for (const NSDateFormatterStyle *s = formatterStyles ; *s < NSDateFormatterNoStyle ; *s++) {
    152107        NSDateFormatter *tryFormatter = [[NSDateFormatter alloc] init];
    153108        [tryFormatter setLenient: YES];
     
    196151    }
    197152   
     153    if (parse_natural_language_date == NULL) return nil;
     154
    198155    date = parse_natural_language_date(string);
    199156    if (date != nil) goto success;
     
    211168+ (BOOL)naturalLanguageParsingAvailable;
    212169{
    213     return (my_perl != NULL);
     170    return (parse_natural_language_date != NULL);
    214171}
    215172@end
Note: See TracChangeset for help on using the changeset viewer.