nx_profile_region_map_candidate_t366.nx source
↩ module page · 87 lines · 4466 B
1// Select an explicit source profile part without changing measurements or coordinate frame.
2// Preserves source comments, V/N contract and Q rows affecting selected part.
3// This is selection, not anatomical validation or conversion between specimen cohorts.
4import "nx_syscalls.nx"
5import "nx_profile_contract.nx"
6func pe_int(b:*u8,p:*i64,e:i64)->i64{
7 while p[0]<e{if b[p[0]]>(32 as u8){break}p[0]=p[0]+1}
8 var n:i64=0;var seen:i64=0
9 while p[0]<e{let c:i64=b[p[0]] as i64;if c<48{break}if c>57{break}
10 if n>922337203685477580{return 0-1}if n==922337203685477580{if c>55{return 0-1}}
11 n=n*10+c-48;seen=1;p[0]=p[0]+1}
12 if seen==0{return 0-1}return n
13}
14func pe_legacy_main(argc:i64,argv:*i64)->i64{
15 if argc!=4{return 2}let size:*i64=sys_mmap(16) as *i64
16 let b:*u8=sys_read_file(argv[1] as *u8,size);if (b as i64)==0{return 3}
17 let info:*i64=sys_mmap(40) as *i64;let status:i64=profile_contract_scan(b,size[0],info);if status<0{return 4}
18 let p:*i64=sys_mmap(8) as *i64;let arg:*u8=argv[3] as *u8;var al:i64=0;while arg[al]!=(0 as u8){al=al+1}
19 p[0]=0;let part:i64=pe_int(arg,p,al);if part<0{return 2}if p[0]!=al{return 2}
20 let out:*u8=sys_mmap(size[0]+1);var used:i64=0;var rows:i64=0;var i:i64=0
21 while i<size[0]{
22 var e:i64=i;while e<size[0]{if b[e]==(10 as u8){break}e=e+1}
23 var j:i64=i;while j<e{if b[j]>(32 as u8){break}j=j+1}
24 var keep:i64=1
25 if j<e{
26 if b[j]==(83 as u8){p[0]=j+1;let id:i64=pe_int(b,p,e);if id<0{return 4}keep=0;if id==part{keep=1;rows=rows+1}}
27 if b[j]==(81 as u8){p[0]=j+1;pe_int(b,p,e);let a:i64=pe_int(b,p,e);let c:i64=pe_int(b,p,e);keep=0;if a==part{keep=1}if c==part{keep=1}}
28 }
29 if keep==1{var k:i64=i;while k<e{out[used]=b[k];used=used+1;k=k+1}out[used]=10 as u8;used=used+1}
30 i=e+1
31 }
32 if rows==0{return 5}
33 let fd:i64=sys_openat_wr(argv[2] as *u8,420);if fd<0{return 6}
34 let wrote:i64=sys_write(fd,out,used);sys_close(fd);if wrote!=used{return 6}
35 return 0
36}
37
38import "nx_buf_dyn.nx"
39import "nx_itoa_lib.nx"
40import "nx_atomic_rewrite.nx"
41// Explicit caller-declared ordinal correspondence. Semantic/source-recipe binding belongs
42// to the calling recipe receipt; this operation does not infer anatomical correspondence.
43func pem_main(argc:i64,argv:*i64)->i64{
44 if argc!=5{return 2}
45 var n:i64=0;let b:*u8=sys_read_file(argv[1] as *u8,&n);if (b as i64)<=0{return 3}
46 let info:*i64=sys_mmap_try(5*__size_of(i64)) as *i64
47 var p:i64=0;var al:i64=0;let src:*u8=argv[3] as *u8;while src[al]!=(0 as u8){al=al+1}
48 let source:i64=pe_int(src,&p,al);if source<0||p!=al{return 2}
49 p=0;al=0;let dst:*u8=argv[4] as *u8;while dst[al]!=(0 as u8){al=al+1}
50 let target:i64=pe_int(dst,&p,al);if target<0||p!=al{return 2}
51 if (info as i64)<=0{return 6}
52 if profile_contract_scan(b,n,info)<0{return 4}
53 let out:*NxBufOwned=sys_mmap_try(__size_of(NxBufOwned)) as *NxBufOwned
54 let tmp:*u8=sys_mmap_try(NXI_BUF)
55 if (out as i64)<=0||(tmp as i64)<=0{return 6}
56 out.buf=0 as *u8;out.len=0;out.cap=0
57 var rc:i64=0;var rows:i64=0;var i:i64=0
58 while i<n{
59 var e:i64=i;while e<n{if b[e]==(10 as u8){break};e=e+1}
60 var j:i64=i;while j<e{if b[j]>(32 as u8){break};j=j+1}
61 var keep:i64=1;var replace_start:i64=-1;var replace_end:i64=-1
62 if j<e{
63 if b[j]==(83 as u8){
64 p=j+1;while p<e{if b[p]>(32 as u8){break};p=p+1};replace_start=p
65 let id:i64=pe_int(b,&p,e);if id<0{rc=-1;break};replace_end=p
66 keep=0;if id==source{keep=1;rows=rows+1}
67 }
68 if b[j]==(81 as u8){
69 p=j+1;pe_int(b,&p,e);let a:i64=pe_int(b,&p,e);let c:i64=pe_int(b,&p,e)
70 // Applicable ambiguity is never silently removed or relabelled as qualified.
71 if a==source||c==source{rc=-2;break};keep=0
72 }
73 }
74 if keep==1{
75 if replace_start>=0{
76 rc=nx_bo_append(out,(b as i64+i) as *u8,replace_start-i,0);if rc<0{break}
77 let digits:i64=nxi_buf(tmp,0,target);rc=nx_bo_append(out,tmp,digits,0);if rc<0{break}
78 rc=nx_bo_append(out,(b as i64+replace_end) as *u8,e-replace_end,0)
79 }else{rc=nx_bo_append(out,(b as i64+i) as *u8,e-i,0)}
80 if rc<0{break};tmp[0]=10 as u8;rc=nx_bo_append(out,tmp,1,0);if rc<0{break}
81 };i=e+1
82 }
83 if rc>=0{if rows==0{rc=-3}else{rc=atomic_rewrite_checked(argv[2] as *u8,out.buf,out.len)}}
84 let rel:i64=nx_bo_release(out);sys_munmap_direct(out as *u8,__size_of(NxBufOwned));sys_munmap_direct(tmp,NXI_BUF);sys_munmap_direct(info as *u8,5*__size_of(i64));sys_free_file(b,n)
85 if rc<0{return 7};if rel<0{return 8};return 0
86}
87func main(argc:i64,argv:*i64)->i64{if argc==4{return pe_legacy_main(argc,argv)};return pem_main(argc,argv)}