예제 #1
0
size_t uread(void *buffer, size_t count, UFILE *rp)
{
	switch( rp->type ) {
	case UFT_HTTP:
		return hread(buffer,count,rp->handle);
		break;
	case UFT_FILE:
		return fread(buffer,1,count,rp->handle);
		break;
	default:
		return -1;
		break;
	}
}
예제 #2
0
파일: puff.c 프로젝트: ilyarudyak/cs50_h
/**
 * Open file  and check if it is Huffile. 
 * Return NULL if not Huffile or other mistake, else return Huffile *.
 */
bool isHuffile(char *path) {
    
    // open input
    Huffile* input = hfopen(path, "r");
    if (input == NULL)
    {
        printf("Could not open %s for reading.\n", path);
        return 0;
    }
    
    // read in header
    Huffeader header;
    if (hread(&header, input) == false)
    {
        hfclose(input);
        printf("Could not read header.\n");
        return 0;
    }

    // check for magic number
    if (header.magic != MAGIC)
    {
        hfclose(input);
        printf("File was not huffed.\n");
        return 0;
    }

    // check checksum
    int checksum = header.checksum;
    for (int i = 0; i < SYMBOLS; i++)
    {
        checksum -= header.frequencies[i];
    }
    if (checksum != 0)
    {
        hfclose(input);
        printf("File was not huffed.\n");
        return 0;
    }
    
    
    
    // close input
    hfclose(input);
    
    return true;   
}
예제 #3
0
int32_t init_ramcoder(struct ramcoder *coder,HUFF *hp,bits256 *seed)
{
    int32_t i,precision,numbits = 0;
    if ( coder->lastsymbol == 0 )
        coder->lastsymbol = 0x100, coder->upper_lastsymbol = (coder->lastsymbol + 1);
    coder->cumulativeProb = coder->lower = coder->code = coder->underflowBits = coder->ranges[0] = 0;
    for (i=1; i<=coder->upper_lastsymbol; i++)
    {
        coder->ranges[i] = coder->ranges[i - 1] + 1 + 10*((i <= 0x100) ? (GETBIT(seed->bytes,i-1) != 0) : 0);
        //printf("%d ",coder->ranges[i]);
    }
    for (i=1; i<=coder->upper_lastsymbol; i++)
        coder->cumulativeProb += (coder->ranges[i] - coder->ranges[i - 1]);
   // printf("cumulative.%d\n",coder->cumulativeProb);
    precision = (8 * sizeof(uint16_t));
    coder->upper = (1LL << precision) - 1;
    if ( hp != 0 )
        coder->code = hread(&numbits,precision,hp), coder->code <<= (precision - numbits);
    return(numbits);
}
예제 #4
0
파일: proto.c 프로젝트: aberg001/plan9
/*
 *	only directories can be read.  everthing else returns EOF.
 */
