Skip to content

Commit 0100c31

Browse files
committed
get string out
1 parent 48daed7 commit 0100c31

11 files changed

+197
-62
lines changed

.gitmodules

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "CHMLib"]
2+
path = CHMLib
3+
url = https://github.com/iaalm/CHMLib.git

CHMLib

Submodule CHMLib added at 4383780

yCHM.xcodeproj/project.pbxproj

+60
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,18 @@
1515
F430AD802812C3E500B71C40 /* yCHM.xcdatamodeld in Sources */ = {isa = PBXBuildFile; fileRef = F430AD7E2812C3E500B71C40 /* yCHM.xcdatamodeld */; };
1616
F430AD8C2812C3E600B71C40 /* yCHMTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = F430AD8B2812C3E600B71C40 /* yCHMTests.swift */; };
1717
F430AD972812C3E600B71C40 /* yCHMUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = F430AD962812C3E600B71C40 /* yCHMUITests.swift */; };
18+
F430ADA92812CE2500B71C40 /* chm_lib.c in Sources */ = {isa = PBXBuildFile; fileRef = F430ADA72812CE2500B71C40 /* chm_lib.c */; };
19+
F430ADAA2812CE2500B71C40 /* chm_lib.c in Sources */ = {isa = PBXBuildFile; fileRef = F430ADA72812CE2500B71C40 /* chm_lib.c */; };
20+
F430ADAB2812CE2500B71C40 /* chm_lib.c in Sources */ = {isa = PBXBuildFile; fileRef = F430ADA72812CE2500B71C40 /* chm_lib.c */; };
21+
F430ADAE2812CE3D00B71C40 /* lzx.c in Sources */ = {isa = PBXBuildFile; fileRef = F430ADAC2812CE3C00B71C40 /* lzx.c */; };
22+
F430ADAF2812CE3D00B71C40 /* lzx.c in Sources */ = {isa = PBXBuildFile; fileRef = F430ADAC2812CE3C00B71C40 /* lzx.c */; };
23+
F430ADB02812CE3D00B71C40 /* lzx.c in Sources */ = {isa = PBXBuildFile; fileRef = F430ADAC2812CE3C00B71C40 /* lzx.c */; };
24+
F430ADB62812D4EC00B71C40 /* DocPicker.swift in Sources */ = {isa = PBXBuildFile; fileRef = F430ADB52812D4EC00B71C40 /* DocPicker.swift */; };
25+
F430ADB72812D4EC00B71C40 /* DocPicker.swift in Sources */ = {isa = PBXBuildFile; fileRef = F430ADB52812D4EC00B71C40 /* DocPicker.swift */; };
26+
F430ADB82812D4EC00B71C40 /* DocPicker.swift in Sources */ = {isa = PBXBuildFile; fileRef = F430ADB52812D4EC00B71C40 /* DocPicker.swift */; };
27+
F430ADBA2812D6FD00B71C40 /* CHMFile.swift in Sources */ = {isa = PBXBuildFile; fileRef = F430ADB92812D6FD00B71C40 /* CHMFile.swift */; };
28+
F430ADBB2812D6FD00B71C40 /* CHMFile.swift in Sources */ = {isa = PBXBuildFile; fileRef = F430ADB92812D6FD00B71C40 /* CHMFile.swift */; };
29+
F430ADBC2812D6FD00B71C40 /* CHMFile.swift in Sources */ = {isa = PBXBuildFile; fileRef = F430ADB92812D6FD00B71C40 /* CHMFile.swift */; };
1830
/* End PBXBuildFile section */
1931

