int main() { // init FILE* fp = NULL; fp = malloc(sizeof(FILE)); char text1[] = "Jack and Jill went up the hill to fetch a pail of water\n"; char text2[] = "Jack fell down and broke his crown\nAnd Jill came tumbling after.\n"; char text3[] = "Up got Jack, and home did trot\nAs fast as he could caper\n"; char text4[] = "He went to bed and bound his head\nWith vinegar and brown paper.\n"; unsigned long int text1_size = sizeof(text1); unsigned long int text2_size = sizeof(text2); unsigned long int text3_size = sizeof(text3); unsigned long int text4_size = sizeof(text4); unsigned long int content_size = 2 * BUFSIZ; char *content = malloc(content_size*sizeof(char)); strcpy(content,""); char szTmp[] = "tmp.txt"; // create FILE* printf("%i - Create temp file pointer (read/write): %s\n", create_tmp(&fp, szTmp), szTmp); // write the lines printf("%i - Append some text to the \'%s\' (linewise)\n", write_linewise(fp, text1, text1_size), szTmp); printf("%i - Append some text to the \'%s\' (linewise)\n", write_linewise(fp, text2, text2_size), szTmp); printf("%i - Append some text to the \'%s\' (linewise)\n", write_linewise(fp, text3, text3_size), szTmp); printf("%i - Append some text to the \'%s\' (linewise)\n", write_linewise(fp, text4, text4_size), szTmp); // read the nth line printf("%i - Read the %i. line (first line index: 1) of \'\%s\'\n", read_nth_line(fp, content, content_size, 2), 2, szTmp); // FIXME printf("content:\n\'%s\'\n", content); strcpy(content,""); // close the tmp file printf("%i - And close the \'%s\' file again\n", close_tmp(&fp), szTmp); printf("Done.\n\n"); //*/ return EXIT_SUCCESS; };
i64 psi12_makeindex(CSA *csa, char *fname) { i64 psize,psize1,psize2; i64 b, b2; i64 i,j,x,xx; i64 y,d,w; int k; FILE *f1,*f2; char *fpsi, *fpsd; //psi1_iterator *pi; i64 runlen; i64 maxrun; i64 n,L; psi1 *ps; int id,id2; FILE *out; diskbuf *psi; char *fbw, *flst; SPARSEARRAY sx, sb; int mm; ps = (psi1 *)csa->psi_struc; id = ps->id; id2 = id & 0x3f; k = strlen(fname); fbw = mymalloc(k+5); flst = mymalloc(k+5); fpsi = mymalloc(k+5); fpsd = mymalloc(k+5); sprintf(fbw,"%s.bw",fname); sprintf(flst,"%s.lst",fname); switch (id2) { case ID_DIFF_GAMMA_RR: sprintf(fpsi,"%s.pxi",fname); sprintf(fpsd,"%s.pxd",fname); break; } out = create_tmp(0); bw_to_psi(out,csa,fbw,flst,&k); psi = open_diskbuf(out,k); ps->last = getint_diskbuf(psi,0); printf("last = %ld\n",ps->last); n = csa->n; L = ps->L; if (L >= n) { printf("L=%ld >= n=%ld\n",L,n); exit(0); } maxrun = L; mkdecodetable(); f1 = fopen(fpsi,"wb"); psize1 = 0; f2 = fopen(fpsd,"wb"); psize2 = 0; ps->k = k = (blog(n+1)+1+8-1)/8; // pi = psi1_iterator_new(ps,0); writeint(1,ID_PSI,f2); writeint(1,k,f2); /* #bytes of integer */ writeint(k,n,f2); writeint(k,L,f2); psize2 += 1+1+2*k; writeint(1,id,f2); psize2 += 1; if (id & ID_COMPPTR) { mm = 0; for (i=0; i<SIGMA; i++) { if (csa->C[i] > 0) mm++; } SPARSEARRAY_construct_init(&sx, (mm+1)*(n+1), n/L+1); SPARSEARRAY_construct_init(&sb, n, n/L+1); } b = b2 = 0; mm = 0; xx = 0; for (j=0; j<=n/L; j++) { // display_progressbar("writing psi ",j,n/L); if (j % 100000 == 0) { printf("%ld %1.3f bpc\r",j,(double)psize2*8/(j+1)/L); fflush(stdout); } y = getint_diskbuf(psi,j*L); if (id & ID_COMPPTR) { if (y <= xx) { mm++; } SPARSEARRAY_construct_set(&sx, j, mm*(n+1) + y); SPARSEARRAY_construct_set(&sb, j, b); xx = y; } else { // printf("%ld x=%ld sp=%ld\n",j,y,b); writeint(k,y,f2); writeint(k,b,f2); psize2 += 2*k; } x = y; runlen = 0; b2 = 0; for (i=j*L+1; i<(j+1)*L && i <= n; i++) { /* psi[j*L] are not encoded */ y = getint_diskbuf(psi,i); d = y - x; if (d <= 0) { d += n+1; } if (i == j*L+1) { if (d == 1) {setbit(Btmp,b2+1,1); runlen = 1;} else {setbit(Btmp,b2+1,0); runlen = 0;} b2++; } if (d > 1) { if (runlen>0) { // printf("d=%ld encode runlen=%ld\n",d,runlen); w = ENCODENUM(Btmp,b2,runlen); b2 += w; runlen = 0; } // printf("encode d-1=%ld\n",d-1); w = ENCODENUM(Btmp,b2,d-1); b2 += w; runlen = 1; } else { runlen++; } x = y; } if (runlen>0) { // printf("encode runlen=%ld\n",runlen); w = ENCODENUM(Btmp,b2,runlen); b2 += w; runlen = 0; } fwrite(Btmp,(b2+15) / 16,sizeof(short),f1); psize1 += (b2+15)/16*sizeof(short); b += (b2+15) / 16; b2 = 0; } if (b2 > 0) { fwrite(Btmp,(b2+15) / 16,sizeof(short),f1); psize1 += (b2+15)/16*sizeof(short); } fwrite(Btmp,1,sizeof(short),f1); // getbitDで1ワード余計に読むため psize1 += 1*sizeof(short); if (id & ID_COMPPTR) { SPARSEARRAY_construct_end(&sx, SDARRAY_SELECT1); SPARSEARRAY_construct_end(&sb, SDARRAY_SELECT1); SPARSEARRAY_write(&sx, f2); SPARSEARRAY_write(&sb, f2); } psize = psize1 + psize2; printf("size %ld (%1.3f bpc)\n",psize,(double)psize*8 / n); fclose(f1); fclose(f2); // psi1_iterator_remove(pi); close_diskbuf(psi); fclose(out); remove_tmp(0); psi1_read(csa, fpsd); free(fpsi); free(fpsd); free(fbw); free(flst); return psize; }
i64 psi2_makeindex(CSA *csa, char *fname) { i64 psize; i64 b, b2; i64 i,j,x; i64 y,d,w; int k; FILE *f1,*f2; char *fpsi; //psi1_iterator *pi; i64 n,m; i64 nn, mm; psi2 *ps; int id; FILE *out; diskbuf *psi; char *fbw, *flst; sparsearray4 sa; ps = (psi2 *)csa->psi_struc; id = csa->id; k = strlen(fname); fbw = (char *) mymalloc(k+5); flst = (char *) mymalloc(k+5); fpsi = (char *) mymalloc(k+5); sprintf(fbw,"%s.bw",fname); sprintf(flst,"%s.lst",fname); sprintf(fpsi,"%s.psa",fname); out = create_tmp(0); bw_to_psi(out,csa,fbw,flst,&k); m = 0; for (i=0; i<SIGMA; i++) { if (csa->C[i] > 0) m++; } psi = open_diskbuf(out,k); ps->last = getint_diskbuf(psi,0); printf("last = %ld\n",ps->last); n = csa->n; mm = n+1; nn = (m+1) * (n+1); sparsearray4_construct_init(&sa, nn, mm); y = 0; d = 0; for (j=0; j<=n; j++) { display_progressbar("compressing psi ",j,n); x = getint_diskbuf(psi,j); if (x <= y) d++; sparsearray4_construct_set(&sa, j, d*(n+1)+x); y = x; } sparsearray4_construct_end(&sa,0, SDARRAY_SELECT1); f2 = fopen(fpsi,"wb"); psize = 0; ps->k = k = (blog(n+1)+1+8-1)/8; writeint(1,ID_PSI,f2); writeint(1,k,f2); /* #bytes of integer */ writeint(k,n,f2); psize += 1+1+k; writeint(1,id,f2); psize += 1; psize += sparsearray4_write(&sa, f2); printf("size %ld (%1.3f bpc)\n",psize,(double)psize*8 / n); fclose(f2); // psi1_iterator_remove(pi); close_diskbuf(psi); fclose(out); remove_tmp(0); psi2_read(csa, fpsi); free(fpsi); free(fbw); free(flst); return psize; }