Changeset aa25216 for libcfa/src


Ignore:
Timestamp:
Aug 25, 2023, 12:53:55 PM (10 months ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
master
Children:
8f2aa3c
Parents:
88001dd
Message:

remove special case for getline in C-string input

Location:
libcfa/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/iostream.cfa

    r88001dd raa25216  
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Thu Aug 24 10:25:20 2023
    13 // Update Count     : 1516
     12// Last Modified On : Fri Aug 25 12:04:27 2023
     13// Update Count     : 1526
    1414//
    1515
     
    2121#include <float.h>                                                                              // DBL_DIG, LDBL_DIG
    2222#include <complex.h>                                                                    // creal, cimag
    23 //#include <string.h>                                                                   // strlen, strcmp, memcpy
     23//#include <stdio.h>
     24
    2425extern "C" {
    2526extern size_t strlen (const char *__s) __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__pure__)) __attribute__ ((__nonnull__ (1)));
     
    971972
    972973                int check = f.wd - 1;
     974                const char * scanset = f.scanset;;
     975                if ( f.flags.delimit ) scanset = f.delimit;             // getline ?
    973976
    974977                // getline
    975                 if ( f.flags.delimit ) {
    976                         enum { size = 32 };
    977                         char fmtstr[size];
    978                         snprintf( fmtstr, size, "%%%d[^%c]s", f.wd, f.delimit );
    979                         if ( ! f.flags.rwd ) f.s[check] = '\0';         // insert sentinel
    980                         int len = fmt( is, fmtstr, f.s );                       // read upto delimiter
    981                         if ( ! f.flags.rwd && f.s[check] != '\0' )      // sentinel overwritten ?
    982                                 throw (cstring_length){ &cstring_length_vt };
    983                         if ( len == 0 ) f.s[0] = '\0';                          // empty read => argument unchanged => set empty
    984 //                      fprintf( stderr, "getline %s %s %d %d %d '%c'\n", fmtstr, f.s, len, f.wd, check, f.s[check] );
    985                         if ( ! eof( is ) ) fmt( is, "%*c" );            // ignore delimiter
    986                         return is;
    987                 } // if
     978                // if ( f.flags.delimit ) {
     979                //      enum { size = 32 };
     980                //      char fmtstr[size];
     981                //      snprintf( fmtstr, size, "%%%d[^%c]s", f.wd, f.delimit );
     982                //      if ( ! f.flags.rwd ) f.s[check] = '\0';         // insert sentinel
     983                //      int len = fmt( is, fmtstr, f.s );                       // read upto delimiter
     984                //      if ( ! f.flags.rwd && f.s[check] != '\0' )      // sentinel overwritten ?
     985                //              throw (cstring_length){ &cstring_length_vt };
     986                //      if ( len == 0 ) f.s[0] = '\0';                          // empty read => argument unchanged => set empty
     987                //      // fprintf( stderr, "getline %s %s %d %d %d '%c'\n", fmtstr, f.s, len, f.wd, check, f.s[check] );
     988                //      if ( ! eof( is ) ) fmt( is, "%*c" );            // ignore delimiter
     989                //      return is;
     990                // } // if
    988991
    989992                size_t len = 0;
    990                 if ( f.scanset ) len = strlen( f.scanset );
     993                if ( scanset ) len = strlen( scanset );
    991994                char fmtstr[len + 16];
    992995                int start = 1;
     
    995998                if ( f.wd != -1 ) { start += sprintf( &fmtstr[start], "%d", f.wd ); }
    996999
    997                 if ( ! f.scanset ) {
     1000                if ( ! scanset ) {
    9981001                        // %s, %*s, %ws, %*ws
    9991002                        fmtstr[start] = 's'; fmtstr[start + 1] = '\0';
     
    10041007                        fmtstr[start] = '['; start += 1;
    10051008                        if ( f.flags.inex ) { fmtstr[start] = '^'; start += 1; }
    1006                         strcpy( &fmtstr[start], f.scanset );            // copy includes '\0'
     1009                        strcpy( &fmtstr[start], scanset );                      // copy includes '\0'
    10071010                        len += start;
    10081011                        fmtstr[len] = ']'; fmtstr[len + 1] = '\0';
     
    10111014
    10121015                if ( ! f.flags.rwd ) f.s[check] = '\0';                 // insert sentinel
    1013                 fmt( is, fmtstr, f.s );
    1014 //              fprintf( stderr, "KK %s %d %c\n", fmtstr, check, f.s[check] );
     1016                len = fmt( is, fmtstr, f.s );
     1017                //fprintf( stderr, "KK %s %zd %d %c\n", fmtstr, len, check, f.s[check] );
    10151018                if ( ! f.flags.rwd && f.s[check] != '\0' )              // sentinel overwritten ?
    10161019                        throw (cstring_length){ &cstring_length_vt };
     1020
     1021                if ( f.flags.delimit ) {                                                // getline ?
     1022                        if ( len == 0 ) f.s[0] = '\0';                          // empty read => argument unchanged => set empty
     1023                        if ( ! eof( is ) ) fmt( is, "%*c" );            // ignore delimiter
     1024                } //if
    10171025                return is;
    10181026        } // ?|?
  • libcfa/src/iostream.hfa

    r88001dd raa25216  
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Thu Aug 24 11:35:02 2023
    13 // Update Count     : 530
     12// Last Modified On : Fri Aug 25 11:55:46 2023
     13// Update Count     : 533
    1414//
    1515
     
    2020
    2121// *********************************** ostream ***********************************
    22 
    2322
    2423forall( ostype & )
     
    305304} // ?|?
    306305
    307 
    308306// *********************************** istream ***********************************
    309 
    310307
    311308#define ISTYPE_VOID( T ) void ?|?( istype &, T )
     
    403400} // distribution
    404401
    405 
    406402// *********************************** exceptions ***********************************
    407403
    408 
    409404ExceptionDecl( cstring_length );
    410 
    411405
    412406// *********************************** manipulators ***********************************
     
    416410        union {
    417411                const char * scanset;
    418                 char delimit;
     412                char delimit[2];
    419413        };
    420414        int wd;                                                                                         // width
     
    440434        _Istream_Cstr skip( const char scanset[] ) { return (_Istream_Cstr){ 0p, {scanset}, -1, {.all : 0} }; }
    441435        _Istream_Cstr skip( unsigned int wd ) { return (_Istream_Cstr){ 0p, {0p}, wd, {.all : 0} }; }
    442         _Istream_Cstr & getline( _Istream_Cstr & fmt, const char delimit = '\n' ) { fmt.delimit = delimit; fmt.flags.delimit = true; return fmt; }
     436        _Istream_Cstr & getline( _Istream_Cstr & fmt, const char delimit = '\n' ) {
     437                fmt.delimit[0] = delimit; fmt.delimit[1] = '\0'; fmt.flags.delimit = true; fmt.flags.inex = true; return fmt; }
    443438        _Istream_Cstr & incl( const char scanset[], _Istream_Cstr & fmt ) { fmt.scanset = scanset; fmt.flags.inex = false; return fmt; }
    444439        _Istream_Cstr & excl( const char scanset[], _Istream_Cstr & fmt ) { fmt.scanset = scanset; fmt.flags.inex = true; return fmt; }
     
    502497INPUT_FMT_DECL( long double _Complex )
    503498
    504 
    505499// *********************************** time ***********************************
    506 
    507500
    508501#include <time_t.hfa>                                                                   // Duration (constructors) / Time (constructors)
Note: See TracChangeset for help on using the changeset viewer.