code wiki / _hdl_build / nx_fb2_chapters_gate.nx

nx_fb2_chapters_gate.nx source

↩ module page · 70 lines · 5419 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" 6 7func gp(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 8func 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 } 9func ghas(hay: *u8, hl: i64, needle: *u8) -> i64 { 10 var nl: i64=0; while needle[nl]!=(0 as u8){nl=nl+1} 11 if nl == 0 { return 0 } 12 var i: i64 = 0 13 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 } 14 return 0 15} 16func rd(path: *u8, lenp: *i64) -> *u8 { lenp[0]=0; return sys_read_file(path, lenp) } 17func spawn(path: *u8, a0: *u8, a1: *u8, redir: *u8) -> i64 { 18 let pid: i64 = sys_fork() 19 if pid == 0 { 20 let fd: i64 = sys_openat_wr(redir, 420) 21 if fd >= 0 { sys_dup3(fd, 1, 0); sys_dup3(fd, 2, 0) } 22 let argv: *i64 = sys_mmap(64) as *i64 23 var n: i64 = 0 24 argv[0] = path as i64; n = 1 25 if (a0 as i64) != 0 { argv[n] = a0 as i64; n = n + 1 } 26 if (a1 as i64) != 0 { argv[n] = a1 as i64; n = n + 1 } 27 argv[n] = 0 28 let envp: *i64 = sys_mmap(16) as *i64 29 envp[0] = "PATH=/usr/bin:/bin\x00" as *u8 as i64; envp[1] = 0 30 sys_execve(path, argv, envp) 31 sys_exit(127) 32 } 33 let st: *i64 = sys_mmap(16) as *i64 34 sys_wait4(pid, st, 0) 35 return (st[0] >> 8) & 0xff 36} 37 38func main() -> i64 { 39 gp("=== nx_fb2_chapters_gate: FB2 <section> -> real chapters + TOC (front-matter skipped) ===\n" as *u8) 40 let FB: *u8 = "_offc/nx_fb2_book.elf\x00" as *u8 41 let SC: *u8 = "knowledge/status/fb2_chapters_scratch.log\x00" as *u8 42 43 let rc: i64 = spawn(FB, "knowledge/fixtures/nishi_fb2_chapters.fb2\x00" as *u8, "nishi_fb2_chapters_fixture\x00" as *u8, SC) 44 45 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] 46 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] 47 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] 48 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] 49 50 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) 51 52 var pass: i64 = 0; var fail: i64 = 0 53 if rc == 0 { pass=pass+1 } else { fail=fail+1; gp(" FAIL decode-nonzero\n" as *u8) } 54 if (bj as i64)==0 { gp("FB2-CHAPTERS-GATE fail=1 verdict=RED no-book.json\n" as *u8); sys_exit(1); return 1 } 55 if ghas(bj, bn, "\"nchapters\":3" as *u8)==1 { pass=pass+1 } else { fail=fail+1; gp(" FAIL not 3 chapters\n" as *u8) } 56 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) } 57 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) } 58 if ghas(bj, bn, "The Forest" as *u8)==1 { pass=pass+1 } else { fail=fail+1; gp(" FAIL no Forest <title>\n" as *u8) } 59 if ghas(bj, bn, "The Mountain" as *u8)==1 { pass=pass+1 } else { fail=fail+1; gp(" FAIL no Mountain <title>\n" as *u8) } 60 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) } 61 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) } 62 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) } 63 // LIAR-KILL: the body-level <title> front-matter is SKIPPED (not a chapter) + the split is real (ch1 != ch0 text) 64 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) } } 65 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) } } 66 67 gp("FB2-CHAPTERS-GATE pass=" as *u8); gnum(pass); gp(" fail=" as *u8); gnum(fail) 68 if fail == 0 { gp(" verdict=GREEN (FB2 <section> -> 3 chapters w/ <title> titles, body-level front-matter skipped, real split)\n" as *u8); sys_exit(0); return 0 } 69 gp(" verdict=RED\n" as *u8); sys_exit(1); return 1 70}