static void
rread(Fcall *f)
{
    long cnt;
    Fid *fidp;

    cnt = f->count;
    f->count = 0;
    fidp = newfid(f->fid);
    f->data = (char*)rbuf+IOHDRSZ;
    if(fidp->open == 0) {
        reply(f, "file not open");
        return;
    }
    if ((fidp->node->d.mode&DMDIR) == 0) {
        reply(f, 0);				/*EOF*/
        return;
    }
    if(cnt > MAXRPC)
        cnt = MAXRPC;

    if(f->offset == 0)
        fidp->dirindex = 0;

    switch(fidp->node->d.type) {
    case Directory:
    case Addrdir:
    case Trusted:
        f->count = dread(fidp, cnt);
        break;
    case IPaddr:
    case Acctaddr:
        f->count = hread(fidp, cnt);
        break;
    default:
        reply(f, "can't read this type of file");
        return;
    }
    reply(f, 0);
}
예제 #5
0
static ssize_t multipart_read(hFILE *fpv, void *buffer, size_t nbytes)
{
    hFILE_multipart *fp = (hFILE_multipart *) fpv;
    size_t n;

open_next:
    if (fp->currentfp == NULL) {
        if (fp->current < fp->nparts) {
            const hfile_part *p = &fp->parts[fp->current];
            hts_log_debug("Opening part #%zu of %zu: \"%.120s%s\"",
                fp->current+1, fp->nparts, p->url,
                (strlen(p->url) > 120)? "..." : "");

            fp->currentfp = p->headers?
                  hopen(p->url, "r:", "httphdr:v", p->headers, NULL)
                : hopen(p->url, "r");

            if (fp->currentfp == NULL) return -1;
        }
        else return 0;  // No more parts, so we're truly at EOF
    }

    n = fp->currentfp->mobile?
          fp->currentfp->backend->read(fp->currentfp, buffer, nbytes)
        : hread(fp->currentfp, buffer, nbytes);

    if (n == 0) {
        // We're at EOF on this part, so set up the next part
        hFILE *prevfp = fp->currentfp;
        free_part(&fp->parts[fp->current]);
        fp->current++;
        fp->currentfp = NULL;
        if (hclose(prevfp) < 0) return -1;
        goto open_next;
    }

    return n;  // Number of bytes read by (or an error from) fp->currentfp
}
예제 #6
0
mFILE *find_file_url(char *file, char *url) {
    char buf[8192], *cp;
    mFILE *mf = NULL;
    int maxlen = 8190 - strlen(file), len;
    hFILE *hf;

    /* Expand %s for the trace name */
    for (cp = buf; *url && cp - buf < maxlen; url++) {
	if (*url == '%' && *(url+1) == 's') {
	    url++;
	    cp += strlen(strcpy(cp, file));
	} else {
	    *cp++ = *url;
	}
    }
    *cp++ = 0;

    if (!(hf = hopen(buf, "r")))
	return NULL;

    if (NULL == (mf = mfcreate(NULL, 0)))
	return NULL;
    while ((len = hread(hf, buf, 8192)) > 0) {
	if (mfwrite(buf, len, 1, mf) <= 0) {
	    hclose_abruptly(hf);
	    mfdestroy(mf);
	    return NULL;
	}
    }
    if (hclose(hf) < 0 || len < 0) {
	mfdestroy(mf);
	return NULL;
    }

    mrewind(mf);
    return mf;
}
예제 #7
0
파일: hfile.c 프로젝트: arq5x/bedtools2
// Loads the contents of filename to produced a read-only, in memory,
// immobile hfile.  fp is the already opened file.  We always close this
// input fp, irrespective of whether we error or whether we return a new
// immobile hfile.
static hFILE *hpreload(hFILE *fp) {
    hFILE *mem_fp;
    char *buf = NULL;
    off_t buf_sz = 0, buf_a = 0, buf_inc = 8192, len;

    for (;;) {
        if (buf_a - buf_sz < 5000) {
            buf_a += buf_inc;
            char *t = realloc(buf, buf_a);
            if (!t) goto err;
            buf = t;
            if (buf_inc < 1000000) buf_inc *= 1.3;
        }
        len = hread(fp, buf+buf_sz, buf_a-buf_sz);
        if (len > 0)
            buf_sz += len;
        else
            break;
    }

    if (len < 0) goto err;
    mem_fp = hfile_init_fixed(sizeof(hFILE), "r", buf, buf_sz, buf_a);
    if (!mem_fp) goto err;
    mem_fp->backend = &mem_backend;

    if (hclose(fp) < 0) {
        hclose_abruptly(mem_fp);
        goto err;
    }
    return mem_fp;

 err:
    free(buf);
    hclose_abruptly(fp);
    return NULL;
}
예제 #8
0
파일: puff.c 프로젝트: ilyarudyak/cs50_h
bool createForest(char *path) {
    
    // create empty forest
    f = mkforest();
    
    // open input
    Huffile* input = hfopen(path, "r");
    if (input == NULL)
    {
        printf("Could not open %s for reading.\n", path);
        return 0;
    }
    
    // read in header
    Huffeader header;
    if (hread(&header, input) == false)
    {
        hfclose(input);
        printf("Could not read header.\n");
        return 0;
    }
    
    // read symbols and freq from header, create and plant trees in forest
    
    for (int i = 0; i < SYMBOLS; i++)
    {
        if ( header.frequencies[i] != 0 )
        {   
            Tree *t = mktree();
            t->symbol = i;
            t->frequency = header.frequencies[i];
            plant(f, t);
            count++;
             
        }
             
    }
    
       
    // join trees as siblings
    
    for (int i = 0; i < count - 1; i++) 
    {
        Tree *t = mktree();
        t->left = pick(f);
        t->right = pick(f);
        t->frequency = t->left->frequency + t->right->frequency;
        plant(f, t);
     }   
        
        
        
             
    
    
    
    
    // close input
    hfclose(input);
    
    return true;
    
} 
예제 #9
0
파일: hfile.c 프로젝트: B-UMMI/INNUca
int main(void)
{
    static const int size[] = { 1, 13, 403, 999, 30000 };

    char buffer[40000];
    char *original;
    int c, i;
    ssize_t n;
    off_t off;

    reopen("vcf.c", "test/hfile1.tmp");
    while ((c = hgetc(fin)) != EOF) {
        if (hputc(c, fout) == EOF) fail("hputc");
    }
    if (herrno(fin)) { errno = herrno(fin); fail("hgetc"); }

    reopen("test/hfile1.tmp", "test/hfile2.tmp");
    if (hpeek(fin, buffer, 50) < 0) fail("hpeek");
    while ((n = hread(fin, buffer, 17)) > 0) {
        if (hwrite(fout, buffer, n) != n) fail("hwrite");
    }
    if (n < 0) fail("hread");

    reopen("test/hfile2.tmp", "test/hfile3.tmp");
    while ((n = hread(fin, buffer, sizeof buffer)) > 0) {
        if (hwrite(fout, buffer, n) != n) fail("hwrite");
        if (hpeek(fin, buffer, 700) < 0) fail("hpeek");
    }
    if (n < 0) fail("hread");

    reopen("test/hfile3.tmp", "test/hfile4.tmp");
    i = 0;
    off = 0;
    while ((n = hread(fin, buffer, size[i++ % 5])) > 0) {
        off += n;
        buffer[n] = '\0';
        check_offset(fin, off, "pre-peek");
        if (hputs(buffer, fout) == EOF) fail("hputs");
        if ((n = hpeek(fin, buffer, size[(i+3) % 5])) < 0) fail("hpeek");
        check_offset(fin, off, "post-peek");
    }
    if (n < 0) fail("hread");

    reopen("test/hfile4.tmp", "test/hfile5.tmp");
    n = hread(fin, buffer, 200);
    if (n < 0) fail("hread");
    else if (n != 200) fail("hread only got %d", (int)n);
    if (hwrite(fout, buffer, 1000) != 1000) fail("hwrite");
    check_offset(fin, 200, "input/first200");
    check_offset(fout, 1000, "output/first200");

    if (hseek(fin, 800, SEEK_CUR) < 0) fail("hseek/cur");
    check_offset(fin, 1000, "input/seek");
    for (off = 1000; (n = hread(fin, buffer, sizeof buffer)) > 0; off += n)
        if (hwrite(fout, buffer, n) != n) fail("hwrite");
    if (n < 0) fail("hread");
    check_offset(fin, off, "input/eof");
    check_offset(fout, off, "output/eof");

    if (hseek(fin, 200, SEEK_SET) < 0) fail("hseek/set");
    if (hseek(fout, 200, SEEK_SET) < 0) fail("hseek(output)");
    check_offset(fin, 200, "input/backto200");
    check_offset(fout, 200, "output/backto200");
    n = hread(fin, buffer, 800);
    if (n < 0) fail("hread");
    else if (n != 800) fail("hread only got %d", (int)n);
    if (hwrite(fout, buffer, 800) != 800) fail("hwrite");
    check_offset(fin, 1000, "input/wrote800");
    check_offset(fout, 1000, "output/wrote800");

    if (hflush(fout) == EOF) fail("hflush");

    original = slurp("vcf.c");
    for (i = 1; i <= 5; i++) {
        char *text;
        sprintf(buffer, "test/hfile%d.tmp", i);
        text = slurp(buffer);
        if (strcmp(original, text) != 0) {
            fprintf(stderr, "%s differs from vcf.c\n", buffer);
            return EXIT_FAILURE;
        }
        free(text);
    }
    free(original);

    if (hclose(fin) != 0) fail("hclose(input)");
    if (hclose(fout) != 0) fail("hclose(output)");

    fout = hopen("test/hfile_chars.tmp", "w");
    if (fout == NULL) fail("hopen(\"test/hfile_chars.tmp\")");
    for (i = 0; i < 256; i++)
        if (hputc(i, fout) != i) fail("chars: hputc (%d)", i);
    if (hclose(fout) != 0) fail("hclose(test/hfile_chars.tmp)");

    fin = hopen("test/hfile_chars.tmp", "r");
    if (fin == NULL) fail("hopen(\"test/hfile_chars.tmp\") for reading");
    for (i = 0; i < 256; i++)
        if ((c = hgetc(fin)) != i)
            fail("chars: hgetc (%d = 0x%x) returned %d = 0x%x", i, i, c, c);
    if ((c = hgetc(fin)) != EOF) fail("chars: hgetc (EOF) returned %d", c);
    if (hclose(fin) != 0) fail("hclose(test/hfile_chars.tmp) for reading");

    fin = hopen("data:hello, world!\n", "r");
    if (fin == NULL) fail("hopen(\"data:...\")");
    n = hread(fin, buffer, 300);
    if (n < 0) fail("hread");
    buffer[n] = '\0';
    if (strcmp(buffer, "hello, world!\n") != 0) fail("hread result");
    if (hclose(fin) != 0) fail("hclose(\"data:...\")");

    return EXIT_SUCCESS;
}
예제 #10
0
파일: dump.c 프로젝트: navpreetsingh/CS_50
int main(int argc, char* argv[])
{
    // ensure proper usage
    if (argc != 2)
    {
        printf("Usage: %s input\n", argv[0]);
        return 1;
    }

    // open input
    Huffile* input = hfopen(argv[1], "r");
    if (input == NULL)
    {
        printf("Could not open %s for reading.\n", argv[1]);
        return 1;
    }

    // read in header
    Huffeader header;
 	   if (hread(&header, input) == false)
    {
        hfclose(input);
        printf("Could not read header.\n");
        return 1;
    }

    // check for magic number
    if (header.magic != MAGIC)
    {
        hfclose(input);
        printf("File was not huffed.\n");
        return 1;
    }

    // check checksum
    int checksum = header.checksum;
    for (int i = 0; i < SYMBOLS; i++)
    {
        checksum -= header.frequencies[i];
    }
    if (checksum != 0)
    {
        hfclose(input);
        printf("File was not huffed.\n");
        return 1;
    }

    // determine number of rows to use
    const int rows = (int) ceil((LAST - FIRST) / (double) COLS);

    // dump frequencies in a nice table
    printf("\n");
    for (int row = 0; row < rows; row++)
    {
        for (int col = 0; col < COLS; col++)
        {
            int index = FIRST + row + rows * col;
            if (index > LAST)
            {
                break;
            }
            printf("%c %-6d  ", index, header.frequencies[index]);
        }
        printf("\n");
    }
    printf("\n");

    // dump bits contiguously
    int bit;
    while ((bit = bread(input)) != EOF)
    {
        printf("%d", bit);
    }
    printf("\n\n");

    // close input
    hfclose(input);

    // that's all folks!
    return 0;
}
예제 #11
0
파일: puff.c 프로젝트: avontd2868/CS50x-3
int main(int argc, char* argv[])
{
    // ensure proper usage
    if (argc != 3)
    {
        printf("Usage: %s input output\n", argv[0]);
        return 1;
    }

    // open input
    Huffile* input = hfopen(argv[1], "r");
    if (input == NULL)
    {
        printf("Could not open %s for reading.\n", argv[1]);
        return 1;
    }

    // open outfile
    FILE* outfile = fopen(argv[2], "w");
    
    // read in header
    Huffeader header;
    if (hread(&header, input) == false)
    {
        hfclose(input);
        printf("Could not read header.\n");
        return 1;
    }

    // check for magic number
    if (header.magic != MAGIC)
    {
        hfclose(input);
        printf("File was not huffed.\n");
        return 1;
    }

    // check checksum
    int checksum = header.checksum;
    for (int i = 0; i < SYMBOLS; i++)
    {
        checksum -= header.frequencies[i];
    }
    if (checksum != 0)
    {
        hfclose(input);
        printf("File was not huffed.\n");
        return 1;
    }
    
    // make forest
    Forest* forest = mkforest();
    
    // search for symbols that are non-zero frequency
    for (int i = 0; i < SYMBOLS; i++)
    {
        // make and plant the trees in the forest
        if (header.frequencies[i] >= 1)
            {
                Tree* newTree = mktree();
                newTree->frequency = header.frequencies[i];
                newTree->symbol = i;
                newTree->left = NULL;
                newTree->right = NULL;
                plant(forest, newTree);
            }
    }
    
    // run loop until there is only one tree left
    bool done = false;
    while (!done)
    {
        // pick smallest tree
        Tree* a = pick(forest);
        
        // pick second smallest tree
        Tree* b = pick(forest);
        
        // if there is only one tree left in the forest, a is the huffman tree
        if (b == NULL)
        {
            done = true;
            root = a;
        }
        
        // if two trees were succesfully picked
        else
        {
            // combine the two trees by calling the combine function
            Tree* combinedTree = combine(a, b);
            
            // plant combined tree back in forest
            plant(forest, combinedTree);
        }

    }

    // write message to file
    int bit;
    Tree* cursor = root;
    while ((bit = bread(input)) != EOF)	
	{	

	    // if bit == 0 -> go left
	    if (bit == 0)
	    {
	        cursor = cursor->left;
	    }

	    // if bit == 1 -> go right
	    else if (bit == 1)
	    {
	       cursor = cursor->right;
	    }

	    // when you find a leaf
	    if ((cursor->right == NULL) && (cursor->left == NULL))
	    {
	        // print the leaf
	        fprintf(outfile, "%c", cursor->symbol);

	        // reset the cursor to root for the next iteration
	        cursor = root;
	    }
	}
    
    // free root
    rmtree(root);
    
    // close forest
    rmforest(forest);
    
    // close input
    hfclose(input);
    
    // close outfile
    fclose(outfile);

    // that's all folks!
    return 0;
}
예제 #12
0
/*
 * Loads a CRAM .crai index into memory.
 *
 * Returns 0 for success
 *        -1 for failure
 */
