Removing ancient style declaration from XBoard

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

User avatar
Look
Posts: 364
Joined: Thu Jun 05, 2014 2:14 pm
Location: Iran
Full name: Mehdi Amini

Removing ancient style declaration from XBoard

Post by Look »

Hi,

I want to run this code on XBoard source files. Note that it should be run in each sub-folder in XBoard since that seems safer to me. Also this works when there is a single line to be improved upon. Rest of correcting should be manual. Here is code so far :

Code: Select all

// This code corrects a P((args)) to (args) , single line in XBoard project

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <ctype.h>
#include <stdnoreturn.h>

#include <unistd.h>
#include <limits.h>

#include <pthread.h>
#include <glib.h>
#include <glib/gstdio.h>
#include <glib/gdir.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
//#include <gio/gio.h>

#define STR_SIZE 256 // 512
#define MY_FN "my_temp.tmp" // tmp text
bool extension(gchar *fn, const char c);
bool ccode(gchar *fn);
void copy_file(const char* const from , char * to);
void restyle(const char * const line_str , char *rsl);
noreturn void err_exit(char * s);

int main(void)
{
    FILE * my_tempfile;

    gchar fn[128]={'\0'};
    gchar *  fn_ptr=&fn[0]; // const

    char line_str[STR_SIZE]={'\0'};
    FILE *sf=NULL;

    gchar cwd[256]={'\0'};
    gchar * cwd_ptr;

    GDir *dir_ptr;

    cwd_ptr=strcpy(&cwd[0],g_get_current_dir() );

    if( cwd_ptr==NULL)
        perror("Bad dir.\n");
    g_printf("cwd is %s \n\n",cwd);

    dir_ptr=g_dir_open(cwd,0,NULL);
    if(dir_ptr==NULL)
        perror("dir err.\n");

    while( (fn_ptr=g_dir_read_name(dir_ptr)) !=NULL) // read all files
    {
        g_print(">fn is %s\n",fn_ptr);

        if(ccode(fn_ptr))
        {
            my_tempfile=fopen(MY_FN,"wt");

            printf(">>ccode is %s\n",fn_ptr);
            sf=fopen(fn_ptr,"rt");
            int l=0;

            while(fgets(line_str,STR_SIZE-1,sf) !=NULL) // read all lines in sf
            {
                l++;

                char  rsl[STR_SIZE];
                restyle(line_str, rsl);

                fprintf(my_tempfile, "%s",rsl);

            }

            fclose(sf);
            fclose(my_tempfile);
            copy_file(MY_FN,fn_ptr);// "test.h"
            g_print(">>>file %s was updated.\n\n",fn_ptr);
        }
    }

    return EXIT_SUCCESS;
}

bool ccode(gchar *fn)
{
    bool r=false;
    size_t sl=strlen(fn);
    if(strstr(fn,".c")!=NULL && isalpha(fn[0]) && extension(fn,'c'))
        return true;
    if(strstr(fn,".h")!=NULL && isalpha(fn[0]) && extension(fn,'h'))
        return true;

    return r;
}

bool extension(gchar *fn, const char c)
{
    size_t sl=strlen(fn);
    assert(sl>=3);

    if(fn[sl]=='\0' && fn[sl-1]==c && fn[sl-2]=='.')
        return true;

    return false;
}


void copy_file(const char* const from , char *to)
{
    FILE *f1;
    FILE *f2;
    int c; // consider EOF

    assert(from != to);
    assert(from != NULL);
    assert(to != NULL);

    f1 = fopen(from, "rt");
    if(f1==NULL)
        err_exit("opening f1");

    f2 = fopen(to, "wt");

    if(f2==NULL)
        err_exit("opening f2");

    rewind(f1);
    rewind(f2);

    c = fgetc(f1);
    if(c==EOF)
        err_exit("first char in f1");
    while(c != EOF) // !feof(f1)
    {
        fputc(c, f2);
        c = fgetc(f1);
    }

    fclose(f1);
    fclose(f2);

    return;
}

void restyle(const char * const line_str , char *rsl)
{
    char * old_open=strstr(line_str,"P((");
    char * old_close= strstr(line_str,"))"); // needs a recompute

    strncpy(&rsl[0], &line_str[0], STR_SIZE-1);

    size_t sl=strlen(line_str);
    assert(sl<STR_SIZE);

    if(old_open!=NULL && old_close!=NULL ) // line needs updating
    {

        assert(old_open<old_close);
        assert(old_open>line_str);
        assert(old_close>line_str);
        assert(old_open<line_str+sl);
        assert(old_close<line_str+sl);

        int n;

        old_open=strstr(line_str,"P((");
        assert(old_open!=NULL);

        char rslt1[STR_SIZE]={'\0'};
        strncpy(&rslt1[0], &line_str[0], STR_SIZE-1);
        for(n=0; line_str[n]!='\0'&& line_str[n+1]!='\0'&&n<512; n++)
            if(line_str+n >= old_open) // TODO why not rsl+n ?
            {
                rsl[n]=rslt1[n+2]; // TODO n-1(or n) till n+2 can be tried
            }

        old_close= strstr(rsl,"))");
        assert(old_close!=NULL);
        char rslt[STR_SIZE]={'\0'};
        strncpy(&rslt[0], &rsl[0], STR_SIZE-1);

        for(n=0; rsl[n]!='\0' && n<512; n++)
            if(rsl+n >= old_close)
            {
                rsl[n]=rslt[n+1];
            }

    }

    return ;
}

noreturn void err_exit(char * s)
{
    perror(s);
    exit(EXIT_FAILURE);
}

Farewell.
Dann Corbit
Posts: 12537
Joined: Wed Mar 08, 2006 8:57 pm
Location: Redmond, WA USA

Re: Removing ancient style declaration from XBoard

Post by Dann Corbit »

If the symbol is set, the macro does that for you
Taking ideas is not a vice, it is a virtue. We have another word for this. It is called learning.
But sharing ideas is an even greater virtue. We have another word for this. It is called teaching.
User avatar
Look
Posts: 364
Joined: Thu Jun 05, 2014 2:14 pm
Location: Iran
Full name: Mehdi Amini

Re: Removing ancient style declaration from XBoard

Post by Look »

Dann Corbit wrote: Thu Nov 28, 2019 5:53 pm If the symbol is set, the macro does that for you
Reading and understanding code with standard C is easier than these obsolete declarations.
Farewell.
User avatar
hgm
Posts: 27788
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: Removing ancient style declaration from XBoard

Post by hgm »

I don't see the point. It never bothered me to have an extra P() around the argument list of function prototypes. It is not very useful to look at the prototypes anyway; they would not tell you what the function does. They mainly reside in .h header files that are #included to enforce consistency between the definition and the call, and usually it is good enough to take it for granted they will be there. When I see a call of a function I don't know I won't be looking for its prototype; I would be looking for its declaration.