<commit_before>#include "GBase.h"
#include "GArgs.h"
#include <ctype.h>

GArgs::GArgs(int argc, char* argv[], const char* format, bool nodigitopts) {
   /* format can be:
      <string>{;|=} e.g. disable-test;PID=S= for --disable-test PID=50 (or --PID 50) S=3.5 etc.
      <letter>[:]  e.g. p:hT  for -p testing (or -ptesting) -h -T
   */
const char* fstr=format;
fmtcount=0;
count=0;
nonOptCount=0;
nonOptPos=0;
optPos=0;
errarg=0;
err_valmissing=false;
args=NULL;
fmt=NULL;
_argc=argc;
_argv=argv;
int fmtlen=strlen(format);
//---- first parse the format string
while (fstr-format < fmtlen ) {
  int l=strcspn(fstr, ";=:");
  if (fstr[l]==0) { //end of string reached
      //all previous chars are just switches:
       GREALLOC(fmt, (fmtcount+l)*sizeof(fmtdef));
       //store each switch
       for (int i=0; i<l;i++) { 
         fmt[fmtcount+i].longopt=NULL;
         fmt[fmtcount+i].opt=fstr[i];
         fmt[fmtcount+i].req_value = false;
         fmt[fmtcount+i].code=fmtcount+i+1;
         }
       fmtcount+=l;
       break;
     }
   else {
     if (fstr[l]==':') {
         //fstr[l-1] is an argument, but all the previous are just switches
         GREALLOC(fmt, (fmtcount+l)*sizeof(fmtdef));
         //store each switch AND the option
         for (int i=0; i<l;i++) { 
           fmt[fmtcount+i].longopt=NULL; //one char length
           fmt[fmtcount+i].opt=fstr[i];
           fmt[fmtcount+i].req_value = (i==l-1);
           fmt[fmtcount+i].code=fmtcount+i+1;
           }
         fmtcount+=l;
         }
      else { // fstr[l]=='=' or ';' 
         GREALLOC(fmt, (fmtcount+1)*sizeof(fmtdef));
         fmt[fmtcount].longopt=Gstrdup(fstr, fstr+l-1);
         fmt[fmtcount].opt=0;
         fmt[fmtcount].req_value=(fstr[l]=='=');
         fmt[fmtcount].code=fmtcount+1;
         fmtcount++;
         }
     fstr+=l+1;
     }
  }
 //-- now parse the arguments based on the given format specification
 parseArgs(nodigitopts);
 }

void dbg_dequote(char* &p) {
   int alen=strlen(p);
   if (alen>1 && p[0]=='\'' && p[alen-1]=='\'') {
	 p++;
	 p[alen-2 ]='\0';
   }
}