int cram_index_load(cram_fd *fd, const char *fn) {
    char fn2[PATH_MAX];
    char buf[65536];
    ssize_t len;
    kstring_t kstr = {0};
    hFILE *fp;
    cram_index *idx;
    cram_index **idx_stack = NULL, *ep, e;
    int idx_stack_alloc = 0, idx_stack_ptr = 0;
    size_t pos = 0;

    /* Check if already loaded */
    if (fd->index)
	return 0;

    fd->index = calloc((fd->index_sz = 1), sizeof(*fd->index));
    if (!fd->index)
	return -1;

    idx = &fd->index[0];
    idx->refid = -1;
    idx->start = INT_MIN;
    idx->end   = INT_MAX;

    idx_stack = calloc(++idx_stack_alloc, sizeof(*idx_stack));
    idx_stack[idx_stack_ptr] = idx;

    sprintf(fn2, "%s.crai", fn);
    if (!(fp = hopen(fn2, "r"))) {
	perror(fn2);
	free(idx_stack);
	return -1; 
    }

    // Load the file into memory
    while ((len = hread(fp, buf, 65536)) > 0)
	kputsn(buf, len, &kstr);
    if (len < 0 || kstr.l < 2) {
	if (kstr.s)
	    free(kstr.s);
	free(idx_stack);
	return -1;
    }

    if (hclose(fp)) {
	if (kstr.s)
	    free(kstr.s);
	free(idx_stack);
	return -1;
    }
	

    // Uncompress if required
    if (kstr.s[0] == 31 && (uc)kstr.s[1] == 139) {
	size_t l;
	char *s = zlib_mem_inflate(kstr.s, kstr.l, &l);
	free(kstr.s);
	if (!s) {
	    free(idx_stack);
	    return -1;
	}
	kstr.s = s;
	kstr.l = l;
	kstr.m = l; // conservative estimate of the size allocated
	kputsn("", 0, &kstr); // ensure kstr.s is NUL-terminated
    }


    // Parse it line at a time
    do {
	/* 1.1 layout */
	if (kget_int32(&kstr, &pos, &e.refid) == -1) {
	    free(kstr.s); free(idx_stack); return -1;
	}
	if (kget_int32(&kstr, &pos, &e.start) == -1) {
	    free(kstr.s); free(idx_stack); return -1;
	}
	if (kget_int32(&kstr, &pos, &e.end) == -1) {
	    free(kstr.s); free(idx_stack); return -1;
	}
	if (kget_int64(&kstr, &pos, &e.offset) == -1) {
	    free(kstr.s); free(idx_stack); return -1;
	}
	if (kget_int32(&kstr, &pos, &e.slice) == -1) {
	    free(kstr.s); free(idx_stack); return -1;
	}
	if (kget_int32(&kstr, &pos, &e.len) == -1) {
	    free(kstr.s); free(idx_stack); return -1;
	}

	e.end += e.start-1;
	//printf("%d/%d..%d\n", e.refid, e.start, e.end);

	if (e.refid < -1) {
	    free(kstr.s);
	    free(idx_stack);
	    fprintf(stderr, "Malformed index file, refid %d\n", e.refid);
	    return -1;
	}

	if (e.refid != idx->refid) {
	    if (fd->index_sz < e.refid+2) {
		size_t index_end = fd->index_sz * sizeof(*fd->index);
		fd->index_sz = e.refid+2;
		fd->index = realloc(fd->index,
				    fd->index_sz * sizeof(*fd->index));
		memset(((char *)fd->index) + index_end, 0,
		       fd->index_sz * sizeof(*fd->index) - index_end);
	    }
	    idx = &fd->index[e.refid+1];
	    idx->refid = e.refid;
	    idx->start = INT_MIN;
	    idx->end   = INT_MAX;
	    idx->nslice = idx->nalloc = 0;
	    idx->e = NULL;
	    idx_stack[(idx_stack_ptr = 0)] = idx;
	}

	while (!(e.start >= idx->start && e.end <= idx->end)) {
	    idx = idx_stack[--idx_stack_ptr];
	}

	// Now contains, so append
	if (idx->nslice+1 >= idx->nalloc) {
	    idx->nalloc = idx->nalloc ? idx->nalloc*2 : 16;
	    idx->e = realloc(idx->e, idx->nalloc * sizeof(*idx->e));
	}

	e.nalloc = e.nslice = 0; e.e = NULL;
	*(ep = &idx->e[idx->nslice++]) = e;
	idx = ep;

	if (++idx_stack_ptr >= idx_stack_alloc) {
	    idx_stack_alloc *= 2;
	    idx_stack = realloc(idx_stack, idx_stack_alloc*sizeof(*idx_stack));
	}
	idx_stack[idx_stack_ptr] = idx;

	while (pos < kstr.l && kstr.s[pos] != '\n')
	    pos++;
	pos++;
    } while (pos < kstr.l);

    free(idx_stack);
    free(kstr.s);

    // dump_index(fd);

    return 0;
}
예제 #13
0
파일: puff.c 프로젝트: ilyarudyak/cs50_h
/**
 * decompress Huffile and print to file.
 */
