diff --git a/AppIcon.png b/AppIcon.png new file mode 100644 index 0000000..a2c4035 Binary files /dev/null and b/AppIcon.png differ diff --git a/DUImageView.m b/DUImageView.m index 496aae4..e5d5dc0 100644 --- a/DUImageView.m +++ b/DUImageView.m @@ -30,13 +30,15 @@ - (BOOL) performDragOperation: (id < NSDraggingInfo >) sender NSPasteboard *pboard = [sender draggingPasteboard]; BOOL successful = NO; - if ([[pboard types] containsObject: NSPasteboardTypeFileURL]) - { - NSArray *files = [pboard propertyListForType: NSPasteboardTypeFileURL]; - [controller startConversion: [files objectAtIndex: 0]]; + if (pboard.pasteboardItems.count <= 1) { + //直接获取文件路径 + + NSString *fileURL = [[NSURL URLFromPasteboard:pboard] path]; + [controller startConversion:fileURL]; successful = NO; } - + + return successful; } diff --git a/DUWindow.m b/DUWindow.m index 31897ef..529f7ec 100644 --- a/DUWindow.m +++ b/DUWindow.m @@ -18,10 +18,10 @@ - (NSDragOperation) draggingEntered: (id < NSDraggingInfo >) sender { NSPasteboard *pboard = [sender draggingPasteboard]; NSDragOperation opType = NSDragOperationNone; - + if ([[pboard types] containsObject: NSPasteboardTypeFileURL]) opType = NSDragOperationCopy; - + return opType; } @@ -29,14 +29,18 @@ - (BOOL) performDragOperation: (id < NSDraggingInfo >) sender { NSPasteboard *pboard = [sender draggingPasteboard]; BOOL successful = NO; - - if ([[pboard types] containsObject: NSPasteboardTypeFileURL]) - { - NSArray *files = [pboard propertyListForType: NSPasteboardTypeFileURL]; - [controller startConversion: [files objectAtIndex: 0]]; + + // 判断是否拖进单文件 + + if (pboard.pasteboardItems.count <= 1) { + //直接获取文件路径 + + NSString *fileURL = [[NSURL URLFromPasteboard:pboard] path]; + [controller startConversion:fileURL]; successful = NO; } - + + return successful; } diff --git a/DictUnifier.xcodeproj/project.pbxproj b/DictUnifier.xcodeproj/project.pbxproj index b2b9ce2..8a6ec39 100644 --- a/DictUnifier.xcodeproj/project.pbxproj +++ b/DictUnifier.xcodeproj/project.pbxproj @@ -7,9 +7,6 @@ objects = { /* Begin PBXBuildFile section */ - 07048BBB13DDF11D00BD86F9 /* libz.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 07048BBA13DDF11D00BD86F9 /* libz.dylib */; }; - 07048BBD13DDF12400BD86F9 /* Python.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 07048BBC13DDF12400BD86F9 /* Python.framework */; }; - 071DC178114CA6420062577C /* lua.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 071DC176114CA6420062577C /* lua.cpp */; }; 075208FD1B799D2700CD10AD /* libglib-2.0.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 075208FC1B799D2700CD10AD /* libglib-2.0.a */; }; 07687D941143E9C30032F3CD /* DictUnifier.icns in Resources */ = {isa = PBXBuildFile; fileRef = 07687D931143E9C30032F3CD /* DictUnifier.icns */; }; 07687DB91143EF690032F3CD /* done.png in Resources */ = {isa = PBXBuildFile; fileRef = 07687DB71143EF690032F3CD /* done.png */; }; @@ -42,13 +39,14 @@ 07D57A2711475FF30033F5A4 /* convert.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 07D57A1A11475FF30033F5A4 /* convert.cpp */; }; 07D57A2811475FF30033F5A4 /* dict.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 07D57A1C11475FF30033F5A4 /* dict.cpp */; }; 07D57A2911475FF30033F5A4 /* index.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 07D57A1E11475FF30033F5A4 /* index.cpp */; }; - 07D57A2B11475FF30033F5A4 /* python.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 07D57A2211475FF30033F5A4 /* python.cpp */; }; 07D57A2C11475FF30033F5A4 /* sdconv.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 07D57A2411475FF30033F5A4 /* sdconv.cpp */; }; 07D57A2D11475FF30033F5A4 /* storage.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 07D57A2511475FF30033F5A4 /* storage.cpp */; }; 07D57A7F114761780033F5A4 /* sdconv in CopyFiles */ = {isa = PBXBuildFile; fileRef = 07D579C411475D930033F5A4 /* sdconv */; }; 07D86040114811A2006EA8A6 /* DictUnifierAppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 07D8603F114811A2006EA8A6 /* DictUnifierAppDelegate.m */; }; 07D860471148128D006EA8A6 /* DUWindow.m in Sources */ = {isa = PBXBuildFile; fileRef = 07D860461148128D006EA8A6 /* DUWindow.m */; }; 07D86066114812F0006EA8A6 /* DUImageView.m in Sources */ = {isa = PBXBuildFile; fileRef = 07D86065114812F0006EA8A6 /* DUImageView.m */; }; + 3A07337326E9E12F007E0174 /* libz.tbd in Frameworks */ = {isa = PBXBuildFile; fileRef = 3A07337226E9E12F007E0174 /* libz.tbd */; }; + 3A91E82528B9BEA10067F474 /* AppIcon.png in Resources */ = {isa = PBXBuildFile; fileRef = 3A91E82428B9BEA10067F474 /* AppIcon.png */; }; 77C8C1F90C07829500965286 /* MainMenu.xib in Resources */ = {isa = PBXBuildFile; fileRef = 77C8C1F70C07829500965286 /* MainMenu.xib */; }; 8D11072D0486CEB800E47090 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 29B97316FDCFA39411CA2CEA /* main.m */; settings = {ATTRIBUTES = (); }; }; 8D11072F0486CEB800E47090 /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1058C7A1FEA54F0111CA2CBB /* Cocoa.framework */; }; @@ -128,11 +126,7 @@ /* End PBXCopyFilesBuildPhase section */ /* Begin PBXFileReference section */ - 07048BBA13DDF11D00BD86F9 /* libz.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libz.dylib; path = ../../../../../usr/lib/libz.dylib; sourceTree = ""; }; - 07048BBC13DDF12400BD86F9 /* Python.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Python.framework; path = ../../../../../System/Library/Frameworks/Python.framework; sourceTree = ""; }; 071D72BB215BFB68005B3C8B /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = Base.lproj/MainMenu.xib; sourceTree = ""; }; - 071DC176114CA6420062577C /* lua.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = lua.cpp; sourceTree = ""; }; - 071DC177114CA6420062577C /* lua.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = lua.h; sourceTree = ""; }; 075208FC1B799D2700CD10AD /* libglib-2.0.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = "libglib-2.0.a"; path = "../../../usr/local/Cellar/glib/2.44.1/lib/libglib-2.0.a"; sourceTree = ""; }; 07687D931143E9C30032F3CD /* DictUnifier.icns */ = {isa = PBXFileReference; lastKnownFileType = image.icns; path = DictUnifier.icns; sourceTree = ""; }; 07687DB71143EF690032F3CD /* done.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = done.png; sourceTree = ""; }; @@ -170,8 +164,6 @@ 07D57A1E11475FF30033F5A4 /* index.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = index.cpp; sourceTree = ""; }; 07D57A1F11475FF30033F5A4 /* index.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = index.h; sourceTree = ""; }; 07D57A2111475FF30033F5A4 /* mdk.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = mdk.h; sourceTree = ""; }; - 07D57A2211475FF30033F5A4 /* python.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = python.cpp; sourceTree = ""; }; - 07D57A2311475FF30033F5A4 /* python.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = python.h; sourceTree = ""; }; 07D57A2411475FF30033F5A4 /* sdconv.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = sdconv.cpp; sourceTree = ""; }; 07D57A2511475FF30033F5A4 /* storage.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = storage.cpp; sourceTree = ""; }; 07D57A2611475FF30033F5A4 /* storage.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = storage.h; sourceTree = ""; }; @@ -246,6 +238,8 @@ 29B97324FDCFA39411CA2CEA /* AppKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AppKit.framework; path = /System/Library/Frameworks/AppKit.framework; sourceTree = ""; }; 29B97325FDCFA39411CA2CEA /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = /System/Library/Frameworks/Foundation.framework; sourceTree = ""; }; 32CA4F630368D1EE00C91783 /* DictUnifier_Prefix.pch */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DictUnifier_Prefix.pch; sourceTree = ""; }; + 3A07337226E9E12F007E0174 /* libz.tbd */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.text-based-dylib-definition"; name = libz.tbd; path = Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.3.sdk/usr/lib/libz.tbd; sourceTree = DEVELOPER_DIR; }; + 3A91E82428B9BEA10067F474 /* AppIcon.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = AppIcon.png; sourceTree = ""; }; 8D1107310486CEB800E47090 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 8D1107320486CEB800E47090 /* DictUnifier.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = DictUnifier.app; sourceTree = BUILT_PRODUCTS_DIR; }; /* End PBXFileReference section */ @@ -255,9 +249,8 @@ isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( + 3A07337326E9E12F007E0174 /* libz.tbd in Frameworks */, 075208FD1B799D2700CD10AD /* libglib-2.0.a in Frameworks */, - 07048BBD13DDF12400BD86F9 /* Python.framework in Frameworks */, - 07048BBB13DDF11D00BD86F9 /* libz.dylib in Frameworks */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -312,8 +305,6 @@ 07D57A1911475FF30033F5A4 /* sdconv */ = { isa = PBXGroup; children = ( - 071DC176114CA6420062577C /* lua.cpp */, - 071DC177114CA6420062577C /* lua.h */, 07D57A2E114760350033F5A4 /* include */, 07D57A1A11475FF30033F5A4 /* convert.cpp */, 07D57A1B11475FF30033F5A4 /* convert.h */, @@ -322,8 +313,6 @@ 07D57A1E11475FF30033F5A4 /* index.cpp */, 07D57A1F11475FF30033F5A4 /* index.h */, 07D57A2111475FF30033F5A4 /* mdk.h */, - 07D57A2211475FF30033F5A4 /* python.cpp */, - 07D57A2311475FF30033F5A4 /* python.h */, 07D57A2411475FF30033F5A4 /* sdconv.cpp */, 07D57A2511475FF30033F5A4 /* storage.cpp */, 07D57A2611475FF30033F5A4 /* storage.h */, @@ -447,8 +436,6 @@ 29B97314FDCFA39411CA2CEA /* DictUnifier */ = { isa = PBXGroup; children = ( - 07048BBC13DDF12400BD86F9 /* Python.framework */, - 07048BBA13DDF11D00BD86F9 /* libz.dylib */, 07D57A1911475FF30033F5A4 /* sdconv */, 07D5799111475C5E0033F5A4 /* bin */, 07D5795E11475B760033F5A4 /* templates */, @@ -473,6 +460,7 @@ 29B97317FDCFA39411CA2CEA /* Resources */ = { isa = PBXGroup; children = ( + 3A91E82428B9BEA10067F474 /* AppIcon.png */, 076D37D6114A37FC00D55901 /* Localizable.strings */, 07687DB71143EF690032F3CD /* done.png */, 07687DB81143EF690032F3CD /* dropper.png */, @@ -487,6 +475,7 @@ 29B97323FDCFA39411CA2CEA /* Frameworks */ = { isa = PBXGroup; children = ( + 3A07337226E9E12F007E0174 /* libz.tbd */, 075208FC1B799D2700CD10AD /* libglib-2.0.a */, 1058C7A0FEA54F0111CA2CBB /* Linked Frameworks */, 1058C7A2FEA54F0111CA2CBB /* Other Frameworks */, @@ -579,6 +568,7 @@ 07687DB91143EF690032F3CD /* done.png in Resources */, 07687DBA1143EF690032F3CD /* dropper.png in Resources */, 076D37D7114A37FC00D55901 /* Localizable.strings in Resources */, + 3A91E82528B9BEA10067F474 /* AppIcon.png in Resources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -608,10 +598,8 @@ 07D57A2711475FF30033F5A4 /* convert.cpp in Sources */, 07D57A2811475FF30033F5A4 /* dict.cpp in Sources */, 07D57A2911475FF30033F5A4 /* index.cpp in Sources */, - 07D57A2B11475FF30033F5A4 /* python.cpp in Sources */, 07D57A2C11475FF30033F5A4 /* sdconv.cpp in Sources */, 07D57A2D11475FF30033F5A4 /* storage.cpp in Sources */, - 071DC178114CA6420062577C /* lua.cpp in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -683,6 +671,7 @@ "$(inherited)", /usr/local/lib, ); + MACOSX_DEPLOYMENT_TARGET = 11.0; OTHER_LDFLAGS = ""; PRODUCT_NAME = sdconv; SDKROOT = ""; @@ -706,6 +695,7 @@ "$(inherited)", /usr/local/lib, ); + MACOSX_DEPLOYMENT_TARGET = 11.0; OTHER_LDFLAGS = ""; PRODUCT_NAME = sdconv; SDKROOT = ""; @@ -716,6 +706,7 @@ C01FCF4B08A954540054247B /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { + CLANG_ENABLE_OBJC_ARC = YES; CLANG_ENABLE_OBJC_WEAK = YES; COMBINE_HIDPI_IMAGES = YES; COPY_PHASE_STRIP = NO; @@ -732,6 +723,7 @@ "$(inherited)", "\"$(SRCROOT)\"", ); + MACOSX_DEPLOYMENT_TARGET = 11.0; PRODUCT_BUNDLE_IDENTIFIER = "org.jjgod.${PRODUCT_NAME:identifier}"; PRODUCT_NAME = DictUnifier; SDKROOT = ""; @@ -744,6 +736,7 @@ C01FCF4C08A954540054247B /* Release */ = { isa = XCBuildConfiguration; buildSettings = { + CLANG_ENABLE_OBJC_ARC = YES; CLANG_ENABLE_OBJC_WEAK = YES; COMBINE_HIDPI_IMAGES = YES; CURRENT_PROJECT_VERSION = 3; @@ -758,6 +751,7 @@ "$(inherited)", "\"$(SRCROOT)\"", ); + MACOSX_DEPLOYMENT_TARGET = 11.0; PRODUCT_BUNDLE_IDENTIFIER = "org.jjgod.${PRODUCT_NAME:identifier}"; PRODUCT_NAME = DictUnifier; SDKROOT = ""; diff --git a/DictUnifier.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist b/DictUnifier.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist new file mode 100644 index 0000000..18d9810 --- /dev/null +++ b/DictUnifier.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist @@ -0,0 +1,8 @@ + + + + + IDEDidComputeMac32BitWarning + + + diff --git a/DictUnifierAppDelegate.m b/DictUnifierAppDelegate.m index 395ed98..182af86 100644 --- a/DictUnifierAppDelegate.m +++ b/DictUnifierAppDelegate.m @@ -19,7 +19,7 @@ - (id) init self.buildTask = nil; self.totalEntries = 1; } - + return self; } @@ -40,10 +40,13 @@ - (void) startConversion: (NSString *) dictFile - (void) showProgress { - [dropper setHidden: YES]; - [progressBar startAnimation: nil]; - [progressBar setIndeterminate: YES]; - [progressBar setHidden: NO]; + dispatch_async(dispatch_get_main_queue(), ^{ + [dropper setHidden: YES]; + [progressBar startAnimation: nil]; + [progressBar setIndeterminate: YES]; + [progressBar setHidden: NO]; + }); + } - (void) hideProgress @@ -54,197 +57,196 @@ - (void) hideProgress - (void) setProgress: (double) curr { - [progressBar setDoubleValue: curr]; + dispatch_async(dispatch_get_main_queue(), ^{ + [progressBar setDoubleValue: curr]; + }); } - (void) prepareName: (NSArray *) outputArray { - NSAutoreleasePool *pool = [NSAutoreleasePool new]; - - [self hideProgress]; - [nameField setStringValue: [outputArray objectAtIndex: 0]]; - - self.totalEntries = [[outputArray objectAtIndex: 1] integerValue]; - NSLog(@"totalEntries = %lu", self.totalEntries); - if (self.totalEntries <= 0) - self.totalEntries = 1; - - [self setStatus: NSLocalizedString(@"Enter a name to start building", "")]; - [nameField setHidden: NO]; - [nameField setEnabled: YES]; - - [button setAction: @selector(startBuilding:)]; - [button setTitle: NSLocalizedString(@"Start", "")]; - [button setHidden: NO]; - - [pool release]; + @autoreleasepool { + [self hideProgress]; + [nameField setStringValue: [outputArray objectAtIndex: 0]]; + + self.totalEntries = [[outputArray objectAtIndex: 1] integerValue]; + NSLog(@"totalEntries = %lu", self.totalEntries); + if (self.totalEntries <= 0) + self.totalEntries = 1; + + [self setStatus: NSLocalizedString(@"Enter a name to start building", "")]; + [nameField setHidden: NO]; + [nameField setEnabled: YES]; + + [button setAction: @selector(startBuilding:)]; + [button setTitle: NSLocalizedString(@"Start", "")]; + [button setHidden: NO]; + } } - (void) startConversionWith: (NSString *) dictFile { NSLog(@"startConversionWith: %@", dictFile); - - NSAutoreleasePool *pool = [NSAutoreleasePool new]; - NSFileManager *manager = [[NSFileManager alloc] init]; - - [self cleanup]; - - NSLog(@"Creating %@", self.tempDir); - - if (! [manager createDirectoryAtPath: self.tempDir - withIntermediateDirectories: YES - attributes: nil - error: NULL]) - { - NSLog(@"Failed to create directory %@", self.tempDir); - goto exit; - } - - NSLog(@"dict extension: %@", [dictFile pathExtension]); - NSString *ifoFile = nil; - - if ([[dictFile pathExtension] isEqual: @"ifo"]) - ifoFile = dictFile; - - // Extract that file if it is a bzipped archive - else if ([[dictFile pathExtension] isEqual: @"bz2"]) - { - NSTask *task = [NSTask launchedTaskWithLaunchPath: @"/usr/bin/tar" - arguments: [NSArray arrayWithObjects: - @"-xjf", dictFile, - @"-C", self.tempDir, nil]]; - [task waitUntilExit]; - - if ([task terminationStatus]) + + @autoreleasepool { + NSFileManager *manager = [[NSFileManager alloc] init]; + + [self cleanup]; + + NSLog(@"Creating %@", self.tempDir); + + if (![manager createDirectoryAtPath: self.tempDir + withIntermediateDirectories: YES + attributes: nil + error: NULL]) { - NSLog(@"Failed to untar %@ at %@", dictFile, self.tempDir); - goto exit; + NSLog(@"Failed to create directory %@", self.tempDir); + return; } - - NSArray *dirContents = [manager contentsOfDirectoryAtPath: self.tempDir error: NULL]; - if ([dirContents count]) + + NSLog(@"dict extension: %@", [dictFile pathExtension]); + NSString *ifoFile = nil; + + if ([[dictFile pathExtension] isEqual: @"ifo"]) + ifoFile = dictFile; + + // Extract that file if it is a bzipped archive + else if ([[dictFile pathExtension] isEqual: @"bz2"]) { - NSString *extractedPath = [self.tempDir stringByAppendingPathComponent: - [dirContents objectAtIndex: 0]]; - dirContents = [manager contentsOfDirectoryAtPath: extractedPath error: NULL]; - for (NSString *file in dirContents) - if ([[file pathExtension] isEqual: @"ifo"]) - ifoFile = [extractedPath stringByAppendingPathComponent: file]; + NSTask *task = [NSTask launchedTaskWithLaunchPath: @"/usr/bin/tar" + arguments: [NSArray arrayWithObjects: + @"-xjf", dictFile, + @"-C", self.tempDir, nil]]; + [task waitUntilExit]; + + if ([task terminationStatus]) + { + NSLog(@"Failed to untar %@ at %@", dictFile, self.tempDir); + return; + } + + NSArray *dirContents = [manager contentsOfDirectoryAtPath: self.tempDir error: NULL]; + if ([dirContents count]) + { + NSString *extractedPath = [self.tempDir stringByAppendingPathComponent: + [dirContents objectAtIndex: 0]]; + dirContents = [manager contentsOfDirectoryAtPath: extractedPath error: NULL]; + for (NSString *file in dirContents) + if ([[file pathExtension] isEqual: @"ifo"]) + ifoFile = [extractedPath stringByAppendingPathComponent: file]; + } } + + if (!ifoFile) + { + NSLog(@"Failed to find any matching ifo file."); + return; + } + + NSLog(@"ifoFile = %@", ifoFile); + self.dictID = [[ifoFile lastPathComponent] stringByDeletingPathExtension]; + NSLog(@"dictID = %@", self.dictID); + + self.dictDir = [self.tempDir stringByAppendingPathComponent: + [NSString stringWithFormat: @"dict-%@", self.dictID]]; + + if (![manager createDirectoryAtPath: self.dictDir + withIntermediateDirectories: YES + attributes: nil + error: NULL]) + { + NSLog(@"Failed to create directory %@", self.dictDir); + return; + } + + NSBundle *bundle = [NSBundle mainBundle]; + NSString *templateDir = [[bundle resourcePath] stringByAppendingPathComponent: @"templates"]; + NSArray *dirContents = [manager contentsOfDirectoryAtPath: templateDir error: NULL]; + + // Copy all the files under templates directory to temporary dictionary building directory + for (NSString *file in dirContents) + [manager copyItemAtPath: [templateDir stringByAppendingPathComponent: file] + toPath: [self.dictDir stringByAppendingPathComponent: file] + error: NULL]; + + [self showProgress]; + [self setStatus: [NSString stringWithFormat: + NSLocalizedString(@"Converting %@...", ""), + [dictFile lastPathComponent]]]; + + // Prepare to run the actual conversion utility: sdconv with ifoFile as source file + NSTask *task = [[NSTask alloc] init]; + NSPipe *pipe = [NSPipe pipe]; + + [task setLaunchPath: [bundle pathForAuxiliaryExecutable: @"sdconv"]]; + [task setArguments: [NSArray arrayWithObjects: + ifoFile, [self.dictDir stringByAppendingPathComponent: @"Dictionary.xml"], nil]]; + [task setStandardOutput: pipe]; + [task setStandardError: pipe]; + + [task launch]; + [task waitUntilExit]; + if ([task terminationStatus]) + { + NSLog(@"Failed to run sdconv."); + [self error: [NSString stringWithFormat: + NSLocalizedString(@"Convert %@ failed, abort now.", ""), self.dictID]]; + [self hideProgress]; + [dropper setHidden: NO]; + return; + } + + NSFileHandle *handle = [pipe fileHandleForReading]; + NSString *output = [[NSString alloc] initWithData: [handle readDataToEndOfFile] + encoding: NSUTF8StringEncoding]; + + [self performSelectorOnMainThread: @selector(prepareName:) + withObject: [output componentsSeparatedByString: @" "] + waitUntilDone: YES]; + } - - if (! ifoFile) - { - NSLog(@"Failed to find any matching ifo file."); - goto exit; - } - - NSLog(@"ifoFile = %@", ifoFile); - self.dictID = [[[ifoFile lastPathComponent] stringByDeletingPathExtension] retain]; - NSLog(@"dictID = %@", self.dictID); - - self.dictDir = [self.tempDir stringByAppendingPathComponent: - [NSString stringWithFormat: @"dict-%@", self.dictID]]; - - if (! [manager createDirectoryAtPath: self.dictDir - withIntermediateDirectories: YES - attributes: nil - error: NULL]) - { - NSLog(@"Failed to create directory %@", self.dictDir); - goto exit; - } - - NSBundle *bundle = [NSBundle mainBundle]; - NSString *templateDir = [[bundle resourcePath] stringByAppendingPathComponent: @"templates"]; - NSArray *dirContents = [manager contentsOfDirectoryAtPath: templateDir error: NULL]; - - // Copy all the files under templates directory to temporary dictionary building directory - for (NSString *file in dirContents) - [manager copyItemAtPath: [templateDir stringByAppendingPathComponent: file] - toPath: [self.dictDir stringByAppendingPathComponent: file] - error: NULL]; - - [self showProgress]; - [self setStatus: [NSString stringWithFormat: - NSLocalizedString(@"Converting %@...", ""), - [dictFile lastPathComponent]]]; - - // Prepare to run the actual conversion utility: sdconv with ifoFile as source file - NSTask *task = [[[NSTask alloc] init] autorelease]; - NSPipe *pipe = [NSPipe pipe]; - - [task setLaunchPath: [bundle pathForAuxiliaryExecutable: @"sdconv"]]; - [task setArguments: [NSArray arrayWithObjects: - ifoFile, [self.dictDir stringByAppendingPathComponent: @"Dictionary.xml"], nil]]; - [task setStandardOutput: pipe]; - [task setStandardError: pipe]; - - [task launch]; - [task waitUntilExit]; - if ([task terminationStatus]) - { - NSLog(@"Failed to run sdconv."); - [self error: [NSString stringWithFormat: - NSLocalizedString(@"Convert %@ failed, abort now.", ""), self.dictID]]; - [self hideProgress]; - [dropper setHidden: NO]; - goto exit; - } - - NSFileHandle *handle = [pipe fileHandleForReading]; - NSString *output = [[[NSString alloc] initWithData: [handle readDataToEndOfFile] - encoding: NSUTF8StringEncoding] autorelease]; - - [self performSelectorOnMainThread: @selector(prepareName:) - withObject: [output componentsSeparatedByString: @" "] - waitUntilDone: YES]; - -exit: - [manager release]; - [pool release]; } - (void) showDone { - [button setHidden: YES]; - [self hideProgress]; - - NSString *imageFile = [[[NSBundle mainBundle] resourcePath] - stringByAppendingPathComponent: @"done.png"]; - NSImage *done = [[NSImage alloc] initWithContentsOfFile: imageFile]; - [dropper setImage: done]; - [done release]; - - [dropper setHidden: NO]; - [self setStatus: NSLocalizedString(@"Done", "")]; + dispatch_async(dispatch_get_main_queue(), ^{ + [button setHidden: YES]; + [self hideProgress]; + + NSString *imageFile = [[[NSBundle mainBundle] resourcePath] + stringByAppendingPathComponent: @"done.png"]; + NSImage *done = [[NSImage alloc] initWithContentsOfFile: imageFile]; + [dropper setImage: done]; + + [dropper setHidden: NO]; + [self setStatus: NSLocalizedString(@"Done", "")]; + }); } - (int) runProgram: (NSString *) program withArguments: (NSArray *) arguments { NSTask *task = [NSTask launchedTaskWithLaunchPath: program arguments: arguments]; - + [task waitUntilExit]; return [task terminationStatus]; } - (void) taskFileHandleRead: (NSNotification *) notification { - NSDictionary *userInfo = [notification userInfo]; - int error = [[userInfo objectForKey: @"NSFileHandleError"] intValue]; - + NSDictionary *userInfo = [notification userInfo]; + int error = [[userInfo objectForKey: @"NSFileHandleError"] intValue]; + if (error) NSLog(@"DictUnifier: error %d.", error); else { - NSData *data = [userInfo objectForKey: NSFileHandleNotificationDataItem]; - NSUInteger length = [data length]; + NSData *data = [userInfo objectForKey: NSFileHandleNotificationDataItem]; + NSUInteger length = [data length]; if (length == 0) return; - + NSString *str = [NSString stringWithUTF8String: [data bytes]]; - + if ([str hasPrefix: @"- "]) { NSUInteger start, end; @@ -265,156 +267,165 @@ - (void) taskFileHandleRead: (NSNotification *) notification { int fd = open([bodyList fileSystemRepresentation], O_RDONLY); NSLog(@"Start watching %@", bodyList); - [progressBar setIndeterminate: NO]; + dispatch_async(dispatch_get_main_queue(), ^{ + [progressBar setIndeterminate: NO]; + }); + unsigned long mask = DISPATCH_VNODE_DELETE | DISPATCH_VNODE_WRITE | DISPATCH_VNODE_EXTEND | DISPATCH_VNODE_ATTRIB | DISPATCH_VNODE_LINK | DISPATCH_VNODE_RENAME | DISPATCH_VNODE_REVOKE; dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); - dispatch_source_t fileSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_READ, fd, 0, globalQueue); + + dispatch_source_t fileSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_VNODE, fd, mask, globalQueue); dispatch_source_set_cancel_handler(fileSource, ^{ close(fd); } ); dispatch_source_set_event_handler(fileSource, ^{ - char buf[1024]; - ssize_t len = read(fd, buf, sizeof(buf)); - if (len > 0) { - // NSLog(@"Got data from stdin: %.*s", len, buf); - ssize_t i; - // scan backwards for the first \t, it's where the last processed item number ends - for (i = len - 1; i >= 0 && buf[i] != '\t'; i--) - ; - // scan backwards to read in the last processed item number - if (i > 0 && buf[i] == '\t') { - ssize_t end = i; - for (i--; i >= 0 && buf[i] != '\n'; i--) + unsigned long l = dispatch_source_get_data(fileSource); + if (l & DISPATCH_VNODE_DELETE) { + printf("watched file deleted! cancelling source\n"); + dispatch_source_cancel(fileSource); + }else { + char buf[1024]; + ssize_t len = read(fd, buf, sizeof(buf)); + if (len > 0) { +// NSLog(@"Got data from stdin: %.*s", len, buf); + ssize_t i; + // scan backwards for the first \t, it's where the last processed item number ends + for (i = len - 1; i >= 0 && buf[i] != '\t'; i--) ; - if (buf[i] == '\n') { - buf[end] = '\0'; - char *str = buf + i + 1; - int curr = 0; - sscanf(str, "%d", &curr); - [self setProgress: curr * 100.0 / self.totalEntries]; + // scan backwards to read in the last processed item number + if (i > 0 && buf[i] == '\t') { + ssize_t end = i; + for (i--; i >= 0 && buf[i] != '\n'; i--) + ; + if (buf[i] == '\n') { + buf[end] = '\0'; + char *str = buf + i + 1; + int curr = 0; + sscanf(str, "%d", &curr); + [self setProgress: curr * 100.0 / self.totalEntries]; + [self setStatus:[NSString stringWithFormat:@"Adding body data:%@/%@", @(curr), @(self.totalEntries)]]; + } } } } }); dispatch_resume(fileSource); break; - } else + } else { sleep(0.5); + } } } } - + [[notification object] readInBackgroundAndNotify]; } } - (void) startBuildingWith: (NSString *) dictName { - NSAutoreleasePool *pool = [NSAutoreleasePool new]; - [self setStatus: [NSString stringWithFormat: NSLocalizedString(@"Building %@...", ""), dictName]]; - - NSLog(@"dictDir = %@", self.dictDir); - - // Set the name and id strings accordingly for the dictionary to build - NSString *plistPath = [self.dictDir stringByAppendingPathComponent: @"DictInfo.plist"]; - NSMutableDictionary *plist = [NSMutableDictionary dictionaryWithContentsOfFile: plistPath]; - - [plist setObject: dictName forKey: @"CFBundleDisplayName"]; - [plist setObject: dictName forKey: @"CFBundleName"]; - [plist setObject: [NSString stringWithFormat: @"com.apple.dictionary.%@", self.dictID] - forKey: @"CFBundleIdentifier"]; - - [plist writeToFile: plistPath atomically: YES]; - - // Construct the arguments to invoke build_dict.sh from Dictionary - // Development Kit - NSBundle *bundle = [NSBundle mainBundle]; - NSString *binaryDir = [[bundle resourcePath] stringByAppendingPathComponent: @"bin"]; - NSTask *task = [[[NSTask alloc] init] autorelease]; - NSPipe *pipe = [NSPipe pipe]; - NSDictionary *environments = [NSDictionary dictionaryWithObjectsAndKeys: - @"en_US.UTF-8", @"LANG", - binaryDir, @"DICT_BUILD_TOOL_BIN", nil]; - NSMutableArray *arguments = [NSMutableArray arrayWithObjects:self.dictID, @"Dictionary.xml", - @"Dictionary.css", @"DictInfo.plist", nil]; - - // If we have Mac OS X 10.6, use the new (compress) feature provided by Dictionary Development Kit. - NSDictionary *version = [NSDictionary dictionaryWithContentsOfFile:@"/System/Library/CoreServices/SystemVersion.plist"]; - NSString *productVersion = [version objectForKey:@"ProductVersion"]; - NSInteger versionMinor = [[[productVersion componentsSeparatedByString:@"."] lastObject] integerValue]; - if (versionMinor >= 6) { - [arguments insertObject: @"-v" atIndex: 0]; - [arguments insertObject: versionMinor >= 11 ? @"10.11" : @"10.6" atIndex: 1]; - NSLog(@"%@", arguments); - } - - [task setEnvironment: environments]; - [task setCurrentDirectoryPath: self.dictDir]; - [task setLaunchPath: [binaryDir stringByAppendingPathComponent: @"build_dict.sh"]]; - [task setArguments: arguments]; - [task setStandardOutput: pipe]; - [task setStandardError: pipe]; - - // Read process output in background and update status accordingly - NSFileHandle *readHandle = [pipe fileHandleForReading]; - [[NSNotificationCenter defaultCenter] addObserver: self - selector: @selector(taskFileHandleRead:) - name: NSFileHandleReadCompletionNotification - object: readHandle]; - [readHandle readInBackgroundAndNotify]; - [task launch]; - - self.buildTask = task; - [task waitUntilExit]; - self.buildTask = nil; - - NSString *dictBasename = [NSString stringWithFormat: @"%@.dictionary", self.dictID]; - NSString *srcDict = [[self.dictDir stringByAppendingPathComponent: @"objects"] - stringByAppendingPathComponent: dictBasename]; - NSFileManager *manager = [[[NSFileManager alloc] init] autorelease]; - BOOL isDirectory; - - if (! [task terminationStatus] && - [manager fileExistsAtPath: srcDict isDirectory: &isDirectory] && isDirectory) - { - NSString *destDir = [@"~/Library/Dictionaries" stringByExpandingTildeInPath]; - NSString *destDict = [destDir stringByAppendingPathComponent: dictBasename]; - - [self setStatus: [NSString stringWithFormat: - NSLocalizedString(@"Installing into %@...", ""), destDir]]; - - NSLog(@"Creating %@", destDir); - [self runProgram: @"/bin/mkdir" withArguments: [NSArray arrayWithObjects: @"-p", destDir, nil]]; - NSLog(@"Removing %@", destDict); - [self runProgram: @"/bin/rm" withArguments: [NSArray arrayWithObjects: @"-rf", destDict, nil]]; - NSLog(@"Installing %@ to %@", srcDict, destDict); - [self runProgram: @"/usr/bin/ditto" - withArguments: [NSArray arrayWithObjects: - @"--noextattr", @"--norsrc", srcDict, destDict, nil]]; - NSLog(@"Done."); - [self showDone]; + @autoreleasepool { + [self setStatus: [NSString stringWithFormat: NSLocalizedString(@"Building %@...", ""), dictName]]; + + NSLog(@"dictDir = %@", self.dictDir); + + // Set the name and id strings accordingly for the dictionary to build + NSString *plistPath = [self.dictDir stringByAppendingPathComponent: @"DictInfo.plist"]; + NSMutableDictionary *plist = [NSMutableDictionary dictionaryWithContentsOfFile: plistPath]; + + [plist setObject: dictName forKey: @"CFBundleDisplayName"]; + [plist setObject: dictName forKey: @"CFBundleName"]; + [plist setObject: [NSString stringWithFormat: @"com.apple.dictionary.%@", self.dictID] + forKey: @"CFBundleIdentifier"]; + + [plist writeToFile: plistPath atomically: YES]; + + // Construct the arguments to invoke build_dict.sh from Dictionary + // Development Kit + NSBundle *bundle = [NSBundle mainBundle]; + NSString *binaryDir = [[bundle resourcePath] stringByAppendingPathComponent: @"bin"]; + NSTask *task = [[NSTask alloc] init]; + NSPipe *pipe = [NSPipe pipe]; + NSDictionary *environments = [NSDictionary dictionaryWithObjectsAndKeys: + @"en_US.UTF-8", @"LANG", + binaryDir, @"DICT_BUILD_TOOL_BIN", nil]; + NSMutableArray *arguments = [NSMutableArray arrayWithObjects:self.dictID, @"Dictionary.xml", + @"Dictionary.css", @"DictInfo.plist", nil]; + + // If we have Mac OS X 10.6, use the new (compress) feature provided by Dictionary Development Kit. + NSDictionary *version = [NSDictionary dictionaryWithContentsOfFile:@"/System/Library/CoreServices/SystemVersion.plist"]; + NSString *productVersion = [version objectForKey:@"ProductVersion"]; + NSInteger versionMinor = [[[productVersion componentsSeparatedByString:@"."] lastObject] integerValue]; + if (versionMinor >= 6) { + [arguments insertObject: @"-v" atIndex: 0]; + [arguments insertObject: versionMinor >= 11 ? @"10.11" : @"10.6" atIndex: 1]; + NSLog(@"%@", arguments); + } + + [task setEnvironment: environments]; + [task setCurrentDirectoryPath: self.dictDir]; + [task setLaunchPath: [binaryDir stringByAppendingPathComponent: @"build_dict.sh"]]; + [task setArguments: arguments]; + [task setStandardOutput: pipe]; + [task setStandardError: pipe]; + + // Read process output in background and update status accordingly + NSFileHandle *readHandle = [pipe fileHandleForReading]; + [[NSNotificationCenter defaultCenter] addObserver: self + selector: @selector(taskFileHandleRead:) + name: NSFileHandleReadCompletionNotification + object: readHandle]; + [readHandle readInBackgroundAndNotify]; + [task launch]; + + self.buildTask = task; + [task waitUntilExit]; + self.buildTask = nil; + + NSString *dictBasename = [NSString stringWithFormat: @"%@.dictionary", self.dictID]; + NSString *srcDict = [[self.dictDir stringByAppendingPathComponent: @"objects"] + stringByAppendingPathComponent: dictBasename]; + NSFileManager *manager = [[NSFileManager alloc] init]; + BOOL isDirectory; + + if (![task terminationStatus] && + [manager fileExistsAtPath: srcDict isDirectory: &isDirectory] && isDirectory) + { + NSString *destDir = [@"~/Library/Dictionaries" stringByExpandingTildeInPath]; + NSString *destDict = [destDir stringByAppendingPathComponent: dictBasename]; + + [self setStatus: [NSString stringWithFormat: + NSLocalizedString(@"Installing into %@...", ""), destDir]]; + + NSLog(@"Creating %@", destDir); + [self runProgram: @"/bin/mkdir" withArguments: [NSArray arrayWithObjects: @"-p", destDir, nil]]; + NSLog(@"Removing %@", destDict); + [self runProgram: @"/bin/rm" withArguments: [NSArray arrayWithObjects: @"-rf", destDict, nil]]; + NSLog(@"Installing %@ to %@", srcDict, destDict); + [self runProgram: @"/usr/bin/ditto" + withArguments: [NSArray arrayWithObjects: + @"--noextattr", @"--norsrc", srcDict, destDict, nil]]; + NSLog(@"Done."); + [self showDone]; + } + + [self cleanup]; } - - [self cleanup]; - [pool release]; + } - (IBAction) startBuilding: (id) sender { [nameField setHidden: YES]; [self showProgress]; - + [button setAction: @selector(stop:)]; [button setTitle: NSLocalizedString(@"Stop", "")]; - + [self performSelectorInBackground: @selector(startBuildingWith:) withObject: [nameField stringValue]]; } - (void) dealloc { - [buildTask release]; - [dictID release]; - [dictDir release]; - [tempDir release]; - [super dealloc]; + } - (void) cleanup @@ -433,7 +444,11 @@ - (void) cleanup - (void) setStatus: (NSString *) str { - [label setStringValue: str]; + NSLog(@"status: %@", str); + dispatch_async(dispatch_get_main_queue(), ^{ + [label setStringValue: str]; + }); + } - (void) error: (NSString *) str @@ -449,7 +464,7 @@ - (IBAction) stop: (id) sender [self.buildTask terminate]; system("killall add_body_record"); } - + [self hideProgress]; [button setHidden: YES]; [dropper setHidden: NO]; diff --git a/Info.plist b/Info.plist index 463ae8c..9eaacb4 100644 --- a/Info.plist +++ b/Info.plist @@ -7,7 +7,7 @@ CFBundleExecutable ${EXECUTABLE_NAME} CFBundleIconFile - DictUnifier.icns + AppIcon.png CFBundleIdentifier $(PRODUCT_BUNDLE_IDENTIFIER) CFBundleInfoDictionaryVersion diff --git a/README.md b/README.md index 4cb1243..bd9d987 100644 --- a/README.md +++ b/README.md @@ -2,15 +2,27 @@ Dictionary conversion tool for Mac OS X 10.5 and above. +## 词典包地址 +http://download.huzheng.org/zh_CN/index.html + +## 转换好的一些词典 +https://github.com/isee15/mac-dictionary-kit/releases/download/v2.2/Dictionaries.zip +解压到~/Library/Dictionaries + ## Download -Download it from [GitHub releases, v2.1](https://github.com/jjgod/mac-dictionary-kit/releases/download/v2.1/DictUnifier-2.1.zip). +Download it from [GitHub releases, v2.2](https://github.com/isee15/mac-dictionary-kit/releases/download/v2.2/DictUnifier.zip). ## Build instructions Install static build of glib before trying to build: +``` +brew update +brew install glib +// brew install glib --with-static +``` - brew install glib --with-static + ## TODO diff --git a/ddk/build_dict.sh b/ddk/build_dict.sh index da5a012..908d31d 100755 --- a/ddk/build_dict.sh +++ b/ddk/build_dict.sh @@ -107,7 +107,7 @@ echo "- Building $DICT_NAME.dictionary." # Check source XML. echo "- Checking source." -xmllint --stream -noout "$SRC_FILE" || error "Error." +# xmllint --stream -noout "$SRC_FILE" || error "Error." # Prepare directory. echo "- Cleaning objects directory." diff --git a/sdconv/convert.cpp b/sdconv/convert.cpp index bb59ee3..fc5e647 100644 --- a/sdconv/convert.cpp +++ b/sdconv/convert.cpp @@ -4,8 +4,6 @@ #include "dict.h" #include "convert.h" -#include "python.h" -#include "lua.h" #include "index.h" GString *mdk_start_convert(struct convert_module *mod) @@ -31,8 +29,6 @@ inline bool convert_with_glib(gchar *src, GString *dest) struct convert_module convert_module_list[] = { { "default", 0, NULL, convert_with_glib, NULL }, - { "python", 1, init_python, convert_with_python, fini_python }, - // { "lua", 1, init_lua, convert_with_lua, fini_lua }, { NULL, 0, NULL, NULL, NULL }, }; diff --git a/sdconv/lua.cpp b/sdconv/lua.cpp deleted file mode 100644 index a705ae9..0000000 --- a/sdconv/lua.cpp +++ /dev/null @@ -1,43 +0,0 @@ -// lua.cpp - -#if 0 -#include "lua.h" -#include - -lua_State *L; - -bool init_lua(const char *file) -{ - int ret; - - L = luaL_newstate(); - - luaL_openlibs(L); - ret = luaL_loadfile(L, file); - if (ret) - { - fprintf(stderr, "Couldn't load file: %s\n", lua_tostring(L, -1)); - return false; - } - - return true; -} - -void fini_lua() -{ - lua_close(L); -} - -bool convert_with_lua(gchar *src, GString *dest) -{ - lua_pushstring(L, src); - lua_setglobal(L, "input"); - - lua_pcall(L, 0, 1, 0); - g_string_append(dest, lua_tostring(L, -1)); - lua_pop(L, 1); - - return true; -} - -#endif \ No newline at end of file diff --git a/sdconv/lua.h b/sdconv/lua.h deleted file mode 100644 index b8b82b8..0000000 --- a/sdconv/lua.h +++ /dev/null @@ -1,13 +0,0 @@ -// lua.h - -#ifndef SDCONV_LUA_H -#define SDCONV_LUA_H - -#include - -bool init_lua(const char *file); -void fini_lua(); -bool convert_with_lua(gchar *src, GString *dest); - -#endif - diff --git a/sdconv/python.cpp b/sdconv/python.cpp deleted file mode 100644 index 4355a95..0000000 --- a/sdconv/python.cpp +++ /dev/null @@ -1,96 +0,0 @@ -// python.cpp - -#include -#include -#include "python.h" - -PyObject *py_transform_func; - -static char *copy_module_name_from_file(const char *file) -{ - char *module = strdup(basename((char *) file)); - if (! module) - return NULL; - - size_t len = strlen(module); - size_t i; - - for (i = len - 1; i >= 0; i--) - if (module[i] == '.') - module[i] = '\0'; - - return module; -} - -void add_directory_of_file_to_path(const char *file) -{ - PyRun_SimpleString("import sys"); - - char buf[256]; - snprintf(buf, sizeof(buf), "sys.path.insert(0, '%s')", - dirname((char *) file)); - - PyRun_SimpleString(buf); -} - -bool init_python(const char *file) -{ - PyObject *module; - - char *module_name = copy_module_name_from_file(file); - if (! module_name) - goto failed2; - - Py_Initialize(); - add_directory_of_file_to_path(file); - - fprintf(stderr, "loading module %s...\n", module_name); - module = PyImport_ImportModule(module_name); - if (! module) - goto failed1; - - fprintf(stderr, "locating function transform...\n"); - /* locate py_module.transform() */ - py_transform_func = PyObject_GetAttrString(module, - "transform"); - Py_DECREF(module); - if (! py_transform_func) - goto failed1; - - return true; - -failed1: - free(module_name); - -failed2: - return false; -} - -void fini_python() -{ - Py_DECREF(py_transform_func); - Py_Finalize(); -} - -bool convert_with_python(gchar *src, GString *dest) -{ - PyObject *pargs = Py_BuildValue("(s)", src); - PyObject *pstr = PyEval_CallObject(py_transform_func, pargs); - - if (pstr) - { - char *cstr = NULL; - PyArg_Parse(pstr, "s", &cstr); - - if (cstr) - g_string_append(dest, cstr); - - Py_DECREF(pstr); - } else - g_string_append(dest, "failed to transform\n"); - - Py_DECREF(pargs); - - return true; -} - diff --git a/sdconv/python.h b/sdconv/python.h deleted file mode 100644 index 3de6083..0000000 --- a/sdconv/python.h +++ /dev/null @@ -1,13 +0,0 @@ -// python.h - -#ifndef SDCONV_PYTHON_H -#define SDCONV_PYTHON_H - -#include - -bool init_python(const char *file); -void fini_python(); -bool convert_with_python(gchar *src, GString *dest); - -#endif -