2032
/* Begin PBXContainerItemProxy section */
@@ -50,6 +62,15 @@
5062
F430AD922812C3E600B71C40 /* yCHMUITests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = yCHMUITests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
5163
F430AD962812C3E600B71C40 /* yCHMUITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = yCHMUITests.swift; sourceTree = "<group>"; };
5264
F430AD982812C3E600B71C40 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
65+
F430ADA42812CE2500B71C40 /* yCHM-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "yCHM-Bridging-Header.h"; sourceTree = "<group>"; };
66+
F430ADA52812CE2500B71C40 /* yCHMTests-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "yCHMTests-Bridging-Header.h"; sourceTree = "<group>"; };
67+
F430ADA62812CE2500B71C40 /* yCHMUITests-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "yCHMUITests-Bridging-Header.h"; sourceTree = "<group>"; };
68+
F430ADA72812CE2500B71C40 /* chm_lib.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = chm_lib.c; path = CHMLib/src/chm_lib.c; sourceTree = SOURCE_ROOT; };
69+
F430ADA82812CE2500B71C40 /* chm_lib.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = chm_lib.h; path = CHMLib/src/chm_lib.h; sourceTree = SOURCE_ROOT; };
70+
F430ADAC2812CE3C00B71C40 /* lzx.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = lzx.c; path = CHMLib/src/lzx.c; sourceTree = SOURCE_ROOT; };
71+
F430ADAD2812CE3D00B71C40 /* lzx.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = lzx.h; path = CHMLib/src/lzx.h; sourceTree = SOURCE_ROOT; };
72+
F430ADB52812D4EC00B71C40 /* DocPicker.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DocPicker.swift; sourceTree = "<group>"; };
73+
F430ADB92812D6FD00B71C40 /* CHMFile.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CHMFile.swift; sourceTree = "<group>"; };
5374
/* End PBXFileReference section */
5475

