Thread: Manually reading a character literal

  1. #31
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,526
    Quote Originally Posted by awsdert View Post
    Just finished fixing the final known issue with my code, I'd like to have everyone whose willing do their own tests of it and give me feedback on what they see as missing since I just draw a blank when I try to think of even one example to test on. Here is a link to the tar.gz I made of the relevant files:
    char_literal_test.tar.gz - Google Drive
    As long as you're able to just run make & cc then typing "make char.run" in a terminal window opened in the same directory as these files should be enough to get the ball rolling
    Doesn't build for me.

    Code:
    $ ls
    char.c  makefile  next.c  next.h
    $ make
    make: *** No rule to make target 'expr.buildexe', needed by 'buildall'.  Stop.
    Trying manually:
    Code:
    $ gcc --std=c99 -Wall char.c next.c
    char.c:1:10: fatal error: bitsof.h: No such file or directory
        1 | #include "bitsof.h"
          |          ^~~~~~~~~~
    compilation terminated.
    In file included from next.c:1:
    next.h:3:10: fatal error: fail.h: No such file or directory
        3 | #include "fail.h"
          |          ^~~~~~~~
    compilation terminated.
    Too hard
    Last edited by Hodor; 09-30-2019 at 08:16 PM.

  2. #32
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    657
    Ah, forgot about those dependencies, I'll add them to the next zip, right now I gotta fix a segfault I somehow made happen while I was trying to cleanup the code

  3. #33
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    657
    Alright I fixed the segfault but now I'm somehow getting empty characters and I need to go to work now, here's an archive of the files:
    character_literal_test.tar.gz - Google Drive
    If you gonna take a look the functions to look for are automatic(), rdEscChr(), rdChar(), rdU64_base62(), utfnextc(), futfnextc() & nextc(), the u16 & u32 variants aren't used on my system so they are not tested for now.

  4. #34
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    657
    Post keeps getting deleted, nth try
    Quote Originally Posted by Hodor View Post
    Doesn't build for me.

    Code:
    $ ls
    char.c  makefile  next.c  next.h
    $ make
    make: *** No rule to make target 'expr.buildexe', needed by 'buildall'.  Stop.
    Btw I'm sure I mentioned this before but you were supposed to add the target "char.run" like so:
    Code:
    make char.run
    so that you only try to build the target I included the files for

  5. #35
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    657
    As it turns out that segfault wasn't fixed last time, I just managed to somehow skip the code that did segfault (for some reason or other I had been using an index that needed to be reset 1st, replaced that with a literal 0 as that was more appriate anyways), will work on string handling next before I upload the next one, will have to change some stuff while I'm at it to do that with my top handler (was named automatic but now renamed to literalc).

  6. #36
    Registered User
    Join Date
    Feb 2019
    Posts
    556
    Quote Originally Posted by awsdert View Post
    Post keeps getting deleted, nth try

    Btw I'm sure I mentioned this before but you were supposed to add the target "char.run" like so:
    Code:
    make char.run
    so that you only try to build the target I included the files for
    Why make such a complex makefile? You only need this:
    Code:
    # Define EXE_EXT and OBJ_EXT below if you are working with Windows
    #EXE_EXT=.exe
    #OBJ_EXT=obj
    
    # These are for Linux and UNIX alike
    EXE_EXT=
    OBJ_EXT=o
    
    #---
    #CC=gcc
    CFLAGS=-Og -g
    LD=$(CC)
    LDFLAGS=-g
    LIBS=
    
    char$(EXE_EXT): char.$(OBJ_EXT) next.$(OBJ_EXT)
        $(LD) $(LDFLAGS) -o $@ $^ $(LIBS)
    
    char.${OBJ_EXT}: char.c next.h fail.h bitsof.h
    next.${OBJ_EXT}: next.c next.h fail.h
    You'll see bitsof.h and fail.h are missing before the compilation starts...

  7. #37
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,526
    Under what circumstances does it segfault? Why is the char.run deleting the build files, that's annoying (made my own makefile instead)

    Also, char.c line 328 (the = now[j] line)

    Code:
            if ( !(max = utfclen( now[0] )) ) {
                c[i++] = now[j];
    I'm not sure if j is initialised

  8. #38
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,526
    Ok, to try and work out why it might be segfaulting I reviewed the code. I still can't get it to segfault and there are some parts of the code that I don't yet understand. Since I reviewed it I may as well offer some observations that I made, but apart from the seemingly uninitalised variable previously mentioned these observations would not cause a segfault and are not "errors" per se, but merely things I found odd or interesting.

    next.c
    Code:
    int fu16nextc( FILE *file, char8_t *c, size_t leng ) {
        int ret = open_std_encodings(); /* line 172 */
    ret is assigned the result of open_std_encodings() and then there's a few more lines, none of which seem to use 'ret', and then on line 177 there is 'ret = ERANGE', so I'm unsure what line 172 (the initial assignment to ret) is for. There is a similar thing done on line 208:

    Code:
    int fu32nextc( FILE *file, char8_t *c, size_t leng ) {
        int ret = open_std_encodings(); /* line 208 */
    // ...
       ret = ERANGE; /* line 212 */
    char.c
    Code:
    if ( sizeof(char32_t) & 1 ) abort(); /* line 254 */
    What does line 254 of char.c do? I am guessing it's making sure that sizeof(char32_t) is an even number but I don't know why. The C standard (section 7.28) says that it's the same as uint_least32_t so I guess it's possible that the type has an odd number of "bytes" but why would that matter? Dunno.

  9. #39
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    657
    Quote Originally Posted by Hodor View Post
    Under what circumstances does it segfault? Why is the char.run deleting the build files, that's annoying (made my own makefile instead)

    Also, char.c line 328 (the = now[j] line)

    Code:
            if ( !(max = utfclen( now[0] )) ) {
                c[i++] = now[j];
    I'm not sure if j is initialised
    Yeah I found that a while ago as you'll note by a previous post, replace it with 0 or just download the updated version of the files which includes string support now, character_literal_test.tar.gz - Google Drive

  10. #40
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    657
    Quote Originally Posted by flp1969 View Post
    Why make such a complex makefile? You only need this:
    Code:
    # Define EXE_EXT and OBJ_EXT below if you are working with Windows
    #EXE_EXT=.exe
    #OBJ_EXT=obj
    
    # These are for Linux and UNIX alike
    EXE_EXT=
    OBJ_EXT=o
    
    #---
    #CC=gcc
    CFLAGS=-Og -g
    LD=$(CC)
    LDFLAGS=-g
    LIBS=
    
    char$(EXE_EXT): char.$(OBJ_EXT) next.$(OBJ_EXT)
        $(LD) $(LDFLAGS) -o $@ $^ $(LIBS)
    
    char.${OBJ_EXT}: char.c next.h fail.h bitsof.h
    next.${OBJ_EXT}: next.c next.h fail.h
    You'll see bitsof.h and fail.h are missing before the compilation starts...
    Yeah I had it like that because those weren't the only files in my project folder, as for the removal it was to keep my folder clear of test binaries I don't need hanging around

  11. #41
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    657
    Quote Originally Posted by Hodor View Post
    Ok, to try and work out why it might be segfaulting I reviewed the code. I still can't get it to segfault and there are some parts of the code that I don't yet understand. Since I reviewed it I may as well offer some observations that I made, but apart from the seemingly uninitalised variable previously mentioned these observations would not cause a segfault and are not "errors" per se, but merely things I found odd or interesting.

    next.c
    Code:
    int fu16nextc( FILE *file, char8_t *c, size_t leng ) {
        int ret = open_std_encodings(); /* line 172 */
    ret is assigned the result of open_std_encodings() and then there's a few more lines, none of which seem to use 'ret', and then on line 177 there is 'ret = ERANGE', so I'm unsure what line 172 (the initial assignment to ret) is for. There is a similar thing done on line 208:

    Code:
    int fu32nextc( FILE *file, char8_t *c, size_t leng ) {
        int ret = open_std_encodings(); /* line 208 */
    // ...
       ret = ERANGE; /* line 212 */
    char.c
    Code:
    if ( sizeof(char32_t) & 1 ) abort(); /* line 254 */
    What does line 254 of char.c do? I am guessing it's making sure that sizeof(char32_t) is an even number but I don't know why. The C standard (section 7.28) says that it's the same as uint_least32_t so I guess it's possible that the type has an odd number of "bytes" but why would that matter? Dunno.
    ret is just meant to catch the return code of functions or reduce replacements if I decide the code is no longer appropriate (or just copy paste & change), if an error occurred in a function it is detected via this then pass back further down the line for the caller to handle (assuming the current function doesn't handl it itself)

    as for the sizeof(char32_t) & 1 I was thinking about obscure scenarios where it had taken just 1 part of a UTF-8 character or something else that just isn't supported, this would at least point out (albeit rather harshly) to the user that they need to add the support themselves, not that it really matters anymore since I found it easier to rip out the char32_t stuff (aside from converting to/from when reading/writing the stuff) and just use short char8_t strings, there is only one scenario I can find thus far where not even char8_t is available and it is very unlikely this would be used in such a scenario anyway.

  12. #42
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    657
    Realised I forgot to reverse multi-byte characters, fixed in my own code but will have to verify how UTF is handled before declaring it 100% done, in mean time I gotta go to work, since the RTL/LTR thing is not a show stopper I'll leave it until tonight or another day

  13. #43
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,526
    next.c (lines 175 and 210) seem to be a typo

    Code:
    if ( !file || !c ) {
            if ( c && size) (void*)memset( c, 0, size ); /* line 175 */
    memset() already returns a void* so the cast is... kinda silly. Maybe it's meant to be (void). Same with line 210

    I still can't get it to segfault, though

  14. #44
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,526
    I like your avatar, by the way. I loved The Book of Unwritten Tales (very funny), but found the sequels less funny

    Edit: My avatar, on the other hand, is boring. It's not even moving and just an optical illusion :/ stupid

  15. #45
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    657
    Quote Originally Posted by Hodor View Post
    next.c (lines 175 and 210) seem to be a typo

    Code:
    if ( !file || !c ) {
            if ( c && size) (void*)memset( c, 0, size ); /* line 175 */
    memset() already returns a void* so the cast is... kinda silly. Maybe it's meant to be (void). Same with line 210

    I still can't get it to segfault, though
    Yeah that was a typo, thx for pointing it out to me, as for the avatar thing *breaths a big breath* YAAAAAAAY SOMEONE GOT THE REFERENCE! XD, sidenote I actually picked that avatar since it suits me to a T, intelligent yet stupid, also wow did not know that was an optical illusion, cool. Btw what sequal? I know of the original plus the prequel but didn't see any sequels

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Manually reading an integer literal
    By awsdert in forum C Programming
    Replies: 17
    Last Post: 09-04-2019, 03:36 PM
  2. Problems reading in character
    By jjohan in forum C Programming
    Replies: 8
    Last Post: 09-11-2014, 01:45 AM
  3. vsprintf ommit literal percent character
    By Niara in forum C Programming
    Replies: 3
    Last Post: 03-05-2012, 04:34 PM
  4. How to keep reading the same character in a file
    By nndhawan in forum C Programming
    Replies: 5
    Last Post: 04-04-2011, 05:53 PM
  5. reading a character
    By Calavera in forum C Programming
    Replies: 6
    Last Post: 11-23-2004, 12:25 PM

Tags for this Thread