int Melder_stringMatchesCriterion (const wchar_t *value, int which_kMelder_string, const wchar_t *criterion) { if (value == NULL) { value = L""; /* Regard null strings as empty strings, as is usual in Praat. */ } if (criterion == NULL) { criterion = L""; /* Regard null strings as empty strings, as is usual in Praat. */ } if (which_kMelder_string <= kMelder_string_NOT_EQUAL_TO) { int matchPositiveCriterion = wcsequ (value, criterion); return (which_kMelder_string == kMelder_string_EQUAL_TO) == matchPositiveCriterion; } if (which_kMelder_string <= kMelder_string_DOES_NOT_CONTAIN) { int matchPositiveCriterion = wcsstr (value, criterion) != NULL; return (which_kMelder_string == kMelder_string_CONTAINS) == matchPositiveCriterion; } if (which_kMelder_string <= kMelder_string_DOES_NOT_START_WITH) { int matchPositiveCriterion = wcsnequ (value, criterion, wcslen (criterion)); return (which_kMelder_string == kMelder_string_STARTS_WITH) == matchPositiveCriterion; } if (which_kMelder_string <= kMelder_string_DOES_NOT_END_WITH) { int criterionLength = wcslen (criterion), valueLength = wcslen (value); int matchPositiveCriterion = criterionLength <= valueLength && wcsequ (value + valueLength - criterionLength, criterion); return (which_kMelder_string == kMelder_string_ENDS_WITH) == matchPositiveCriterion; } if (which_kMelder_string == kMelder_string_MATCH_REGEXP) { wchar_t *place = NULL, *errorMessage; regexp *compiled_regexp = CompileRE (criterion, & errorMessage, 0); if (compiled_regexp == NULL) return FALSE; // BUG: what about removing errorMessage? if (ExecRE (compiled_regexp, NULL, value, NULL, 0, '\0', '\0', NULL, NULL, NULL)) place = compiled_regexp -> startp [0]; free (compiled_regexp); return place != NULL; } return 0; /* Should not occur. */ }
static void gui_drawingarea_cb_expose (I, GuiDrawingAreaExposeEvent event) { iam (RunnerMFC); Melder_assert (event -> widget == my d_drawingArea); if (my graphics == NULL) return; // Could be the case in the very beginning. ExperimentMFC experiment = (ExperimentMFC) my data; long iresponse; if (my data == NULL) return; Graphics_setGrey (my graphics, 0.8); Graphics_fillRectangle (my graphics, 0, 1, 0, 1); Graphics_setGrey (my graphics, 0.0); if (experiment -> trial == 0) { Graphics_setTextAlignment (my graphics, Graphics_CENTRE, Graphics_HALF); Graphics_setFontSize (my graphics, 24); Graphics_text (my graphics, 0.5, 0.5, experiment -> startText); } else if (experiment -> pausing) { Graphics_setTextAlignment (my graphics, Graphics_CENTRE, Graphics_HALF); Graphics_setFontSize (my graphics, 24); Graphics_text (my graphics, 0.5, 0.5, experiment -> pauseText); if (experiment -> oops_right > experiment -> oops_left && experiment -> trial > 1) { drawControlButton (me, experiment -> oops_left, experiment -> oops_right, experiment -> oops_bottom, experiment -> oops_top, experiment -> oops_label); } } else if (experiment -> trial <= experiment -> numberOfTrials) { const wchar_t *visibleText = experiment -> stimulus [experiment -> stimuli [experiment -> trial]]. visibleText; wchar_t *visibleText_dup = Melder_wcsdup_f (visibleText ? visibleText : L""), *visibleText_p = visibleText_dup; Graphics_setFont (my graphics, kGraphics_font_TIMES); Graphics_setFontSize (my graphics, 10); Graphics_setColour (my graphics, Graphics_BLACK); Graphics_setTextAlignment (my graphics, Graphics_LEFT, Graphics_TOP); Graphics_text3 (my graphics, 0, 1, Melder_integer (experiment -> trial), L" / ", Melder_integer (experiment -> numberOfTrials)); Graphics_setTextAlignment (my graphics, Graphics_CENTRE, Graphics_TOP); Graphics_setFontSize (my graphics, 24); /* * The run text. */ if (visibleText_p [0] != '\0') { wchar_t *visibleText_q = wcschr (visibleText_p, '|'); if (visibleText_q) *visibleText_q = '\0'; Graphics_text (my graphics, 0.5, 1.0, visibleText_p [0] != '\0' ? visibleText_p : experiment -> runText); if (visibleText_q) visibleText_p = visibleText_q + 1; else visibleText_p += wcslen (visibleText_p); } else { Graphics_text (my graphics, 0.5, 1.0, experiment -> runText); } Graphics_setTextAlignment (my graphics, Graphics_CENTRE, Graphics_HALF); for (iresponse = 1; iresponse <= experiment -> numberOfDifferentResponses; iresponse ++) { ResponseMFC response = & experiment -> response [iresponse]; wchar_t *textToDraw = response -> label; // can be overridden if (visibleText_p [0] != '\0') { wchar_t *visibleText_q = wcschr (visibleText_p, '|'); if (visibleText_q) *visibleText_q = '\0'; textToDraw = visibleText_p; // override if (visibleText_q) visibleText_p = visibleText_q + 1; else visibleText_p += wcslen (visibleText_p); } if (wcsnequ (textToDraw, L"\\FI", 3)) { structMelderFile file; MelderDir_relativePathToFile (& experiment -> rootDirectory, textToDraw + 3, & file); Graphics_imageFromFile (my graphics, Melder_fileToPath (& file), response -> left, response -> right, response -> bottom, response -> top); } else { Graphics_setColour (my graphics, response -> name [0] == '\0' ? Graphics_SILVER : experiment -> responses [experiment -> trial] == iresponse ? Graphics_RED : experiment -> ok_right > experiment -> ok_left || experiment -> responses [experiment -> trial] == 0 ? Graphics_YELLOW : Graphics_SILVER); Graphics_setLineWidth (my graphics, 3.0); Graphics_fillRectangle (my graphics, response -> left, response -> right, response -> bottom, response -> top); Graphics_setColour (my graphics, Graphics_MAROON); Graphics_rectangle (my graphics, response -> left, response -> right, response -> bottom, response -> top); Graphics_setFontSize (my graphics, response -> fontSize ? response -> fontSize : 24); Graphics_text (my graphics, 0.5 * (response -> left + response -> right), 0.5 * (response -> bottom + response -> top), textToDraw); } Graphics_setFontSize (my graphics, 24); } for (iresponse = 1; iresponse <= experiment -> numberOfGoodnessCategories; iresponse ++) { GoodnessMFC goodness = & experiment -> goodness [iresponse]; Graphics_setColour (my graphics, experiment -> responses [experiment -> trial] == 0 ? Graphics_SILVER : experiment -> goodnesses [experiment -> trial] == iresponse ? Graphics_RED : Graphics_YELLOW); Graphics_setLineWidth (my graphics, 3.0); Graphics_fillRectangle (my graphics, goodness -> left, goodness -> right, goodness -> bottom, goodness -> top); Graphics_setColour (my graphics, Graphics_MAROON); Graphics_rectangle (my graphics, goodness -> left, goodness -> right, goodness -> bottom, goodness -> top); Graphics_text (my graphics, 0.5 * (goodness -> left + goodness -> right), 0.5 * (goodness -> bottom + goodness -> top), goodness -> label); } if (experiment -> replay_right > experiment -> replay_left && my numberOfReplays < experiment -> maximumNumberOfReplays) { drawControlButton (me, experiment -> replay_left, experiment -> replay_right, experiment -> replay_bottom, experiment -> replay_top, experiment -> replay_label); } if (experiment -> ok_right > experiment -> ok_left && experiment -> responses [experiment -> trial] != 0 && (experiment -> numberOfGoodnessCategories == 0 || experiment -> goodnesses [experiment -> trial] != 0)) { drawControlButton (me, experiment -> ok_left, experiment -> ok_right, experiment -> ok_bottom, experiment -> ok_top, experiment -> ok_label); } if (experiment -> oops_right > experiment -> oops_left && experiment -> trial > 1) { drawControlButton (me, experiment -> oops_left, experiment -> oops_right, experiment -> oops_bottom, experiment -> oops_top, experiment -> oops_label); } Melder_free (visibleText_dup); } else { Graphics_setTextAlignment (my graphics, Graphics_CENTRE, Graphics_HALF); Graphics_setFontSize (my graphics, 24); Graphics_text (my graphics, 0.5, 0.5, experiment -> endText); if (experiment -> oops_right > experiment -> oops_left && experiment -> trial > 1) { drawControlButton (me, experiment -> oops_left, experiment -> oops_right, experiment -> oops_bottom, experiment -> oops_top, experiment -> oops_label); } } }
static Strings Strings_createAsFileOrDirectoryList (const wchar_t *path, int type) { #if USE_STAT /* * Initialize. */ DIR *d = NULL; try { autoMelderString filePath, searchDirectory, left, right; /* * Parse the path. * Example: in /Users/paul/sounds/h*.wav", * the search directory is "/Users/paul/sounds", * the left environment is "h", and the right environment is ".wav". */ MelderString_copy (& searchDirectory, path); wchar_t *asterisk = wcsrchr (searchDirectory. string, '*'); if (asterisk != NULL) { *asterisk = '\0'; searchDirectory. length = asterisk - searchDirectory. string; // probably superfluous, but correct wchar_t *lastSlash = wcsrchr (searchDirectory. string, Melder_DIRECTORY_SEPARATOR); if (lastSlash != NULL) { *lastSlash = '\0'; // This fixes searchDirectory. searchDirectory. length = lastSlash - searchDirectory. string; // probably superfluous, but correct MelderString_copy (& left, lastSlash + 1); } else { MelderString_copy (& left, searchDirectory. string); // quickly save... MelderString_empty (& searchDirectory); // ...before destruction } MelderString_copy (& right, asterisk + 1); } char buffer8 [1+kMelder_MAXPATH]; Melder_wcsTo8bitFileRepresentation_inline (searchDirectory. string, buffer8); d = opendir (buffer8 [0] ? buffer8 : "."); if (d == NULL) Melder_throw ("Cannot open directory ", searchDirectory. string, "."); //Melder_casual ("opened"); autoStrings me = Thing_new (Strings); my strings = NUMvector <wchar_t *> (1, 1000000); struct dirent *entry; while ((entry = readdir (d)) != NULL) { MelderString_copy (& filePath, searchDirectory. string [0] ? searchDirectory. string : L"."); MelderString_appendCharacter (& filePath, Melder_DIRECTORY_SEPARATOR); wchar_t bufferW [1+kMelder_MAXPATH]; Melder_8bitFileRepresentationToWcs_inline (entry -> d_name, bufferW); MelderString_append (& filePath, bufferW); //Melder_casual ("read %s", filePath. string); Melder_wcsTo8bitFileRepresentation_inline (filePath. string, buffer8); struct stat stats; if (stat (buffer8, & stats) != 0) { Melder_throw ("Cannot look at file ", filePath. string, "."); //stats. st_mode = -1L; } //Melder_casual ("statted %s", filePath. string); //Melder_casual ("file %s mode %s", filePath. string, Melder_integer (stats. st_mode / 4096)); if ((type == Strings_createAsFileOrDirectoryList_TYPE_FILE && S_ISREG (stats. st_mode)) || (type == Strings_createAsFileOrDirectoryList_TYPE_DIRECTORY && S_ISDIR (stats. st_mode))) { Melder_8bitFileRepresentationToWcs_inline (entry -> d_name, bufferW); unsigned long length = wcslen (bufferW); if (bufferW [0] != '.' && (left. length == 0 || wcsnequ (bufferW, left. string, left. length)) && (right. length == 0 || (length >= right. length && wcsequ (bufferW + (length - right. length), right. string)))) { my strings [++ my numberOfStrings] = Melder_wcsdup (bufferW); } } } closedir (d); Strings_sort (me.peek()); return me.transfer(); } catch (MelderError) { if (d) closedir (d); // "finally" throw; } #elif defined (_WIN32) try { wchar_t searchPath [1+kMelder_MAXPATH]; int len = wcslen (path), hasAsterisk = wcschr (path, '*') != NULL, endsInSeparator = len != 0 && path [len - 1] == '\\'; autoStrings me = Thing_new (Strings); my strings = NUMvector <wchar_t *> (1, 1000000); swprintf (searchPath, 1+kMelder_MAXPATH, L"%ls%ls%ls", path, hasAsterisk || endsInSeparator ? L"" : L"\\", hasAsterisk ? L"" : L"*"); WIN32_FIND_DATAW findData; HANDLE searchHandle = FindFirstFileW (searchPath, & findData); if (searchHandle != INVALID_HANDLE_VALUE) { do { if ((type == Strings_createAsFileOrDirectoryList_TYPE_FILE && (findData. dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0) || (type == Strings_createAsFileOrDirectoryList_TYPE_DIRECTORY && (findData. dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0)) { if (findData. cFileName [0] != '.') { my strings [++ my numberOfStrings] = Melder_wcsdup (findData. cFileName); } } } while (FindNextFileW (searchHandle, & findData)); FindClose (searchHandle); } Strings_sort (me.peek()); return me.transfer(); } catch (MelderError) { throw; } #endif }