Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7751883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:39:43+00:00 2026-06-01T11:39:43+00:00

In my open-source plain C code I use this simple structure to read and

  • 0

In my open-source plain C code I use this simple structure to read and parse data from a string buffer:

typedef struct lts_LoadState
{
  const unsigned char * pos;
  size_t unread;
} lts_LoadState;

The buffer is accessed with this simple API:

/* Initialize buffer */
void ltsLS_init(lts_LoadState * ls,const unsigned char * data, size_t len);

/* Do we have something to read? (Actually a macro.) */
BOOL ltsLS_good(ls);

/* How much do we have to read? (Actually a macro.) */
size_t ltsLS_unread(ls);

/* Eat given number of characters, return pointer to beginning of eaten data */
const unsigned char * ltsLS_eat(lts_LoadState * ls, size_t len);

Note: ltsLS_unread may be replaced with return (ltsLS_good(ls)) ? SIZE_MAX : 0 without breaking the current implementation.

This code is used to load some data in a custom format from a string buffer. (This may be a better illustration.)


Now I need to load data not from a string buffer, but from a FILE pointer.

I would hate to copy-paste the implementation, and would like to reuse existing code instead. (I’m OK with refactoring/adapting it, of course.)

This is a textbook stuff in C++, but how to do that in plain C without incurring runtime overhead?


Here is an example function that uses the lts_LoadState API and that is not to be copy-pasted (but may be changed, of course, to support both string buffer and FILE *):

static int ltsLS_readline(
    lts_LoadState * ls,
    const unsigned char ** dest,
    size_t * len
  )
{
  const unsigned char * origin = ls->pos;
  unsigned char last = 0;
  size_t read = 0;

  while (ltsLS_good(ls))
  {
    if (ltsLS_unread(ls) > 0)
    {
      unsigned char b = *ls->pos; /* OK, this should be ltsLS_eat_char macro. */
      ++ls->pos;
      --ls->unread;

      if (b == '\n')
      {
        *dest = origin;
        *len = (last == '\r') ? read - 1 : read;

        return LUATEXTS_ESUCCESS;
      }

      last = b;
      ++read;
    }
    else
    {
      ls->unread = 0;
      ls->pos = NULL;
    }
  }

  return LUATEXTS_ECLIPPED;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T11:39:45+00:00Added an answer on June 1, 2026 at 11:39 am

    It sounds like you want function variables, which you would pass as a parameter. C can do them, but the syntax isn’t very pretty.

    There probably is a bit of runtime overhead, but not much.

    How do you pass a function as a parameter in C?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My open-source Android application uses this in an SQL query: String.format(%f, someDoubleValue); Unfortunately, in
I'm looking for an open-source SGML parser written in plain C. This is to
I need an open-source (preferably MIT-licensed) light-weight growable buffer implemented in plain C (preferably
Some open source libraries have tendency to re implement basic structures like string, list,
What is the best open-source library to manage X10 devices from Java?
Gobby/Sobby is an open source client/server for collaborative edition of plain text file (source
This code streams large files to our users: // Open the file. iStream =
I would like to port an existing Linux application from its plain C/C++ source
I run an open source project that normally distributes our code in a universal
which open source software options exist for generating terminal graphics? I mean, there is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.