Reuse, not rework
Home

License Awareness



Highly Reusable Software

By activity
Professions, Sciences, Humanities, Business, ...

User Interface
Text-based, GUI, Audio, Video, Keyboards, Mouse, Images,...

Text Strings
Conversions, tests, processing, manipulation,...

Math
Integer, Floating point, Matrix, Statistics, Boolean, ...

Processing
Algorithms, Memory, Process control, Debugging, ...

Stored Data
Data storage, Integrity, Encryption, Compression, ...

Communications
Networks, protocols, Interprocess, Remote, Client Server, ...

Hard World
Timing, Calendar and Clock, Audio, Video, Printer, Controls...

File System
Management, Filtering, File & Directory access, Viewers, ...


NAME

librock_afgets - read line of unlimited length from FILE *
librock_afgetsX - read line of unlimited length from FILE * using specified allocator

#License - #Source code - #Example Use -

SYNOPSIS

#include <librock/target/types.c>
#include <librock/astring.h>

char *librock_afgets(
    char * *ppasz,
    FILE *f
);

DESCRIPTION

These are fgets() replacements which do automatic reallocation. Instead of a pointer to the destination buffer, these take a pointer to the pointer, so that the buffer can be reallocated if necessary to avoid overrun.

librock_afgets will read characters from FILE f until reach EOF, \0, or \n. All characters plus the \n (if it appeared) and a NUL terminator are stored into a (re)alloced buffer obtained by calling librock_astrensure() Note: To preserve \0 in the stream, see librock_afgettoch() instead.

The return value is also stored at *ppasz.

These functions are part of the Librock allocated string functions. See () for an overview. Since the librock allocated string buffers hold NUL terminated strings, most "read-only" string functions of libc and other libraries, such as strlen(), strchr(), can be used.

As with all librock_astr* functions, the caller must assume the buffer will move, and consider all pointers into the old buffer *ppasz unusable. See the () implementation for a good way to use indexes to avoid pointers which go stale.

The allocation function, librock_astrensure(), makes realloc requests in multiples of a fixed size. The primary benefit of this strategy is to reduce realloc calls by assuming the buffer is at least a minimum size. For this and other reasons, be sure to call only with *ppasz set to 0 or a value obtained from a previous call to one of these functions.

Typical Use:
#ifdef librock_TYPICAL_USE_afgets
    char *asz =0; /* Be sure to initialize to 0 */
    while(librock_afgets(&asz,f)) {
        /* Process line Here */
    }
    /* As long as go through entire loop, no need to astrfree */
#endif

VARIANTS

This function uses the realloc function supplied when the astring implementation was compiled. Supply a realloc function on a per-call basis (useful for memory pooling, optimization, and more), by using the X suffix variant:
#include <librock/target/types.c>
#include <librock/astring.h>

char *librock_afgetsX(
    char * *ppasz,
    FILE *f,
    void *(*reallocfn)(void *,size_t)
);

USES

  fgetc() feof()
  librock_astrensureX() librock_astrfreeX()

  realloc()                     // used by librock_afgets

LICENSE

  Copyright 1998-2000 Forrest J. Cavalier III, http://www.mibsoftware.com
  Open-source under MIT license. NO WARRANTY. Copies must retain this block.
  License text in <librock/license/mit.txt> librock_LIDESC_HC=cc598307414a9997b32b60a2e7a8e7c6a13d6438

Source Code

./text/astring.c (implementation, plus source of this manual page)

This software is part of Librock

Rapid reuse, without rework. Details
This page copyright (C) 2002-2004 Forrest J. Cavalier III, d-b-a Mib Software, Saylorsburg PA 18353, USA

Verbatim copying and distribution of this generated page is permitted in any medium provided that no changes are made.
(The source of this manual page may be covered by a more permissive license which allows modifications.)

Want to help? We welcome comments, patches. -- Need help? Request paid support.