Platon Technologies
not logged in Login Registration
EnglishSlovak
open source software development celebrating 10 years of open source development! Thursday, March 28, 2024

File: [Platon] / cpdf / utils.c (download)

Revision 1.12, Tue Feb 11 20:10:30 2003 UTC (21 years, 1 month ago) by lynx


Changes since 1.11: +3 -2 lines

improved reading and working with objects
remove read bug from get_startxref() if PDF uses \r\n as a newline.
better reading streams.
AND MANY MANY MORE! :))) but you still cant use it :-D

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "parse.h"
void *xmalloc(size_t size)
{
    void *p;
    p = malloc(size);
    if (!p) {
        fprintf(stderr, "malloc: %s", strerror(errno));
        exit(errno);
    }
    return p;
}

void *xcalloc(size_t nmemb, size_t size)
{
    void *p;
    p = xmalloc(nmemb * size);
    memset(p, '\0', nmemb * size);
    return p;
}
char *resize(char *ptr, size_t size)
{
/*    char *ret;
    if (!ptr)
        ret = (char *) xcalloc(size, 1);
    else {
        ret = (char *) xcalloc(size, 1);
        memcpy(ret, ptr, size);
        free(ptr);
    }
    return ret;*/
    return (char *) realloc(ptr, size);
}

char *fget(char *s, int size, FILE * stream)
{
    int i;
    char c;
    for (i = 0; i < size; i++) {
        s[i] = fgetc(stream);
        if (s[i] == '\n' || s[i] == '\r') {
            s[i] = '\n';
            s[i + 1] = '\0';
            do {
                c = fgetc(stream);
            } while (c == '\n' || c == '\r');
            fseek(stream, -1, SEEK_CUR);
            break;
        }
    }
    return s;
}

char *strnstr(const char *haystack, char *needle, size_t size)
{
    char *max;
    size_t len = strlen(needle);
    if (!haystack || !needle || !size)
        return NULL;
    for (max = (char *) (haystack + size - len); haystack < max;
         haystack++)
        if (*haystack == *needle)
            if (!strncmp(haystack, needle, len))
                return (char *) haystack;
    return NULL;
}

Platon Group <platon@platon.org> http://platon.org/
Copyright © 2002-2006 Platon Group
Site powered by Metafox CMS
Go to Top