5576
/* Begin PBXFrameworksBuildPhase section */
@@ -100,6 +121,10 @@
100121
F430AD722812C3DE00B71C40 /* yCHM */ = {
101122
isa = PBXGroup;
102123
children = (
124+
F430ADAC2812CE3C00B71C40 /* lzx.c */,
125+
F430ADAD2812CE3D00B71C40 /* lzx.h */,
126+
F430ADA72812CE2500B71C40 /* chm_lib.c */,
127+
F430ADA82812CE2500B71C40 /* chm_lib.h */,
103128
F430AD732812C3DE00B71C40 /* yCHMApp.swift */,
104129
F430AD752812C3DE00B71C40 /* ContentView.swift */,
105130
F430AD772812C3E500B71C40 /* Assets.xcassets */,
@@ -108,6 +133,11 @@
108133
F430AD822812C3E500B71C40 /* yCHM.entitlements */,
109134
F430AD7E2812C3E500B71C40 /* yCHM.xcdatamodeld */,
110135
F430AD792812C3E500B71C40 /* Preview Content */,
136+
F430ADA42812CE2500B71C40 /* yCHM-Bridging-Header.h */,
137+
F430ADA52812CE2500B71C40 /* yCHMTests-Bridging-Header.h */,
138+
F430ADA62812CE2500B71C40 /* yCHMUITests-Bridging-Header.h */,
139+
F430ADB52812D4EC00B71C40 /* DocPicker.swift */,
140+
F430ADB92812D6FD00B71C40 /* CHMFile.swift */,
111141
);
112142
path = yCHM;
113143
sourceTree = "<group>";
@@ -205,13 +235,16 @@
205235
TargetAttributes = {
206236
F430AD6F2812C3DE00B71C40 = {
207237
CreatedOnToolsVersion = 12.5.1;
238+
LastSwiftMigration = 1250;
208239
};
209240
F430AD862812C3E600B71C40 = {
210241
CreatedOnToolsVersion = 12.5.1;
242+
LastSwiftMigration = 1250;
211243
TestTargetID = F430AD6F2812C3DE00B71C40;
212244
};
213245
F430AD912812C3E600B71C40 = {
214246
CreatedOnToolsVersion = 12.5.1;
247+
LastSwiftMigration = 1250;
215248
TestTargetID = F430AD6F2812C3DE00B71C40;
216249
};
217250
};
@@ -267,8 +300,12 @@
267300
isa = PBXSourcesBuildPhase;
268301
buildActionMask = 2147483647;
269302
files = (
303+
F430ADBA2812D6FD00B71C40 /* CHMFile.swift in Sources */,
304+
F430ADAE2812CE3D00B71C40 /* lzx.c in Sources */,
270305
F430AD7D2812C3E500B71C40 /* Persistence.swift in Sources */,
306+
F430ADA92812CE2500B71C40 /* chm_lib.c in Sources */,
271307
F430AD762812C3DE00B71C40 /* ContentView.swift in Sources */,
308+
F430ADB62812D4EC00B71C40 /* DocPicker.swift in Sources */,
272309
F430AD802812C3E500B71C40 /* yCHM.xcdatamodeld in Sources */,
273310
F430AD742812C3DE00B71C40 /* yCHMApp.swift in Sources */,
274311
);
@@ -278,6 +315,10 @@
278315
isa = PBXSourcesBuildPhase;
279316
buildActionMask = 2147483647;
280317
files = (
318+
F430ADAF2812CE3D00B71C40 /* lzx.c in Sources */,
319+
F430ADAA2812CE2500B71C40 /* chm_lib.c in Sources */,
320+
F430ADBB2812D6FD00B71C40 /* CHMFile.swift in Sources */,
321+
F430ADB72812D4EC00B71C40 /* DocPicker.swift in Sources */,
281322
F430AD8C2812C3E600B71C40 /* yCHMTests.swift in Sources */,
282323
);
283324
runOnlyForDeploymentPostprocessing = 0;
@@ -286,6 +327,10 @@
286327
isa = PBXSourcesBuildPhase;
287328
buildActionMask = 2147483647;
288329
files = (
330+
F430ADB02812CE3D00B71C40 /* lzx.c in Sources */,
331+
F430ADAB2812CE2500B71C40 /* chm_lib.c in Sources */,
332+
F430ADBC2812D6FD00B71C40 /* CHMFile.swift in Sources */,
333+
F430ADB82812D4EC00B71C40 /* DocPicker.swift in Sources */,
289334
F430AD972812C3E600B71C40 /* yCHMUITests.swift in Sources */,
290335
);
291336
runOnlyForDeploymentPostprocessing = 0;
@@ -426,6 +471,7 @@
426471
buildSettings = {
427472
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
428473
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
474+
CLANG_ENABLE_MODULES = YES;
429475
CODE_SIGN_ENTITLEMENTS = yCHM/yCHM.entitlements;
430476
CODE_SIGN_STYLE = Automatic;
431477
COMBINE_HIDPI_IMAGES = YES;
@@ -441,6 +487,8 @@
441487
MACOSX_DEPLOYMENT_TARGET = 11.0;
442488
PRODUCT_BUNDLE_IDENTIFIER = cn.iaalm.yCHM;
443489
PRODUCT_NAME = "$(TARGET_NAME)";
490+
SWIFT_OBJC_BRIDGING_HEADER = "yCHM/yCHM-Bridging-Header.h";
491+
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
444492
SWIFT_VERSION = 5.0;
445493
};
446494
name = Debug;
@@ -450,6 +498,7 @@
450498
buildSettings = {
451499
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
452500
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
501+
CLANG_ENABLE_MODULES = YES;
453502
CODE_SIGN_ENTITLEMENTS = yCHM/yCHM.entitlements;
454503
CODE_SIGN_STYLE = Automatic;
455504
COMBINE_HIDPI_IMAGES = YES;
@@ -465,6 +514,7 @@
465514
MACOSX_DEPLOYMENT_TARGET = 11.0;
466515
PRODUCT_BUNDLE_IDENTIFIER = cn.iaalm.yCHM;
467516
PRODUCT_NAME = "$(TARGET_NAME)";
517+
SWIFT_OBJC_BRIDGING_HEADER = "yCHM/yCHM-Bridging-Header.h";
468518
SWIFT_VERSION = 5.0;
469519
};
470520
name = Release;
@@ -474,6 +524,7 @@
474524
buildSettings = {
475525
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
476526
BUNDLE_LOADER = "$(TEST_HOST)";
527+
CLANG_ENABLE_MODULES = YES;
477528
CODE_SIGN_STYLE = Automatic;
478529
COMBINE_HIDPI_IMAGES = YES;
479530
DEVELOPMENT_TEAM = GF89F8J3K7;
@@ -486,6 +537,8 @@
486537
MACOSX_DEPLOYMENT_TARGET = 11.0;
487538
PRODUCT_BUNDLE_IDENTIFIER = cn.iaalm.yCHMTests;
488539
PRODUCT_NAME = "$(TARGET_NAME)";
540+
SWIFT_OBJC_BRIDGING_HEADER = "yCHM/yCHMTests-Bridging-Header.h";
541+
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
489542
SWIFT_VERSION = 5.0;
490543
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/yCHM.app/Contents/MacOS/yCHM";
491544
};
@@ -496,6 +549,7 @@
496549
buildSettings = {
497550
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
498551
BUNDLE_LOADER = "$(TEST_HOST)";
552+
CLANG_ENABLE_MODULES = YES;
499553
CODE_SIGN_STYLE = Automatic;
500554
COMBINE_HIDPI_IMAGES = YES;
501555
DEVELOPMENT_TEAM = GF89F8J3K7;
@@ -508,6 +562,7 @@
508562
MACOSX_DEPLOYMENT_TARGET = 11.0;
509563
PRODUCT_BUNDLE_IDENTIFIER = cn.iaalm.yCHMTests;
510564
PRODUCT_NAME = "$(TARGET_NAME)";
565+
SWIFT_OBJC_BRIDGING_HEADER = "yCHM/yCHMTests-Bridging-Header.h";
511566
SWIFT_VERSION = 5.0;
512567
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/yCHM.app/Contents/MacOS/yCHM";
513568
};
@@ -517,6 +572,7 @@
517572
isa = XCBuildConfiguration;
518573
buildSettings = {
519574
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
575+
CLANG_ENABLE_MODULES = YES;
520576
CODE_SIGN_STYLE = Automatic;
521577
COMBINE_HIDPI_IMAGES = YES;
522578
DEVELOPMENT_TEAM = GF89F8J3K7;
@@ -528,6 +584,8 @@
528584
);
529585
PRODUCT_BUNDLE_IDENTIFIER = cn.iaalm.yCHMUITests;
530586
PRODUCT_NAME = "$(TARGET_NAME)";
587+
SWIFT_OBJC_BRIDGING_HEADER = "yCHM/yCHMUITests-Bridging-Header.h";
588+
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
531589
SWIFT_VERSION = 5.0;
532590
TEST_TARGET_NAME = yCHM;
533591
};
@@ -537,6 +595,7 @@
537595
isa = XCBuildConfiguration;
538596
buildSettings = {
539597
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
598+
CLANG_ENABLE_MODULES = YES;
540599
CODE_SIGN_STYLE = Automatic;
541600
COMBINE_HIDPI_IMAGES = YES;
542601
DEVELOPMENT_TEAM = GF89F8J3K7;
@@ -548,6 +607,7 @@
548607
);
549608
PRODUCT_BUNDLE_IDENTIFIER = cn.iaalm.yCHMUITests;
550609
PRODUCT_NAME = "$(TARGET_NAME)";
610+
SWIFT_OBJC_BRIDGING_HEADER = "yCHM/yCHMUITests-Bridging-Header.h";
551611
SWIFT_VERSION = 5.0;
552612
TEST_TARGET_NAME = yCHM;
553613
};

