MCPcopy Create free account
hub / github.com/AutoHotkey/AutoHotkey / FilePatternApply

Method FilePatternApply

source/script2.cpp:8551–8596  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

8549
8550
8551ResultType Line::FilePatternApply(LPTSTR aFilePattern, FileLoopModeType aOperateOnFolders
8552 , bool aDoRecurse, FilePatternCallback aCallback, void *aCallbackData)
8553{
8554 if (!*aFilePattern)
8555 // Caller should handle this case before calling us if an exception is to be thrown.
8556 return SetLastErrorMaybeThrow(true, ERROR_INVALID_PARAMETER);
8557
8558 if (aOperateOnFolders == FILE_LOOP_INVALID) // In case runtime dereference of a var was an invalid value.
8559 aOperateOnFolders = FILE_LOOP_FILES_ONLY; // Set default.
8560 g->LastError = 0; // Set default. Overridden only when a failure occurs.
8561
8562 FilePatternStruct fps;
8563
8564 LPTSTR last_backslash = _tcsrchr(aFilePattern, '\\');
8565 if (last_backslash)
8566 fps.dir_length = last_backslash - aFilePattern + 1; // Include the slash.
8567 else // Use current working directory, e.g. if user specified only *.*
8568 fps.dir_length = 0;
8569 fps.pattern_length = _tcslen(aFilePattern + fps.dir_length);
8570
8571 // Testing shows that the ANSI version of FindFirstFile() will not accept a path+pattern longer
8572 // than 259, even if the pattern would match files whose names are short enough to be legal.
8573 if (fps.dir_length + fps.pattern_length >= _countof(fps.path)
8574 || fps.pattern_length >= _countof(fps.pattern))
8575 return SetLastErrorMaybeThrow(true, ERROR_BUFFER_OVERFLOW);
8576
8577 // Make copies in case of overwrite of deref buf during LONG_OPERATION/MsgSleep,
8578 // and to allow modification:
8579 _tcscpy(fps.path, aFilePattern); // Include the pattern initially.
8580 _tcscpy(fps.pattern, aFilePattern + fps.dir_length); // Just the naked filename or pattern, for use with aDoRecurse.
8581
8582 if (!StrChrAny(fps.pattern, _T("?*")))
8583 // Since no wildcards, always operate on this single item even if it's a folder.
8584 aOperateOnFolders = FILE_LOOP_FILES_AND_FOLDERS;
8585
8586 // Passing the parameters this way reduces code size:
8587 fps.aCallback = aCallback;
8588 fps.aCallbackData = aCallbackData;
8589 fps.aDoRecurse = aDoRecurse;
8590 fps.aOperateOnFolders = aOperateOnFolders;
8591
8592 fps.failure_count = 0;
8593
8594 FilePatternApply(fps);
8595 return ThrowIntIfNonzero(fps.failure_count); // i.e. indicate success if there were no failures.
8596}
8597
8598
8599

Callers

nothing calls this directly

Calls 1

StrChrAnyFunction · 0.85

Tested by

no test coverage detected