// THIS METHOD IS OF TIME COMPLEXITY O(ALen + BLen) . Hence this is preferred struct transaction * sortedArraysCommonElements(struct transaction *A, int ALen, struct transaction *B, int BLen) { int i = 0, j = 0, date, len = 0; struct transaction *result = NULL; if (A == NULL || B == NULL || ALen == NULL || BLen == NULL) return NULL; while (i < ALen && j < BLen) { date = date_compare(A[i].date, B[j].date); if (date == 1) j++; else if (date == 2) i++; // If transactions are high, (> 1000), we can increment i, j in steps of powers of 2. // Here, this is enough as there are less transactions else { if (A[i].amount == B[j].amount && stringcompare(A[i].description, B[j].description)) { len++; result = (struct transaction*)realloc(result, len*sizeof(struct transaction)); result[len - 1].amount = A[i].amount; string_copy(result[len - 1].date, A[i].date); string_copy(result[len - 1].description, A[i].description); } // For the given testcases and question , this else is not needed.(special case) else { len++; result = (struct transaction*)realloc(result, len*sizeof(struct transaction)); result[len - 1].amount = A[i].amount; string_copy(result[len - 1].date, A[i].date); string_copy(result[len - 1].description, A[i].description); len++; result = (struct transaction*)realloc(result, len*sizeof(struct transaction)); result[len - 1].amount = B[j].amount; string_copy(result[len - 1].date, B[j].date); string_copy(result[len - 1].description, B[j].description); } i++; j++; } } return result; }
static HtmlAttribKey html_lookup_length_attrib_key(const char *string, size_t length) { int i, imin = 0, imax = HTML_ATTRIB_KEYS, res; while(imax >= imin) { i = (imax - imin)/2 + imin; if (i==HTML_ATTRIB_KEYS) break; res = stringcompare(string, html_attrib[i], length); if(res < 0) imax = i - 1; else if(res > 0) imin = i + 1; else return i; } return HTML_ATTRIB_UNKNOWN; }
HtmlTag html_lookup_tag(const char *string) { int i, imin = 0, imax = HTML_TAGS, res; while(imax >= imin) { i = (imax - imin)/2 + imin; if (i==HTML_TAGS) break; res = stringcompare(string, html_tag[i], ~0); if(res < 0) imax = i - 1; else if(res > 0) imin = i + 1; else return i; } return HTML_TAG_UNKNOWN; }
static HtmlTag html_lookup_length_tag(const char *string, size_t length) { //TODO: optimize string compare for binary tree search (no "restarts") int i, imin = 0, imax = HTML_TAGS, res; while(imax >= imin) { i = (imax - imin)/2 + imin; if (i==HTML_TAGS) break; res = stringcompare(string, html_tag[i], length); if(res < 0) imax = i - 1; else if(res > 0) imin = i + 1; else return i; } return HTML_TAG_UNKNOWN; }
void menufunction(int) { switch(stringmenu) { case 1: stringcopy(); break; case 2: stringcharcopy(); break; case 3: stringcompare(); break; case 4: stringcharcompare(); break; case 5: stringmerge(); break; case 6: stringcharmerge(); break; case 7: stringlength(); break; default: cout<<"This is not a valid option! \n"; cout<<"Choose again! \n"; break; } }
/* The argument has been expressed in a long-form, i.e. prefixed * by -- * Decode and act on the argument. * The associated_value will only be required by some arguments. * Return whether one or both were required. */ int process_long_form_argument(const char *argument, const char *associated_value) { if(stringcompare(argument, "addhashcode") == 0) { GlobalState.add_hashcode_tag = TRUE; return 1; } else if(stringcompare(argument, "append") == 0) { process_argument(APPEND_TO_OUTPUT_FILE_ARGUMENT, associated_value); return 2; } else if(stringcompare(argument, "checkfile") == 0) { process_argument(CHECK_FILE_ARGUMENT, associated_value); return 2; } else if(stringcompare(argument, "checkmate") == 0) { process_argument(MATCH_CHECKMATE_ARGUMENT, ""); return 1; } else if(stringcompare(argument, "duplicates") == 0) { process_argument(DUPLICATES_FILE_ARGUMENT, associated_value); return 2; } else if(stringcompare(argument, "evaluation") == 0) { /* Output an evaluation is required with each move. */ GlobalState.output_evaluation = TRUE; return 1; } else if(stringcompare(argument, "fencomments") == 0) { /* Output an evaluation is required with each move. */ GlobalState.add_FEN_comments = TRUE; /* Turn off any separate setting of output_FEN_comment. */ GlobalState.output_FEN_string = FALSE; return 1; } else if(stringcompare(argument, "fuzzydepth") == 0) { /* Extract the depth. */ int depth = 0; if(sscanf(associated_value, "%d",&depth) == 1){ if(depth >= 0) { GlobalState.fuzzy_match_duplicates = TRUE; GlobalState.fuzzy_match_depth = depth; } else { fprintf(GlobalState.logfile, "--%s requires a number greater than or equal to zero.\n", argument); exit(1); } } else { fprintf(GlobalState.logfile, "--%s requires a number following it.\n", argument); exit(1); } return 2; } else if(stringcompare(argument, "help") == 0) { process_argument(HELP_ARGUMENT, ""); return 1; } else if(stringcompare(argument, "keepbroken") == 0) { GlobalState.keep_broken_games = TRUE; return 1; } else if(stringcompare(argument, "linelength") == 0) { process_argument(LINE_WIDTH_ARGUMENT, associated_value); return 2; } else if(stringcompare(argument, "markmatches") == 0) { if(*associated_value != '\0') { GlobalState.add_position_match_comments = TRUE; GlobalState.position_match_comment = copy_string(associated_value); } else { fprintf(GlobalState.logfile, "--markmatches requires a comment string following it.\n"); exit(1); } return 2; } else if(stringcompare(argument, "nochecks") == 0) { GlobalState.keep_checks = FALSE; return 1; } else if(stringcompare(argument, "nocomments") == 0) { process_argument(DONT_KEEP_COMMENTS_ARGUMENT, ""); return 1; } else if(stringcompare(argument, "noduplicates") == 0) { process_argument(DONT_KEEP_DUPLICATES_ARGUMENT, ""); return 1; } else if(stringcompare(argument, "nofauxep") == 0) { GlobalState.suppress_redundant_ep_info = TRUE; return 1; } else if(stringcompare(argument, "nomovenumbers") == 0) { GlobalState.keep_move_numbers = FALSE; return 1; } else if(stringcompare(argument, "nonags") == 0) { process_argument(DONT_KEEP_NAGS_ARGUMENT, ""); return 1; } else if(stringcompare(argument, "noresults") == 0) { GlobalState.keep_results = FALSE; return 1; } else if(stringcompare(argument, "notags") == 0) { if(GlobalState.tag_output_format == ALL_TAGS || GlobalState.tag_output_format == NO_TAGS) { GlobalState.tag_output_format = NO_TAGS; } else { fprintf(GlobalState.logfile, "--notags clashes with another argument.\n"); exit(1); } return 1; } else if(stringcompare(argument, "nounique") == 0) { process_argument(SUPPRESS_ORIGINALS_ARGUMENT, ""); return 1; } else if(stringcompare(argument, "novars") == 0) { process_argument(DONT_KEEP_VARIATIONS_ARGUMENT, ""); return 1; } else if(stringcompare(argument, "selectonly") == 0) { unsigned long selection = 0; /* Extract the selected match number. */ if(sscanf(associated_value, "%lu",&selection) == 1){ if(selection > 0) { GlobalState.matching_game_number = selection; } else { fprintf(GlobalState.logfile, "--%s requires a number greater than zero.\n", argument); exit(1); } } else { fprintf(GlobalState.logfile, "--%s requires a number greater than zero following it.\n", argument); exit(1); } return 2; } else if(stringcompare(argument, "output") == 0) { process_argument(WRITE_TO_OUTPUT_FILE_ARGUMENT, associated_value); return 2; } else if(stringcompare(argument, "plylimit") == 0) { int limit = 0; /* Extract the limit. */ if(sscanf(associated_value, "%d",&limit) == 1){ if(limit >= 0) { GlobalState.output_ply_limit = limit; } else { fprintf(GlobalState.logfile, "--%s requires a number greater than or equal to zero.\n", argument); exit(1); } } else { fprintf(GlobalState.logfile, "--%s requires a number following it.\n", argument); exit(1); } return 2; } else if(stringcompare(argument, "seven") == 0) { process_argument(SEVEN_TAG_ROSTER_ARGUMENT, ""); return 1; } else if(stringcompare(argument, "stalemate") == 0) { GlobalState.match_only_stalemate = TRUE; return 1; } else if(stringcompare(argument, "totalplycount") == 0) { GlobalState.output_total_plycount = TRUE; return 1; } else if(stringcompare(argument, "version") == 0) { fprintf(GlobalState.logfile, "pgn-extract %s\n", CURRENT_VERSION); exit(0); return 1; } else { fprintf(GlobalState.logfile, "Unrecognised long-form argument: --%s\n", argument); exit(1); return 1; } }
/* Process the argument character and its associated value. * This function processes arguments from the command line and * from an argument file associated with the -A argument. * * An argument -ofile.pgn would be passed in as: * 'o' and "file.pgn". * A zero-length string for associated_value is not necessarily * an error, e.g. -e has an optional following filename. * NB: If the associated_value is to be used beyond this function, * it must be copied. */ void process_argument(char arg_letter,const char *associated_value) { /* Provide an alias for associated_value because it will * often represent a file name. */ const char *filename = skip_leading_spaces(associated_value); switch(arg_letter){ case WRITE_TO_OUTPUT_FILE_ARGUMENT: case APPEND_TO_OUTPUT_FILE_ARGUMENT: if(GlobalState.ECO_level > 0){ fprintf(GlobalState.logfile,"-%c conflicts with -E\n", arg_letter); } else if(GlobalState.games_per_file > 0){ fprintf(GlobalState.logfile,"-%c conflicts with -#\n", arg_letter); } else if(GlobalState.output_filename != NULL){ fprintf(GlobalState.logfile, "-%c: File %s has already been selected for output.\n", arg_letter,GlobalState.output_filename); exit(1); } else if(*filename == '\0'){ fprintf(GlobalState.logfile,"Usage: -%cfilename.\n",arg_letter); exit(1); } else{ if(GlobalState.outputfile != NULL){ (void) fclose(GlobalState.outputfile); } if(arg_letter == WRITE_TO_OUTPUT_FILE_ARGUMENT){ GlobalState.outputfile = must_open_file(filename,"w"); } else{ GlobalState.outputfile = must_open_file(filename,"a"); } GlobalState.output_filename = filename; } break; case WRITE_TO_LOG_FILE_ARGUMENT: case APPEND_TO_LOG_FILE_ARGUMENT: /* Take precautions against multiple log files. */ if((GlobalState.logfile != stderr) && (GlobalState.logfile != NULL)){ (void) fclose(GlobalState.logfile); } if(arg_letter == WRITE_TO_LOG_FILE_ARGUMENT){ GlobalState.logfile = fopen(filename,"w"); } else{ GlobalState.logfile = fopen(filename,"a"); } if(GlobalState.logfile == NULL){ fprintf(stderr,"Unable to open %s for writing.\n",filename); GlobalState.logfile = stderr; } break; case DUPLICATES_FILE_ARGUMENT: if(*filename == '\0'){ fprintf(GlobalState.logfile,"Usage: -%cfilename.\n",arg_letter); exit(1); } else if(GlobalState.suppress_duplicates){ fprintf(GlobalState.logfile, "-%c clashes with the -%c flag.\n",arg_letter, DONT_KEEP_DUPLICATES_ARGUMENT); exit(1); } else{ GlobalState.duplicate_file = must_open_file(filename,"w"); } break; case USE_ECO_FILE_ARGUMENT: GlobalState.add_ECO = TRUE; if(*filename != '\0'){ GlobalState.eco_file = copy_string(filename); } else if((filename = getenv("ECO_FILE")) != NULL){ GlobalState.eco_file = filename; } else{ /* Use the default which is already set up. */ } initEcoTable(); break; case ECO_OUTPUT_LEVEL_ARGUMENT: { unsigned level; if(GlobalState.output_filename != NULL){ fprintf(GlobalState.logfile, "-%c: File %s has already been selected for output.\n", arg_letter, GlobalState.output_filename); exit(1); } else if(GlobalState.games_per_file > 0){ fprintf(GlobalState.logfile, "-%c conflicts with -#.\n", arg_letter); exit(1); } else if(sscanf(associated_value,"%u",&level) != 1){ fprintf(GlobalState.logfile, "-%c requires a number attached, e.g., -%c1.\n", arg_letter,arg_letter); exit(1); } else if((level < MIN_ECO_LEVEL) || (level > MAX_ECO_LEVEL)){ fprintf(GlobalState.logfile, "-%c level should be between %u and %u.\n", MIN_ECO_LEVEL,MAX_ECO_LEVEL,arg_letter); exit(1); } else{ GlobalState.ECO_level = level; } } break; case CHECK_FILE_ARGUMENT: if(*filename != '\0'){ /* See if it is a single PGN file, or a list * of files. */ size_t len = strlen(filename); /* Check for a .PGN suffix. */ const char *suffix = output_file_suffix(SAN); if((len > strlen(suffix)) && (stringcompare(&filename[len-strlen(suffix)], suffix) == 0)){ add_filename_to_source_list(filename,CHECKFILE); } else{ FILE *fp = must_open_file(filename,"r"); add_filename_list_from_file(fp,CHECKFILE); (void) fclose(fp); } } break; case FILE_OF_FILES_ARGUMENT: if(*filename != '\0'){ FILE *fp = must_open_file(filename,"r"); add_filename_list_from_file(fp,NORMALFILE); (void) fclose(fp); } else{ fprintf(GlobalState.logfile,"Filename expected with -%c\n", arg_letter); } break; case BOUNDS_ARGUMENT: { /* Bounds on the number of moves are to be found. * "l#" means less-than-or-equal-to. * "g#" means greater-than-or-equal-to. * Otherwise "#" (or "e#") means that number. */ /* Equal by default. */ char which = 'e'; unsigned value; Boolean Ok = TRUE; const char *bound = associated_value; switch(*bound){ case 'l': case 'u': case 'e': which = *bound; bound++; break; default: if(!isdigit((int) *bound)){ fprintf(GlobalState.logfile, "-%c must be followed by e, l, or u.\n", arg_letter); Ok = FALSE; } break; } if(Ok && (sscanf(bound,"%u",&value) == 1)){ GlobalState.check_move_bounds = TRUE; switch(which){ case 'e': GlobalState.lower_move_bound = value; GlobalState.upper_move_bound = value; break; case 'l': if(value <= GlobalState.upper_move_bound){ GlobalState.lower_move_bound = value; } else{ fprintf(GlobalState.logfile, "Lower bound is greater than the upper bound; -%c ignored.\n", arg_letter); Ok = FALSE; } break; case 'u': if(value >= GlobalState.lower_move_bound){ GlobalState.upper_move_bound = value; } else{ fprintf(GlobalState.logfile, "Upper bound is smaller than the lower bound; -%c ignored.\n", arg_letter); Ok = FALSE; } break; } } else{ fprintf(GlobalState.logfile, "-%c should be in the form -%c[elu]number.\n", arg_letter,arg_letter); Ok = FALSE; } if(!Ok){ exit(1); } } break; case GAMES_PER_FILE_ARGUMENT: if(GlobalState.ECO_level > 0){ fprintf(GlobalState.logfile, "-%c conflicts with -E.\n",arg_letter); exit(1); } else if(GlobalState.output_filename != NULL){ fprintf(GlobalState.logfile, "-%c: File %s has already been selected for output.\n", arg_letter, GlobalState.output_filename); exit(1); } else if(sscanf(associated_value,"%u", &GlobalState.games_per_file) != 1){ fprintf(GlobalState.logfile, "-%c should be followed by an unsigned integer.\n", arg_letter); exit(1); } else{ /* Value set. */ } break; case FILE_OF_ARGUMENTS_ARGUMENT: if(*filename != '\0'){ /* @@@ Potentially recursive call. Is this safe? */ read_args_file(filename); } else{ fprintf(GlobalState.logfile,"Usage: -%cfilename.\n", arg_letter); } break; case NON_MATCHING_GAMES_ARGUMENT: if(*filename != '\0'){ if(GlobalState.non_matching_file != NULL){ (void) fclose(GlobalState.non_matching_file); } GlobalState.non_matching_file = must_open_file(filename,"w"); } else{ fprintf(GlobalState.logfile,"Usage: -%cfilename.\n",arg_letter); exit(1); } break; case TAG_EXTRACTION_ARGUMENT: /* A single tag extraction criterion. */ extract_tag_argument(associated_value); break; case LINE_WIDTH_ARGUMENT: { /* Specify an output line width. */ unsigned length; if(sscanf(associated_value,"%u",&length) > 0){ set_output_line_length(length); } else{ fprintf(GlobalState.logfile, "-%c should be followed by an unsigned integer.\n", arg_letter); exit(1); } } break; case HELP_ARGUMENT: usage_and_exit(); break; case OUTPUT_FORMAT_ARGUMENT: /* Whether to use the source form of moves or * rewrite them into another format. */ { OutputFormat format = which_output_format(associated_value); if(format == UCI) { /* Rewrite the game in a format suitable for input to * a UCI-compatible engine. * This is actually LALG but involves adjusting a lot of * the other statuses, too. */ GlobalState.keep_NAGs = FALSE; GlobalState.keep_comments = FALSE; GlobalState.keep_move_numbers = FALSE; GlobalState.keep_checks = FALSE; GlobalState.keep_variations = FALSE; set_output_line_length(5000); format = LALG; } GlobalState.output_format = format; } break; case SEVEN_TAG_ROSTER_ARGUMENT: if(GlobalState.tag_output_format == ALL_TAGS || GlobalState.tag_output_format == SEVEN_TAG_ROSTER) { GlobalState.tag_output_format = SEVEN_TAG_ROSTER; } else { fprintf(GlobalState.logfile, "-%c clashes with another argument.\n", SEVEN_TAG_ROSTER_ARGUMENT); exit(1); } break; case DONT_KEEP_COMMENTS_ARGUMENT: GlobalState.keep_comments = FALSE; break; case DONT_KEEP_DUPLICATES_ARGUMENT: /* Make sure that this doesn't clash with -d. */ if(GlobalState.duplicate_file == NULL){ GlobalState.suppress_duplicates = TRUE; } else{ fprintf(GlobalState.logfile, "-%c clashes with -%c flag.\n", DONT_KEEP_DUPLICATES_ARGUMENT, DUPLICATES_FILE_ARGUMENT); exit(1); } break; case DONT_MATCH_PERMUTATIONS_ARGUMENT: GlobalState.match_permutations = FALSE; break; case DONT_KEEP_NAGS_ARGUMENT: GlobalState.keep_NAGs = FALSE; break; case OUTPUT_FEN_STRING_ARGUMENT: /* Output a FEN string of the final position. * This is displayed in a comment. */ if(GlobalState.add_FEN_comments) { /* Already implied. */ GlobalState.output_FEN_string = FALSE; } else { GlobalState.output_FEN_string = TRUE; } break; case CHECK_ONLY_ARGUMENT: /* Report errors, but don't convert. */ GlobalState.check_only = TRUE; break; case KEEP_SILENT_ARGUMENT: /* Turn off progress reporting. */ GlobalState.verbose = FALSE; break; case USE_SOUNDEX_ARGUMENT: /* Use soundex matches for player tags. */ GlobalState.use_soundex = TRUE; break; case MATCH_CHECKMATE_ARGUMENT: /* Match only games that end in checkmate. */ GlobalState.match_only_checkmate = TRUE; break; case SUPPRESS_ORIGINALS_ARGUMENT: GlobalState.suppress_originals = TRUE; break; case DONT_KEEP_VARIATIONS_ARGUMENT: GlobalState.keep_variations = FALSE; break; case USE_VIRTUAL_HASH_TABLE_ARGUMENT: GlobalState.use_virtual_hash_table = TRUE; break; case TAGS_ARGUMENT: if(*filename != '\0'){ read_tag_file(filename); } break; case TAG_ROSTER_ARGUMENT: if(*filename != '\0'){ read_tag_roster_file(filename); } break; case MOVES_ARGUMENT: if(*filename != '\0'){ /* Where the list of variations of interest are kept. */ FILE *variation_file = must_open_file(filename,"r"); /* We wish to search for particular variations. */ add_textual_variations_from_file(variation_file); fclose(variation_file); } break; case POSITIONS_ARGUMENT: if(*filename != '\0'){ FILE *variation_file = must_open_file(filename,"r"); /* We wish to search for positional variations. */ add_positional_variations_from_file(variation_file); fclose(variation_file); } break; case ENDINGS_ARGUMENT: if(*filename != '\0'){ if(!build_endings(filename)){ exit(1); } } break; default: fprintf(GlobalState.logfile, "Unrecognized argument -%c\n", arg_letter); } }
void WDMMenu::CheckGameFiles(const struct discHdr * header) { wbfs_disc_t *disc = WBFS_OpenDisc((u8 *) header->id); if (!disc) { WindowPrompt(tr( "ERROR:" ), tr( "Could not open Disc" ), tr( "OK" )); return; } wiidisc_t *wdisc = wd_open_disc((int(*)(void *, u32, u32, void *)) wbfs_disc_read, disc); if (!wdisc) { WindowPrompt(tr( "ERROR:" ), tr( "Could not open Disc" ), tr( "OK" )); return; } FST_ENTRY * fstbuffer = (FST_ENTRY *) wd_extract_file(wdisc, ONLY_GAME_PARTITION, (char*) "FST"); if (!fstbuffer) { WindowPrompt(tr( "ERROR:" ), tr( "Not enough free memory." ), tr( "OK" )); return; } wd_close_disc(wdisc); WBFS_CloseDisc(disc); int position = 0; vector<pair<int, string> > FilesNotInWDM; for(int i = 0; i < wdmFile->size(); ++i) { if(stringcompare(wdmFile->GetDolName(i), "main") == true) { DOLOffsetList.push_back(pair<int, int>(0, wdmFile->GetParameter(i))); Options->SetName(position, "%i.", position+1); Options->SetValue(position, wdmFile->GetReplaceName(i)); position++; } } for (u32 i = 1; i < fstbuffer[0].filelen; i++) { //don't add files that aren't .dol to the list const char * filename = fstfiles(fstbuffer, i); const char * fileext = NULL; if(filename) fileext = strrchr(filename, '.'); if (fileext && strcasecmp(fileext, ".dol") == 0) { char NameCpy[strlen(filename)+1]; strcpy(NameCpy, filename); char *extension = strrchr(NameCpy, '.'); if(extension) *extension = 0; int j; for(j = 0; j < wdmFile->size(); ++j) { if(stringcompare(wdmFile->GetDolName(j), NameCpy) == true) { DOLOffsetList.push_back(pair<int, int>(i, wdmFile->GetParameter(j))); Options->SetName(position, "%i.", position+1); Options->SetValue(position, wdmFile->GetReplaceName(j)); position++; break; } } if(j == wdmFile->size()) FilesNotInWDM.push_back(pair<int, string>(i, filename)); } } for(u32 i = 0; i < FilesNotInWDM.size(); ++i) { DOLOffsetList.push_back(pair<int, int>(FilesNotInWDM[i].first, 1)); Options->SetName(position, "%i.", position+1); Options->SetValue(position, FilesNotInWDM[i].second.c_str()); position++; } free(fstbuffer); }