yCHM/CHMFile.swift

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//
2+
// CHMFile.swift
3+
// yCHM
4+
//
5+
// Created by simon xu on 4/22/22.
6+
//
7+
8+
import Foundation
9+
10+
class CHMFile {
11+
let fd: OpaquePointer
12+
13+
init(filename: String) {
14+
fd = chm_open(filename)
15+
}
16+
17+
deinit {
18+
19+
}
20+
21+
func first() -> String {
22+
let buf = UnsafeMutablePointer<UnsafeMutablePointer<UInt8>>.allocate(capacity: 1)
23+
let type = CHM_ENUMERATE_FILES + CHM_ENUMERATE_NORMAL
24+
chm_enumerate(fd, type, {(file, item, p) in
25+
let pres = p!.assumingMemoryBound(to: UnsafeMutablePointer<UInt8>.self)
26+
let len = item!.pointee.length
27+
let buf = UnsafeMutablePointer<UInt8>.allocate(capacity: Int(len))
28+
chm_retrieve_object(file, item, buf, 0, LONGINT64(len))
29+
pres.pointee = buf
30+
31+
return CHM_ENUMERATOR_SUCCESS
32+
}, buf)
33+
34+
let res = String(cString: buf.pointee)
35+
buf.pointee.deallocate()
36+
return res
37+
}
38+
}
39+
40+

