void actualizeNonTerminals(GrammarADT grammar) { int nontermquant = getQuantTerminals(grammar); char * nontermsfounded = NULL ; int nontermsfoundedsize =0; ProductionsADT productions = getProductions(grammar); int productionquant = getQuant(productions),i; /*detect current non terminals*/ for (i=0; i<productionquant; i++) { ProductionADT p = getProduction(productions,i); char first = getProductionComponent(p,0); char sec = getProductionComponent(p,1); char third = getProductionComponent(p,2); if (isNonTerminal(first) && !containsChar(nontermsfounded,nontermsfoundedsize,first) ) { addChar(&nontermsfounded, &nontermsfoundedsize, first); } if (isNonTerminal(sec) && !containsChar(nontermsfounded,nontermsfoundedsize,sec) ) { addChar(&nontermsfounded, &nontermsfoundedsize, sec); } if(isNonTerminal(third) && !containsChar(nontermsfounded,nontermsfoundedsize,third)) { addChar(&nontermsfounded, &nontermsfoundedsize, third); } } /*actualize non terminals*/ if( nontermsfoundedsize != nontermquant ) { /*there are less current non terminals*/ setNonTerminals(grammar,nontermsfounded,nontermsfoundedsize); } }
void addTerminal(Grammar g, char * from){ if(g->terminals!=NULL && containsChar(g->terminals, from[0])){ printf("Terminal duplicado.\n No se acepta el lenguaje.\n"); exit(1); } g->terminals=concat(g->terminals,from); }
void test_generate_base85_password () { char* password = generatePassword(12, BASE85); int i=0; for(i=0;i<getStringLength(password);i++) CU_ASSERT_TRUE(containsChar(base85,password[i])); CU_ASSERT_EQUAL(getStringLength(password),12); }
void test_generate_base64_password () { char* password = generatePassword(12, BASE64); int i=0; for(i=0;i<getStringLength(password);i++) CU_ASSERT_TRUE(containsChar(base64,password[i])); printf("len: %d", getStringLength(password)); CU_ASSERT_EQUAL(getStringLength(password),12); }
void findWord(char* currentWord, const char board[4][4], int position, struct ListNode** HEAD) { int i, j, index; int row = position / 4; int col = position % 4; char* newWord = malloc(sizeof(char) * 33); if (newWord == NULL) { printf("Could not allocate enough memory. Returning.\n"); return; } memset(newWord, 0, 32); char adjacent; strcpy(newWord, currentWord); index = strlen(newWord); for (i = row-1; i <= row+1; i++) { for (j = col-1; j <= col+1; j++) { adjacent = getAdjacentChar(i, j, board); if (adjacent != 0 && !containsChar(adjacent, newWord)) { newWord[index] = board[i][j]; if (isWord(newWord)) { struct ListNode* newNode = malloc(sizeof(struct ListNode)); newNode->word = malloc(strlen(newWord) + 1); if (newNode == NULL || newNode->word == NULL) { printf("Could not allocate enough memory. Returning.\n"); return; } strcpy(newNode->word, newWord); newNode->next = *HEAD; *HEAD = newNode; ++index; } if (isPrefix(newWord)) { findWord(newWord, board, i*4+j, HEAD); } } } } free(newWord); }
void removeUnproductiveProductions(GrammarADT grammar) { ProductionsADT productions = getProductions(grammar); int i, quantproductions = getQuant(productions), productivequant=0,lastproductivequant=-1; char * productives = NULL; char * aux1 = NULL; while(productivequant != lastproductivequant) { lastproductivequant = productivequant; for( i=0; i< quantproductions; i++ ) { ProductionADT p1 = getProduction(productions,i); char first1 = getProductionComponent(p1,0); char sec1 = getProductionComponent(p1,1); char third1 = getProductionComponent(p1,2); if ( !containsChar(productives,productivequant,first1) ) { if ( ( sec1 == LAMDA && third1 == LAMDA ) || /*lamda*/ (isTerminal(sec1) && isTerminal(third1) ) || /*both terminal*/ ( isTerminal(sec1) && third1 == LAMDA ) || /*one terminal*/ ( isTerminal(third1) && sec1 == LAMDA ) || /*one terminal and one productive*/ (isTerminal(sec1) && ( isNonTerminal(third1) && containsChar(productives,productivequant,third1) ) ) || (isTerminal(third1) && ( isNonTerminal(sec1) && containsChar(productives,productivequant,sec1) ) ) || ( sec1 == LAMDA && ( isNonTerminal(third1) && containsChar(productives,productivequant,third1) ) ) || ( third1 == LAMDA && ( isNonTerminal(sec1) && containsChar(productives,productivequant,sec1) ) )) { if ( ( aux1 = realloc(productives, sizeof(char)*(productivequant+1)) ) == NULL ) { fprintf(stderr, "Error doing realloc \n"); } productives = aux1; productives[productivequant++] = first1; } } } } /*remove non terminals and terminals that are no longer there */ actualizeTerminals(grammar); actualizeNonTerminals(grammar); actualizeProductions(grammar); }
void removeUnreachableProductions(GrammarADT grammar) { ProductionsADT productions = getProductions(grammar); int i, quantproductions = getQuant(productions), reachablesquant=0,lastreachablesquant=0; char * reachables = malloc(sizeof(char)); char * aux1 = NULL; /*starts only with distinguished symbol, if it is in the current productions*/ if(inCurrentProductions(productions,getDistinguished(grammar))) { reachables[reachablesquant++] = getDistinguished(grammar); } /*until something the quantity of reachables varies*/ while (reachablesquant != lastreachablesquant) { lastreachablesquant = reachablesquant; for(i=0; i<quantproductions; i++) { char first = getProductionComponent(getProduction(productions,i),0); char sec = getProductionComponent(getProduction(productions,i),1); char third = getProductionComponent(getProduction(productions,i),2); /*if the symbol of the left is contained in the reachables, the non terminal * symbols of the right must be added*/ if (containsChar(reachables,reachablesquant,first)) { /*if the second symbol is nonterminal and is not yet in the * reachable list, it must be added*/ if ( isNonTerminal( sec ) && !containsChar(reachables,reachablesquant,sec)) { if ( ( aux1 = realloc(reachables, sizeof(char)*(reachablesquant+1)) ) == NULL ) { fprintf(stderr, "Error doing realloc \n"); } reachables = aux1; reachables[reachablesquant++] = sec; }/*if the third symbol is nonterminal and is not yet in the * reachable list, it must be added*/ else if( isNonTerminal(third) && !containsChar(reachables,reachablesquant,third) ) { if ( (aux1 = realloc(reachables, sizeof(char)*(reachablesquant+1)) ) == NULL ) { fprintf(stderr, "Error doing realloc \n"); } reachables = aux1; reachables[reachablesquant++] = third; } } } } /*TODO: delete debug printf*/ printf("\nReachables!!: "); printArray(reachables,reachablesquant); int symsToRemovequant=0; /*remove the unreachable productions*/ /*If the quantity of reachables is equal to the quantity of nonterminals, * nothing should be removed*/ if (reachablesquant != getQuantNonTerminals(grammar)) { char * symsToRemove = NULL; symsToRemovequant = getDifferents(getNonTerminals(grammar), getQuantNonTerminals(grammar) ,reachables, reachablesquant, &symsToRemove); printf("\nTO REMOVE:"); printArray(symsToRemove,symsToRemovequant ); for(i=0; i<symsToRemovequant; i++) { removeProduction(productions,symsToRemove[i]); } } /*remove non terminals and terminals that are no longer there */ actualizeTerminals(grammar); actualizeNonTerminals(grammar); actualizeProductions(grammar); }
String File::parseAbsolutePath (const String& p) { if (p.isEmpty()) return {}; #if JUCE_WINDOWS // Windows.. auto path = removeEllipsis (p.replaceCharacter ('/', '\\')); if (path.startsWithChar (getSeparatorChar())) { if (path[1] != getSeparatorChar()) { /* When you supply a raw string to the File object constructor, it must be an absolute path. If you're trying to parse a string that may be either a relative path or an absolute path, you MUST provide a context against which the partial path can be evaluated - you can do this by simply using File::getChildFile() instead of the File constructor. E.g. saying "File::getCurrentWorkingDirectory().getChildFile (myUnknownPath)" would return an absolute path if that's what was supplied, or would evaluate a partial path relative to the CWD. */ jassertfalse; path = File::getCurrentWorkingDirectory().getFullPathName().substring (0, 2) + path; } } else if (! path.containsChar (':')) { /* When you supply a raw string to the File object constructor, it must be an absolute path. If you're trying to parse a string that may be either a relative path or an absolute path, you MUST provide a context against which the partial path can be evaluated - you can do this by simply using File::getChildFile() instead of the File constructor. E.g. saying "File::getCurrentWorkingDirectory().getChildFile (myUnknownPath)" would return an absolute path if that's what was supplied, or would evaluate a partial path relative to the CWD. */ jassertfalse; return File::getCurrentWorkingDirectory().getChildFile (path).getFullPathName(); } #else // Mac or Linux.. // Yes, I know it's legal for a unix pathname to contain a backslash, but this assertion is here // to catch anyone who's trying to run code that was written on Windows with hard-coded path names. // If that's why you've ended up here, use File::getChildFile() to build your paths instead. jassert ((! p.containsChar ('\\')) || (p.indexOfChar ('/') >= 0 && p.indexOfChar ('/') < p.indexOfChar ('\\'))); auto path = removeEllipsis (p); if (path.startsWithChar ('~')) { if (path[1] == getSeparatorChar() || path[1] == 0) { // expand a name of the form "~/abc" path = File::getSpecialLocation (File::userHomeDirectory).getFullPathName() + path.substring (1); } else { // expand a name of type "~dave/abc" auto userName = path.substring (1).upToFirstOccurrenceOf ("/", false, false); if (auto* pw = getpwnam (userName.toUTF8())) path = addTrailingSeparator (pw->pw_dir) + path.fromFirstOccurrenceOf ("/", false, false); } } else if (! path.startsWithChar (getSeparatorChar())) { #if JUCE_DEBUG || JUCE_LOG_ASSERTIONS if (! (path.startsWith ("./") || path.startsWith ("../"))) { /* When you supply a raw string to the File object constructor, it must be an absolute path. If you're trying to parse a string that may be either a relative path or an absolute path, you MUST provide a context against which the partial path can be evaluated - you can do this by simply using File::getChildFile() instead of the File constructor. E.g. saying "File::getCurrentWorkingDirectory().getChildFile (myUnknownPath)" would return an absolute path if that's what was supplied, or would evaluate a partial path relative to the CWD. */ jassertfalse; #if JUCE_LOG_ASSERTIONS Logger::writeToLog ("Illegal absolute path: " + path); #endif } #endif return File::getCurrentWorkingDirectory().getChildFile (path).getFullPathName(); } #endif while (path.endsWithChar (getSeparatorChar()) && path != getSeparatorString()) // careful not to turn a single "/" into an empty string. path = path.dropLastCharacters (1); return path; }
void tokenizer(char* str) { //================= //Check if input was empty //================= if(!strcmp(str, "")) return; char* space_delim = " "; char* save; char* savestring = strdup(str); char* cmd = strtok_r(str, space_delim, &save); char** paths = parsePath(); //=============================== //built-ins //=============================== //================= //CD //================= if (strcmp(cmd, "cd") == 0) { char* path = strtok_r(NULL, space_delim, &save); if(chdir(path)!= 0) { printf("%s: No such file or directory.\n", path); } } //================= //Viewproc **DOES NOT WORK*** //================= else if (strcmp(cmd, "viewproc") == 0) { char* proc_delim = "\n "; /* Need to do input redirection for this to print contents. Should check whether the file is there or not properly*/ char* file_name = strtok_r(NULL, proc_delim , &save); char* proc = "/proc/"; //have to do some memory managment here for concatenation of strings int new_size = strlen(proc) + strlen(file_name); char* proc_file = malloc(new_size*sizeof(char)); strcat(proc_file, proc); strcat(proc_file, file_name); int access_flag = access(proc_file, 1); if(access_flag == 0) { printf("%s exists\n", file_name); } else if(access_flag == -1) { printf("error: %s is not in /proc.\n", proc_file); } free(proc_file); } //================= //Echo //================= else if (strcmp(cmd, "echo") == 0) { char* word; for (word = strtok_r(NULL, space_delim, &save); word; word = strtok_r(NULL, space_delim, &save) ) { if (isEnvVar(word) == 1) { char* val = expandEnvVar(word); printf("%s ", val); } else if(isEnvVar(word) == -1) { printf("%s: Undefined variable.\n", word); return; } else { printf("%s ", word); } } printf("\n"); } //================= //the rest //================= else { char* input_delim = "<"; char* output_delim = ">"; char* word; char** argv = NULL; char* command = strdup(savestring); char* input = NULL; char* output = NULL; int backgroundExec=0; //================ //Check for background execution. If 1, then yes. //================ if(containsChar(command, '&')) { if(command[strlen(command)-1] != '&') { printf("'&' may only occur at the end of a command line.\n"); return; } else { backgroundExec=1; } } //================ //Scan for input redirection. If it exists, split. //First half goes into command. Second half goes into input. //================ if (containsChar(command, '<')) { command = strtok_r(command, input_delim, &save); input = strtok_r(NULL, input_delim, &save); } //================ //Scan for output redirection. Must scan both halves of above. //Must first check to make sure input exists. //================ if(containsChar(command, '>')) { command = strtok_r(command, output_delim, &save); output = strtok_r(NULL, output_delim, &save); } else if(input != NULL) { if(containsChar(input, '>')) { input = strtok_r(input, output_delim, &save); output = strtok_r(NULL, output_delim, &save); } } //================ //Scan for arguments. Scan command, input and output(since we are unsure which may contain it). //Save it into char* argv[]. char* argv[] initialized to 0. Add a null string at end. //Use realloc when adding a new argument //Append a null string at the end. This will act as a null terminator to a string-of-strings. //================ int i = 0; char* flag_delim = " &"; if(command != NULL) { command = strtok_r(command, flag_delim, &save); for (word = strtok_r(NULL, flag_delim, &save); word; word = strtok_r(NULL, flag_delim, &save) ) { argv = realloc(argv, (i+1)* sizeof(char*)); argv[i] = strdup(word); i++; } } if(input != NULL) { input = strtok_r(input, flag_delim, &save); for (word = strtok_r(NULL, flag_delim, &save); word; word = strtok_r(NULL, flag_delim, &save) ) { argv = realloc(argv, (i+1)* sizeof(char*)); argv[i] = strdup(word); i++; } } if(output != NULL) { output = strtok_r(output, flag_delim, &save); for (word = strtok_r(NULL, flag_delim, &save); word; word = strtok_r(NULL, flag_delim, &save) ) { argv = realloc(argv, (i+1)* sizeof(char*)); argv[i] = strdup(word); i++; } } argv = realloc(argv, i+1); argv[i] = 0; //================ //Print parsing results. Perfect! //Remove this once we get job processing working //================ if(command != NULL) { printf("Command: %s ", command); } if (argv != NULL) { printf("Flags: "); int j = 0; while(argv[j] != 0) { printf("%s ", argv[j]); j++; } } if (input != NULL) { printf("Input: %s ", input); } if (output!= NULL) { printf("Output: %s", output); } if(backgroundExec ==1) { printf("Background execution: yes"); } printf("\n"); } return; }
void test_contains_char_empty(void){ char* testString = ""; bool result = containsChar(testString, 'f'); CU_ASSERT_FALSE(result); }
void test_contains_char_null(void){ char* testString = NULL; bool result = containsChar(testString, 'f'); CU_ASSERT_FALSE(result); }
void test_contains_char_positive(void){ char* testString = "foobar"; bool result = containsChar(testString, 'f'); CU_ASSERT_TRUE(result); }
boolean isNonTerminal(Grammar g, char c){ return containsChar(g->nonTerminals,c); }