bool decompress(char *path1, char *path2) {

    // open input
    Huffile* input = hfopen(path1, "r");
    if (input == NULL)
    {
        printf("Could not open %s for reading.\n", path1);
        return false;
    }

    // read in header
    Huffeader header;
    if (hread(&header, input) == false)
    {
        hfclose(input);
        printf("Could not read header.\n");
        return false;
    }
    
    // open file for writing
    FILE *out = fopen(path2, "w");
    if (out == NULL)
    {
        printf("Could not open %s for writing.\n", path2);
        return false;
    }
    
    // read bits, crawl the tree and write to out
    int bit;
    char ch;
    Tree *p = f->first->tree;
    
    // check single-character file
    if ( count == 1 ) 
    {
        ch = p->symbol;
        if ( fputc(ch, out) == EOF ) 
        {
            printf("can't write\n");
            return false;
        }
        
        return true;
    }
    
    while ((bit = bread(input)) != EOF)
    {   
        
        
        if ( bit == 0 ) 
        {
            p = p->left;
            if ( isLeaf(p) ) 
            {
                ch = p->symbol;
                if ( fputc(ch, out) == EOF ) 
                {
                printf("can't write\n");
                return false;
                }
                p = f->first->tree;
            }    
        }
            
        else if ( bit == 1 )
        {
            p = p->right;
            if ( isLeaf(p) ) 
            {
                ch = p->symbol;
                if ( fputc(ch, out) == EOF )
                {
                printf("can't write\n");
                return false;
                } 
                p = f->first->tree;
            }    
        }
        
        
    }    
    
    return true;
}
예제 #14
0
HTTPRESULT *http_read(char *url, int maxlen)
{
	HTTPRESULT *result = http_new_result();
	
	if ( strncmp(url,"http://",7)==0 )
	{
		HTTP *fp = hopen(url,maxlen);
		if ( fp==NULL )
		{
			output_error("http_read('%s'): unable to access url", url);
			result->status = errno;
		}
		else
		{
			size_t len = (int)hfilelength(fp);
			size_t hlen = len;
			char *buffer = (char*)malloc(len+1);
			char *data = NULL;
			if ( hread(buffer,len,fp)<=0 )
			{
				output_error("http_read('%s'): unable to read file", url);
				result->status = errno;
			}
			else 
			{
				buffer[len]='\0';
				data = strstr(buffer,"\r\n\r\n");
				if ( data!=NULL )
				{
					hlen = data - buffer;
					*data++ = '\0';
					result->body.size = (int)(len - hlen - 1);
					while ( isspace(*data) ) { data++; result->body.size--; }
//					if ( sscanf(data,"%x",&dlen)==1 )
//					{
//						data = strchr(data,'\n');
//						while ( isspace(*data) ) {data++;}
//						result->body.size = dlen;
//						result->body.data = malloc(dlen+1);
//					}
//					else
//					{
						result->body.data = malloc(result->body.size+1);
//					}
					memcpy(result->body.data,data,result->body.size);
					result->body.data[result->body.size] = '\0';
				}
				else
				{
					result->body.data = "";
					result->body.size = 0;
				}
				result->header.size = (int)hlen;
				result->header.data = malloc(hlen+1);
				strcpy(result->header.data,buffer);
				result->status = 0;
			}
		}
		hclose(fp);
	}

	else if ( strncmp(url,"file://",7)==0 )
	{
		FILE *fp = fopen(url+7,"rt");
		if ( fp==NULL )
		{
			output_error("http_read('%s'): unable to access file", url);
			result->status = errno;
		}
		else
		{
			result->body.size = filelength(fileno(fp))+1;
			result->body.data = malloc(result->body.size);
			memset(result->body.data,0,result->body.size);
			if ( fread(result->body.data,1,result->body.size,fp)<=0 )
			{
				output_error("http_read('%s'): unable to read file", url);
				result->status = errno;
			}
			else
				result->status = 0;
		}
		fclose(fp);
	}
	return result;
}
예제 #15
0
파일: bayes.c 프로젝트: AustenConrad/plan-9
void
main(int argc, char **argv)
{
	int i, j, a, mi, oi, tot, keywords;
	double totp, p, xp[MAXTAB];
	Hash *hmsg;
	Word w;
	Stringtab *s, *t;
	Biobuf bout;

	mbest = 15;
	keywords = 0;
	ARGBEGIN{
	case 'D':
		debug = 1;
		break;
	case 'k':
		keywords = 1;
		break;
	case 'm':
		mbest = atoi(EARGF(usage()));
		if(mbest > MAXBEST)
			sysfatal("cannot keep more than %d words", MAXBEST);
		break;
	default:
		usage();
	}ARGEND

	for(i=0; i<argc; i++)
		if(strcmp(argv[i], "~") == 0)
			break;

	if(i > MAXTAB)
		sysfatal("cannot handle more than %d tables", MAXTAB);

	if(i+1 >= argc)
		usage();

	for(i=0; i<argc; i++){
		if(strcmp(argv[i], "~") == 0)
			break;
		tab[ntab].file = argv[i];
		tab[ntab].hash = hread(argv[i]);
		s = findstab(tab[ntab].hash, "*nmsg*", 6, 1);
		if(s == nil || s->count == 0)
			tab[ntab].nmsg = 1;
		else
			tab[ntab].nmsg = s->count;
		ntab++;
	}

	Binit(&bout, 1, OWRITE);

	oi = ++i;
	for(a=i; a<argc; a++){
		hmsg = hread(argv[a]);
		nbest = 0;
		for(s=hmsg->all; s; s=s->link){
			w.s = s;
			tot = 0;
			totp = 0.0;
			for(i=0; i<ntab; i++){
				t = findstab(tab[i].hash, s->str, s->n, 0);
				if(t == nil)
					w.count[i] = 0;
				else
					w.count[i] = t->count;
				tot += w.count[i];
				p = w.count[i]/(double)tab[i].nmsg;
				if(p >= 1.0)
					p = 1.0;
				w.p[i] = p;
				totp += p;
			}

			if(tot < 5){		/* word does not appear enough; give to box 0 */
				w.p[0] = 0.5;
				for(i=1; i<ntab; i++)
					w.p[i] = 0.1;
				w.mp = 0.5;
				w.mi = 0;
				noteword(&w);
				continue;
			}

			w.mp = 0.0;
			for(i=0; i<ntab; i++){
				p = w.p[i];
				p /= totp;
				if(p < 0.01)
					p = 0.01;
				else if(p > 0.99)
					p = 0.99;
				if(p > w.mp){
					w.mp = p;
					w.mi = i;
				}
				w.p[i] = p;
			}
			noteword(&w);
		}

		totp = 0.0;
		for(i=0; i<ntab; i++){
			p = 1.0;
			for(j=0; j<nbest; j++)
				p *= best[j].p[i];
			xp[i] = p;
			totp += p;
		}
		for(i=0; i<ntab; i++)
			xp[i] /= totp;
		mi = 0;
		for(i=1; i<ntab; i++)
			if(xp[i] > xp[mi])
				mi = i;
		if(oi != argc-1)
			Bprint(&bout, "%s: ", argv[a]);
		Bprint(&bout, "%s %f", tab[mi].file, xp[mi]);
		if(keywords){
			for(i=0; i<nbest; i++){
				Bprint(&bout, " ");
				Bwrite(&bout, best[i].s->str, best[i].s->n);
				Bprint(&bout, " %f", best[i].p[mi]);
			}
		}
		freehash(hmsg);
		Bprint(&bout, "\n");
		if(debug){
			for(i=0; i<nbest; i++){
				Bwrite(&bout, best[i].s->str, best[i].s->n);
				Bprint(&bout, " %f", best[i].p[mi]);
				if(best[i].p[mi] < best[i].mp)
					Bprint(&bout, " (%f %s)", best[i].mp, tab[best[i].mi].file);
				Bprint(&bout, "\n");
			}
		}
	}
	Bterm(&bout);
}
예제 #16
0
파일: puff.c 프로젝트: ramanbedi1989/CS50
int main(int argc, char* argv[])
{
    // ensure proper usage
    if (argc != 3)
    {
        printf("Usage: %s input output\n", argv[0]);
        return 1;
    }

    // open input
    Huffile* input = hfopen(argv[1], "r");
    if (input == NULL)
    {
        printf("Could not open %s for reading.\n", argv[1]);
        return 1;
    }

    // read in header
    Huffeader header;
    if (hread(&header, input) == false)
    {
        hfclose(input);
        printf("Could not read header.\n");
        return 1;
    }

    // check for magic number
    if (header.magic != MAGIC)
    {
        hfclose(input);
        printf("File was not huffed.\n");
        return 1;
    }

    // check checksum
    int checksum = header.checksum;
    for (int i = 0; i < SYMBOLS; i++)
    {
        checksum -= header.frequencies[i];
    }
    if (checksum != 0)
    {
        hfclose(input);
        printf("File was not huffed.\n");
        return 1;
    }
    int index;
    Forest* f1 = mkforest();
    for(index = 0; index<SYMBOLS;index++)
    {
      if(header.frequencies[index] != 0)
      {
        Tree* t1 = mktree();
        t1->symbol = (char)index;
        t1->frequency = header.frequencies[index];
        plant(f1,t1);
      }
    }
    
    while(true)
    {
        Tree* temp1 = pick(f1);
        Tree* temp2 = pick(f1);
        if(temp2 != NULL)
        {
          Tree* new_tree = mktree();
          new_tree->left = temp1;
          new_tree->right = temp2;
          new_tree->frequency = temp1->frequency + temp2->frequency;
          plant(f1,new_tree);
        }
        else
        {
          plant(f1,temp1);
          break;
        }
    }
    Tree* huffman_tree = pick(f1);
    int bit;
    Tree* temp = huffman_tree;
    FILE *output = fopen(argv[2],"w");
    while ((bit = bread(input)) != EOF)
    {
        if(bit == 1)
        {
            temp = temp->right;
            if(temp->right == NULL && temp->left ==NULL)
            {
              fprintf(output,"%c",temp->symbol);
              temp = huffman_tree;
            }
        }
        else
        {
            temp = temp->left;
            if(temp->right == NULL && temp->left ==NULL)
            {
              fprintf(output,"%c",temp->symbol);
              temp = huffman_tree;
            }
        }
    }

    // close input
    hfclose(input);
    return 0;
}
예제 #17
0
파일: puff.c 프로젝트: juliamann/CS50
int main(int argc, char* argv[])
{
    // ensure proper usage
    if (argc != 3)
    {
        printf("Usage: %s input\n", argv[0]);
        return 1;
    }

    // open input
    Huffile* input = hfopen(argv[1], "r");
    if (input == NULL)
    {
        printf("Could not open %s for reading.\n", argv[1]);
        return 1;
    }

    // open outfile
    FILE* outfile = fopen(argv[2], "w");
    if (outfile == NULL)
    {
        fclose(outfile);
        fprintf(stderr, "Could not create outfile.\n");
        return 2;
    }

    // read in header
    Huffeader header;
    if (hread(&header, input) == false)
    {
        hfclose(input);
        printf("Could not read header.\n");
        return 1;
    }

    // check for magic number
    if (header.magic != MAGIC)
    {
        hfclose(input);
        printf("File was not huffed.\n");
        return 1;
    }

    // check checksum
    int checksum = header.checksum;
    for (int i = 0; i < SYMBOLS; i++)
    {
        checksum -= header.frequencies[i];
    }
    if (checksum != 0)
    {
        hfclose(input);
        printf("File was not huffed.\n");
        return 1;
    }

    // make forest
    Forest* forest = mkforest();

    // read in huffeader frequencies
    for (int i = 0; i < SYMBOLS; i++)
    {
         // ignore 0 frequencies
        if (header.frequencies[i] > 0)
        {
            // make new tree for every non-zero frequency occurance
            Tree* new_tree = mktree();
            new_tree->symbol = i;
            new_tree->frequency = header.frequencies[i];
            new_tree->left = NULL;
            new_tree->right = NULL;

            // plant every non-zero frequency tree in forest
            plant(forest, new_tree);
        }
    }

    // run loop until there is only one tree left
    bool done = false;
    while (!done)
    {
        // pick smallest tree from forest
        Tree* a = pick(forest);

        // pick second smallest tree from forest
        Tree* b = pick(forest);

        // if there is no second tree in forest...
        if (b == NULL)
        {
            // break loop
            done = true;
            // set root to tree 'a' (last remaining) tree
            root = a;
        }

        // else there are at least two remaining trees in the forest
        else
        {
            // combine the two trees into a parent tree
            Tree* parent_tree = combine(a, b);

            // plant combined tree in forest
            plant(forest, parent_tree);
        }
    }

    // write message to outfile
    int bit;
    Tree* ptr_location = root;
    while ((bit = bread(input)) != EOF)
    {
        // if bit is 0, go left, else go right
        if (bit == 0)
            ptr_location = ptr_location->left; 

        // if bit is 1, go right
        if (bit == 1)
            ptr_location = ptr_location->right;

        // leaf is found when both branchs are NULL
        if ((ptr_location->left == NULL) && (ptr_location->right == NULL))
        {
            // write leaf's symbol to outfile
            fprintf(outfile, "%c", ptr_location->symbol);

            // reset pointer location to root for next iteration of tree
            ptr_location = root;
        }
    }

    // free root
    rmtree(root);

    // close forest
    rmforest(forest);

    // close input & outfile
    hfclose(input);
    fclose(outfile);

    // that's all folks!
    return 0;
}