yCHM/ContentView.swift

+39-56
Original file line numberDiff line numberDiff line change
@@ -6,71 +6,54 @@
66
//
77

88
import SwiftUI
9+
import WebKit
910
import CoreData
1011

1112
struct ContentView: View {
12-
@Environment(\.managedObjectContext) private var viewContext
13-
14-
@FetchRequest(
15-
sortDescriptors: [NSSortDescriptor(keyPath: \Item.timestamp, ascending: true)],
16-
animation: .default)
17-
private var items: FetchedResults<Item>
18-
13+
@State var page: CHMPage
14+
15+
let docPicker = DocPicker()
16+
1917
var body: some View {
20-
List {
21-
ForEach(items) { item in
22-
Text("Item at \(item.timestamp!, formatter: itemFormatter)")
23-
}
24-
.onDelete(perform: deleteItems)
25-
}
26-
.toolbar {
27-
Button(action: addItem) {
28-
Label("Add Item", systemImage: "plus")
29-
}
30-
}
31-
}
32-
33-
private func addItem() {
34-
withAnimation {
35-
let newItem = Item(context: viewContext)
36-
newItem.timestamp = Date()
37-
38-
do {
39-
try viewContext.save()
40-
} catch {
41-
// Replace this implementation with code to handle the error appropriately.
42-
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
43-
let nsError = error as NSError
44-
fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
45-
}
46-
}
47-
}
48-
49-
private func deleteItems(offsets: IndexSet) {
50-
withAnimation {
51-
offsets.map { items[$0] }.forEach(viewContext.delete)
52-
53-
do {
54-
try viewContext.save()
55-
} catch {
56-
// Replace this implementation with code to handle the error appropriately.
57-
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
58-
let nsError = error as NSError
59-
fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
60-
}
18+
VStack
19+
{
20+
Button(action: {() in
21+
let filename = docPicker.display()!
22+
let update = CHMFile(filename: filename).first()
23+
print("prepared")
24+
DispatchQueue.main.async {
25+
print(update)
26+
print("update")
27+
self.page.html = update
28+
}
29+
}, label: {
30+
/*@START_MENU_TOKEN@*/Text("Button")/*@END_MENU_TOKEN@*/
31+
})
32+
WebView(text: $page.html)
33+
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
6134
}
6235
}
6336
}
6437

65-
private let itemFormatter: DateFormatter = {
66-
let formatter = DateFormatter()
67-
formatter.dateStyle = .short
68-
formatter.timeStyle = .medium
69-
return formatter
70-
}()
71-
7238
struct ContentView_Previews: PreviewProvider {
7339
static var previews: some View {
74-
ContentView().environment(\.managedObjectContext, PersistenceController.preview.container.viewContext)
40+
ContentView(page: CHMPage(html: "a<b>b</b>"))
7541
}
7642
}
43+
44+
45+
struct WebView: NSViewRepresentable {
46+
@Binding var text: String
47+
48+
func makeNSView(context: Context) -> WKWebView {
49+
return WKWebView()
50+
}
51+
52+
func updateNSView(_ uiView: WKWebView, context: Context) {
53+
uiView.loadHTMLString(text, baseURL: nil)
54+
}
55+
}
56+
57+
struct CHMPage {
58+
var html: String
59+
}

0 commit comments

Comments
 (0)