int GArgs::parseArgs(bool nodigitopts) {
  int p=1; //skip program name
  int f=0;
  while (p<_argc) {
   // silly patch for annnoying MacOS gdb/eclipse issues:
   #if defined(__APPLE__) && defined(DEBUG)
	   dbg_dequote(_argv[p]);
   #endif
   //--
   if (_argv[p][0]=='-' && (_argv[p][1]==0 || _argv[p][1]!='-')) { 
     //single-dash argument
     int cpos=1;
     char c=_argv[p][cpos];
     if (c==0 || (nodigitopts && isdigit(c)) || 
            (c=='.' && isdigit(_argv[p][cpos+1]))) { 
        //special case: plain argument '-' or just a negative number
        GREALLOC(args, (count+1)*sizeof(argdata));
        args[count].opt=NULL;
        args[count].fmti=-1;
        if (c==0) {
          GCALLOC(args[count].value, 2);
          args[count].value[0]='-';
          }
         else { //negative number given
          args[count].value=Gstrdup(_argv[p]);
          }
        count++;
        nonOptCount++;
        }
      else { //single-dash argument or switch
       COLLAPSED:
        if ((f=validShortOpt(c))>=0) {
          GREALLOC(args, (count+1)*sizeof(argdata));
          GCALLOC(args[count].opt, 2);
          args[count].opt[0]=c;
          args[count].fmti=f;
          if (!fmt[f].req_value) {//switch type
            GCALLOC(args[count].value,1);//so getOpt() functions would not return NULL
            count++;
            // only switches can be grouped with some other switches or options
            if (_argv[p][cpos+1]!='\0') {
               cpos++;
               c=_argv[p][cpos];
               goto COLLAPSED;
               }
            }
           else {
              //single-dash argument followed by a value
            if (_argv[p][cpos+1]=='\0') {
                if (p+1<_argc && _argv[p+1][0]!=0) { //value is the whole next argument
                   p++;
				#if defined(__APPLE__) && defined(DEBUG)
				   dbg_dequote(_argv[p]);
				#endif
                   args[count].value=Gstrdup(_argv[p]);
                }
                  else {
                   errarg=p;
                   err_valmissing=true;
                   return errarg;
                   }
                }
               else { //value immediately follows the dash-option
                args[count].value=Gstrdup(_argv[p]+cpos+1);
                }
            count++;
            }
          } //was validShortOpt
         else { //option not found in format definition!
           errarg=p;
           return errarg;
           }
        }
     } //-single-dash
   else {//not a single-dash argument
     char* ap=_argv[p];
     bool is_longopt=false;
     if (*ap=='-' && ap[1]=='-') { //double-dash option
        is_longopt=true;
        ap+=2;
        }
     char* e=strchr(ap+1,'=');
     while (e!=NULL && *(e-1)=='\\') e=strchr(e,'=');
     if (e==NULL && is_longopt) {
        e=ap;
        while (*e!=0 && *e!=' ') e++;
        //e will be on eos or next space
        }
     if (e!=NULL && e>ap) {
       //this must be a long option
       //e is on eos, space or '='
       if ((f=validLongOpt(ap,e-1))>=0) {
            GREALLOC(args, (count+1)*sizeof(argdata));
            args[count].opt=Gstrdup(ap,e-1);
            args[count].fmti=f;
            if (fmt[f].req_value) {
               if (*e==0) {
                   //value is the next argument
                   if (p+1<_argc && _argv[p+1][0]!=0) {
                      p++;
                      args[count].value=Gstrdup(_argv[p]);
                      }
                    else {
                      errarg=p;
                      err_valmissing=true;
                      return errarg;
                      }<commit_msg>fix gargs<commit_after>#include "GBase.h"
#include "GArgs.h"
#include <ctype.h>

GArgs::GArgs(int argc, char* argv[], const char* format, bool nodigitopts) {
   /* format can be:
      <string>{;|=} e.g. disable-test;PID=S= for --disable-test PID=50 (or --PID 50) S=3.5 etc.
      <letter>[:]  e.g. p:hT  for -p testing (or -ptesting) -h -T
   */
const char* fstr=format;
fmtcount=0;
count=0;
nonOptCount=0;
nonOptPos=0;
optPos=0;
errarg=0;
err_valmissing=false;
args=NULL;
fmt=NULL;
_argc=argc;
_argv=argv;
int fmtlen=strlen(format);
//---- first parse the format string
while (fstr-format < fmtlen ) {
  int l=strcspn(fstr, ";=:");
  if (fstr[l]==0) { //end of string reached
      //all previous chars are just switches:
       GREALLOC(fmt, (fmtcount+l)*sizeof(fmtdef));
       //store each switch
       for (int i=0; i<l;i++) { 
         fmt[fmtcount+i].longopt=NULL;
         fmt[fmtcount+i].opt=fstr[i];
         fmt[fmtcount+i].req_value = false;
         fmt[fmtcount+i].code=fmtcount+i+1;
         }
       fmtcount+=l;
       break;
     }
   else {
     if (fstr[l]==':') {
         //fstr[l-1] is an argument, but all the previous are just switches
         GREALLOC(fmt, (fmtcount+l)*sizeof(fmtdef));
         //store each switch AND the option
         for (int i=0; i<l;i++) { 
           fmt[fmtcount+i].longopt=NULL; //one char length
           fmt[fmtcount+i].opt=fstr[i];
           fmt[fmtcount+i].req_value = (i==l-1);
           fmt[fmtcount+i].code=fmtcount+i+1;
           }
         fmtcount+=l;
         }
      else { // fstr[l]=='=' or ';' 
         GREALLOC(fmt, (fmtcount+1)*sizeof(fmtdef));
         fmt[fmtcount].longopt=Gstrdup(fstr, fstr+l-1);
         fmt[fmtcount].opt=0;
         fmt[fmtcount].req_value=(fstr[l]=='=');
         fmt[fmtcount].code=fmtcount+1;
         fmtcount++;
         }
     fstr+=l+1;
     }
  }
 //-- now parse the arguments based on the given format specification
 parseArgs(nodigitopts);
 }

void dbg_dequote(char* &p) {
   int alen=strlen(p);
   if (alen>1 && p[0]=='\'' && p[alen-1]=='\'') {
	 p++;
	 p[alen-2 ]='\0';
   }
}

int GArgs::parseArgs(bool nodigitopts) {
  int p=1; //skip program name
  int f=0;
  while (p<_argc) {
   // silly patch for annnoying MacOS gdb/eclipse issues:
   #if defined(__APPLE__) && defined(DEBUG)
	   dbg_dequote(_argv[p]);
   #endif
   //--
   if (_argv[p][0]=='-' && (_argv[p][1]==0 || _argv[p][1]!='-')) { 
     //single-dash argument
     int cpos=1;
     char c=_argv[p][cpos];
     if (c==0 || (nodigitopts && isdigit(c)) || 
            (c=='.' && isdigit(_argv[p][cpos+1]))) { 
        //special case: plain argument '-' or just a negative number
        GREALLOC(args, (count+1)*sizeof(argdata));
        args[count].opt=NULL;
        args[count].fmti=-1;
        if (c==0) {
          GCALLOC(args[count].value, 2);
          args[count].value[0]='-';
          }
         else { //negative number given
          args[count].value=Gstrdup(_argv[p]);
          }
        count++;
        nonOptCount++;
        }
      else { //single-dash argument or switch
       COLLAPSED:
        if ((f=validShortOpt(c))>=0) {
          GREALLOC(args, (count+1)*sizeof(argdata));
          GCALLOC(args[count].opt, 2);
          args[count].opt[0]=c;
          args[count].fmti=f;
          if (!fmt[f].req_value) {//switch type
            GCALLOC(args[count].value,1);//so getOpt() functions would not return NULL
            count++;
            // only switches can be grouped with some other switches or options
            if (_argv[p][cpos+1]!='\0') {
               cpos++;
               c=_argv[p][cpos];
               goto COLLAPSED;
               }
            }
           else {
              //single-dash argument followed by a value
            if (_argv[p][cpos+1]=='\0') {
                if (p+1<_argc && _argv[p+1][0]!=0) { //value is the whole next argument
                   p++;
				#if defined(__APPLE__) && defined(DEBUG)
				   dbg_dequote(_argv[p]);
				#endif
                   args[count].value=Gstrdup(_argv[p]);
                }
                  else {
                   errarg=p;
                   err_valmissing=true;
                   return errarg;
                   }
                }
               else { //value immediately follows the dash-option
                args[count].value=Gstrdup(_argv[p]+cpos+1);
                }
            count++;
            }
          } //was validShortOpt
         else { //option not found in format definition!
           errarg=p;
           return errarg;
           }
        }
     } //-single-dash
   else {//not a single-dash argument
     char* ap=_argv[p];
     bool is_longopt=false;
     if (*ap=='-' && ap[1]=='-') { //double-dash option
        is_longopt=true;
        ap+=2;
        }
     char* e=strchr(ap+1,'=');
     while (e!=NULL && *(e-1)=='\\') e=strchr(e,'=');
     if (e==NULL && is_longopt) {
        e=ap;
        while (*e!=0 && *e!=' ') e++;
        //e will be on eos or next space
        }
     if (e!=NULL && e>ap) {
       //this must be a long option
       //e is on eos, space or '='
       if ((f=validLongOpt(ap,e-1))>=0) {
            GREALLOC(args, (count+1)*sizeof(argdata));
            args[count].opt=Gstrdup(ap,e-1);
            args[count].fmti=f;
            if (fmt[f].req_value) {
               if (*e==0) {
                   //value is the next argument
                   if (p+1<_argc && _argv[p+1][0]!=0) {
                      p++;
					#if defined(__APPLE__) && defined(DEBUG)
                      dbg_dequote(_argv[p]);
   	   	   	   	   	#endif
                      args[count].value=Gstrdup(_argv[p]);
                      }
                    else {
                      errarg=p;
                      err_valmissing=true;
                      return errarg;
                      }
{'fraction_non_alphanumeric': 0.16807811959903216, 'fraction_numerical': 0.012270998963014172, 'mean_word_length': 1.7033403410418126, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6837592', 'n_tokens_mistral': 4053, 'n_tokens_neox': 3909, 'n_words': 806}