static void search(char * filename) { FILE * fptr; char line[1024]; if ((fptr = fopen(filename, "r")) == NULL) { write(2, "ERROR: can NOT open file!!\n", 27); return; } while (fgets(line, 1024, fptr) != NULL) { char * ptr = line; while (*ptr != 0) { int r; if ((r = matchStr(ptr, pattern)) == 1) { printf("file: %s\n", filename); printf("line: %s\n", line); break; } ptr++; } } fclose(fptr); }
/// Consume whitespace and comments void Input::eatWS() { // Until the end of the whitespace for (;;) { // Consume whitespace characters if (isspace(peekCh())) { readCh(); continue; } // If this is a single-line comment if (matchStr("//")) { // Read until and end of line is reached for (;;) { char ch = readCh(); if (ch == '\n' || ch == '\0') break; } continue; } // If this is a multi-line comment if (matchStr("/*")) { // Read until the end of the comment for (;;) { char ch = readCh(); if (ch == '*' && matchCh('/')) break; } continue; } // This isn't whitespace, stop break; } }
/** Primitive to access the matchStr method of the input object */ Value matchStr(Value inputVal, Value strVal) { if (!inputVal.isRawPtr()) throw EvalError("invalid argument to matchStr"); if (!strVal.isString()) throw EvalError("matchStr expects string argument"); auto input = (Input*)(void*)inputVal; auto str = (std::string)strVal; return input->matchStr(str)? Value::one:Value::zero; }
int updatePath (char *update) { if (matchStr(update, ".")) { return 1; } else if (matchStr(update, "..")) { char * lastSlash = strrchr(bPath + strlen(inuse->root) + 2, '/'); if (lastSlash == NULL) { return 0; } *lastSlash = 0; } else { sprintf(bPath, "%s/%s", bPath, update); } getFiles(); return 1; }
void unmountDevice () { if (inuse != NULL) { if (matchStr(inuse->root, "dvd")) { ISO9660_Unmount(); } else { fatUnmount(inuse->root); } inuse = NULL; } }
int setDevice (fatdev device) { unmountDevice(); if (!(device.io->isInserted())) { return 0; } if (matchStr(device.root, "dvd")) { DI_Mount(); if (!(ISO9660_Mount())) { setError(1); return 0; } } else { if (!fatMount(device.root, device.io, 0, 8, 512)) { setError(2); return 0; } } inuse = &device; memset(&bPath, 0, sizeof(bPath)); sprintf(bPath, "%s:/", inuse->root); getFiles(); return 1; }
static int matchStr(char * str, char * pattern) { int len_str, len_pat, c; /* base case */ len_str = strlen(str); len_pat = strlen(pattern); if (len_pat == 0) { return 1; } else if (len_str == 0) { if (len_pat == 2 && ((c = *(pattern+2)) == '*' || c == '?')) { return 1; } return 0; } if ((c = *(pattern+1)) == '?') { /* ignore current char in pattern */ if (matchStr(str, pattern+2)) { return 1; } /* compare current char in pattern */ if (MATCH(*pattern, *str)) { if (matchStr(str+1, pattern+2)) { return 1; } } } else if (c == '*') { /* ignore current char in pattern */ if (matchStr(str, pattern+2)) { return 1; } /* compare current char in pattern */ if (MATCH(*pattern, *str)) { if (matchStr(str+1, pattern)) { return 1; } } } else { if (MATCH(*pattern, *str)) { if (matchStr(str+1, pattern+1)) { return 1; } } return 0; } return 0; }
void getFiles () { int index; char name[MAXPATHLEN]; DIR_ITER *iter = diropen(bPath); struct stat fstat; if (list != NULL) { free(list); list = NULL; fCount = 0; } list = malloc(sizeof(item)); if (iter == NULL) { return; } index = 0; while (dirnext(iter, name, &fstat) == 0) { list = (item *)realloc(list, sizeof(item) * (index + 1)); memset(&(list[index]), 0, sizeof(item)); sprintf(list[index].name, "%s", name); if (fstat.st_mode & S_IFDIR) { list[index].size = 0; } else { list[index].size = fstat.st_size; } if (matchStr(list[index].name, ".")) { sprintf(list[index].labl, "[Current directory]"); } else if (matchStr(list[index].name, "..")) { sprintf(list[index].labl, "[Parent directory]"); } else { if (list[index].size > 0) { sprintf(list[index].labl, "%.40s", list[index].name); } else { sprintf(list[index].labl, "[%.380s]", list[index].name); } } index++; } dirclose(iter); fCount = index; }
/** Primitive to match a string and whitespace before it */ Value matchStrWS(Value inputVal, Value strVal) { eatWS(inputVal); return matchStr(inputVal, strVal); }
bool Lexer::matchs(char32_t const * string, size_t len) { return matchStr(string, len); }
bool Lexer::matchs(char const * string, size_t len) { // Default char may be both signed and unsigned. // Cast it to unsigned to be sure. return matchStr(reinterpret_cast<unsigned char const *>(string), len); }