code wiki / _hdl_build / nx_fb2_chapters_gate.nx

nx_fb2_chapters_gate.nx source

↩ module page · 78 lines · 5810 B

1// nx_fb2_chapters_gate.nx -- gate FB2 <section> chapter-splitting: a multi-section FictionBook -> real chapters + 2// navigable TOC (titles from each section's <title>), the body-level <title> front-matter SKIPPED (not a chapter), 3// each chapter file holding only its own section's prose. Completes text-format TOC consistency (EPUB/MOBI/KF8/FB2). 4// expect_exit: 0 license_tier: ORIGINAL 5import "nx_syscalls.nx" 6import "nx_gate_verdict.nx" 7 8func gp(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 9func gnum(v0: i64) -> i64 { var v: i64=v0; if v<0 { sys_write(1,"-" as *u8,1); v=0-v } let b: *u8=sys_mmap(24); var k: i64=0; if v==0 {b[0]=48 as u8;k=1} while v>0 {b[k]=(48+(v%10)) as u8; v=v/10; k=k+1} let o: *u8=sys_mmap(24); var j: i64=0; while j<k {o[j]=b[k-1-j];j=j+1} sys_write(1,o,k); return 0 } 10func ghas(hay: *u8, hl: i64, needle: *u8) -> i64 { 11 var nl: i64=0; while needle[nl]!=(0 as u8){nl=nl+1} 12 if nl == 0 { return 0 } 13 var i: i64 = 0 14 while i + nl <= hl { var k: i64=0; var hit: i64=1; while k<nl { if hay[i+k]!=needle[k]{hit=0;k=nl}else{k=k+1} } if hit==1 {return 1} i=i+1 } 15 return 0 16} 17func rd(path: *u8, lenp: *i64) -> *u8 { lenp[0]=0; return sys_read_file(path, lenp) } 18func spawn(path: *u8, a0: *u8, a1: *u8, redir: *u8) -> i64 { 19 let pid: i64 = sys_fork() 20 if pid == 0 { 21 let fd: i64 = sys_openat_wr(redir, 420) 22 if fd >= 0 { sys_dup3(fd, 1, 0); sys_dup3(fd, 2, 0) } 23 let argv: *i64 = sys_mmap(64) as *i64 24 var n: i64 = 0 25 argv[0] = path as i64; n = 1 26 if (a0 as i64) != 0 { argv[n] = a0 as i64; n = n + 1 } 27 if (a1 as i64) != 0 { argv[n] = a1 as i64; n = n + 1 } 28 argv[n] = 0 29 let envp: *i64 = sys_mmap(16) as *i64 30 envp[0] = "PATH=/usr/bin:/bin\x00" as *u8 as i64; envp[1] = 0 31 sys_execve(path, argv, envp) 32 sys_exit(127) 33 } 34 let st: *i64 = sys_mmap(16) as *i64 35 sys_wait4(pid, st, 0) 36 return (st[0] >> 8) & 0xff 37} 38 39func main() -> i64 { 40 gp("=== nx_fb2_chapters_gate: FB2 <section> -> real chapters + TOC (front-matter skipped) ===\n" as *u8) 41 let FB: *u8 = "_offc/nx_fb2_book.elf\x00" as *u8 42 let SC: *u8 = "knowledge/status/fb2_chapters_scratch.log\x00" as *u8 43 44 let rc: i64 = spawn(FB, "knowledge/fixtures/nishi_fb2_chapters.fb2\x00" as *u8, "nishi_fb2_chapters_fixture\x00" as *u8, SC) 45 46 let lj: *i64 = sys_mmap(8) as *i64; let bj: *u8 = rd("knowledge/staging/media/reader/nishi_fb2_chapters_fixture/book.json\x00" as *u8, lj); let bn: i64 = lj[0] 47 let l0: *i64 = sys_mmap(8) as *i64; let c0: *u8 = rd("knowledge/staging/media/reader/nishi_fb2_chapters_fixture/chap0.txt\x00" as *u8, l0); let n0: i64 = l0[0] 48 let l1: *i64 = sys_mmap(8) as *i64; let c1: *u8 = rd("knowledge/staging/media/reader/nishi_fb2_chapters_fixture/chap1.txt\x00" as *u8, l1); let n1: i64 = l1[0] 49 let l2: *i64 = sys_mmap(8) as *i64; let c2: *u8 = rd("knowledge/staging/media/reader/nishi_fb2_chapters_fixture/chap2.txt\x00" as *u8, l2); let n2: i64 = l2[0] 50 51 gp(" rc=" as *u8); gnum(rc); gp(" book.json=" as *u8); gnum(bn); gp("B chap0/1/2=" as *u8); gnum(n0); gp("/" as *u8); gnum(n1); gp("/" as *u8); gnum(n2); gp("\n" as *u8) 52 53 var pass: i64 = 0; var fail: i64 = 0 54 if rc == 0 { pass=pass+1 } else { fail=fail+1; gp(" FAIL decode-nonzero\n" as *u8) } 55 if (bj as i64)==0 { gp("FB2-CHAPTERS-GATE fail=1 verdict=RED no-book.json\n" as *u8); sys_exit(1); return 1 } 56 if ghas(bj, bn, "\"nchapters\":3" as *u8)==1 { pass=pass+1 } else { fail=fail+1; gp(" FAIL not 3 chapters\n" as *u8) } 57 if ghas(bj, bn, "\"ntoc\":3" as *u8)==1 { pass=pass+1 } else { fail=fail+1; gp(" FAIL not 3 TOC entries\n" as *u8) } 58 if ghas(bj, bn, "\"toc_source\":\"fb2-section\"" as *u8)==1 { pass=pass+1 } else { fail=fail+1; gp(" FAIL toc_source not fb2-section\n" as *u8) } 59 if ghas(bj, bn, "The Forest" as *u8)==1 { pass=pass+1 } else { fail=fail+1; gp(" FAIL no Forest <title>\n" as *u8) } 60 if ghas(bj, bn, "The Mountain" as *u8)==1 { pass=pass+1 } else { fail=fail+1; gp(" FAIL no Mountain <title>\n" as *u8) } 61 if (c0 as i64)!=0 { if ghas(c0, n0, "tall pines" as *u8)==1 { pass=pass+1 } else { fail=fail+1; gp(" FAIL ch0 wrong content\n" as *u8) } } else { fail=fail+1; gp(" FAIL no chap0\n" as *u8) } 62 if (c1 as i64)!=0 { if ghas(c1, n1, "otters" as *u8)==1 { pass=pass+1 } else { fail=fail+1; gp(" FAIL ch1 wrong content\n" as *u8) } } else { fail=fail+1; gp(" FAIL no chap1\n" as *u8) } 63 if (c2 as i64)!=0 { if ghas(c2, n2, "eagles" as *u8)==1 { pass=pass+1 } else { fail=fail+1; gp(" FAIL ch2 wrong content\n" as *u8) } } else { fail=fail+1; gp(" FAIL no chap2\n" as *u8) } 64 // LIAR-KILL: the body-level <title> front-matter is SKIPPED (not a chapter) + the split is real (ch1 != ch0 text) 65 if (c0 as i64)!=0 { if ghas(c0, n0, "Front Matter" as *u8)==0 { pass=pass+1 } else { fail=fail+1; gp(" FAIL front-matter leaked into a chapter\n" as *u8) } } 66 if (c1 as i64)!=0 { if ghas(c1, n1, "tall pines" as *u8)==0 { pass=pass+1 } else { fail=fail+1; gp(" FAIL ch1 contains ch0 text (split is fake)\n" as *u8) } } 67 68 gp("FB2-CHAPTERS-GATE pass=" as *u8); gnum(pass); gp(" fail=" as *u8); gnum(fail) 69 // MIGRATED onto nx_gate_verdict by nx_gate_dry_apply (D001, minimal form): every check 70 // row above is untouched, so the PASS/FAIL vector cannot change; only the hand-rolled 71 // verdict emission is replaced by the ONE shared base class. Proven by nx_gate_migrate verify. 72 let ctr__dry: *i64 = gv_ctr() 73 ctr__dry[0] = pass 74 ctr__dry[1] = pass + fail 75 let rc__dry: i64 = gv_verdict("FB2-CHAPTERS-GATE" as *u8, ctr__dry, "FB2 <section> -> 3 chapters w/ <title> titles, body-level front-matter skipped, real split)" as *u8) 76 sys_exit(rc__dry) 77 return rc__dry 78}