📄 fileselect.c
字号:
if (event->type == SDL_MOUSEBUTTONDOWN) { x = b->x; y = b->y; xlim = x + b->w; ylim = y + b->h; mx = event->button.x; my = event->button.y; if (mx >= x && mx <= xlim && my >= y && my <= ylim) { index = (my - y) / FONT_HEIGHT; if (index < dirCount) { lastActive = 0; dirIndex = index + dirOffset; strcpy(currentDirName, getText(dirBox, dirIndex + 1)); chdir(currentDirName); getcwd(currentDir, 255); fileIndex = -1; dirIndex = -1; if (fileList) { freeList(fileList); if (fileBox->text) { free(fileBox->text); if (fileBox->line) fileBox->line = 0; fileBox->text = NULL; } } if (dirList) { freeList(dirList); if (dirBox->text) { free(dirBox->text); if (dirBox->line) dirBox->line = 0; dirBox->text = NULL; } } dirList = getDirectories(); if (dirList) { dirCount = getListCount(dirList); dirSlider->range = dirCount; if (dirCount < 20) dirSlider->change = 1; else dirSlider->change = 20; if (dirCount) { dirList = sortList(dirList); list = dirList; while (list) { addText(dirBox, list->name); list = list->next; } } } fileList = getFiles(filePattern); if (fileList) { fileCount = getListCount(fileList); fileSlider->range = fileCount; if (fileCount < 20) fileSlider->change = 1; else fileSlider->change = 20; if (fileCount) { fileList = sortList(fileList); list = fileList; while (list) { addText(fileBox, list->name); list = list->next; } } } else fileCount = 0; dirOffset = fileOffset = 0; dirSlider->offset = 0; fileSlider->offset = 0; fileSlider->position = 0; dirSlider->position = 0; } } }}void fileEntryCB(SDL_Event *event, BUTTON *b){ int x, y, xlim, ylim, mx, my, index; Uint8 button; static Uint32 ticks; if (((WIDGET *) topWidget)->p.type == AlertType) return; if (event->type == SDL_MOUSEBUTTONDOWN) { button = ((SDL_MouseButtonEvent *)event)->button; lastActive = 0; x = b->x; y = b->y; xlim = x + b->w; ylim = y + b->h; mx = event->button.x; my = event->button.y; if (mx >= x && mx <= xlim && my >= y && my <= ylim) { index = (my - y) / FONT_HEIGHT; // index = 0 -> max entry if (index < fileCount - fileOffset) { if (fileIndex >= 0) toggleTextHighlight(fileBox, fileIndex + 1); if ((fileIndex == index + fileOffset) && (SDL_GetTicks() < ticks + 500)) { okCB(event, b); } else if (button == 3) { fileIndex = index + fileOffset; okCB(event, b); } else { ticks = SDL_GetTicks(); fileIndex = index + fileOffset; toggleTextHighlight(fileBox, fileIndex + 1); } } } }}FILELIST *getDirectories(void){ FILELIST *list, *next; int dnlen; DIR *dirp; struct dirent *dp; struct stat sbuf; list = next = NULL; dirp = opendir("."); while ((dp = readdir(dirp)) != NULL) { lstat(dp->d_name, &sbuf); if ((sbuf.st_mode & S_IFMT) == S_IFDIR) { if (!list) { list = (FILELIST *) malloc(sizeof(FILELIST)); next = list; } else { next->next = (FILELIST *) malloc(sizeof(FILELIST)); next = next->next; } dnlen = strlen(dp->d_name) + 2; next->name = (char *) malloc(dnlen); strcpy(next->name, dp->d_name); next->next = NULL; } } return list;}FILELIST *getFiles(char *pattern[]){ char *name; FILELIST *list, *next; int i, fnlen; DIR *dirp; struct dirent *dp; struct stat sbuf; list = next = NULL; dirp = opendir("."); i = 0; name = pattern[i]; while (name) { while ((dp = readdir(dirp)) != NULL) { lstat(dp->d_name, &sbuf); if ((sbuf.st_mode & S_IFMT) != S_IFDIR) { if (ismatch(name, dp->d_name)) { if (!list) { list = (FILELIST *) malloc(sizeof(FILELIST)); next = list; } else { next->next = (FILELIST *) malloc(sizeof(FILELIST)); next = next->next; } fnlen = strlen(dp->d_name) + 2; next->name = (char *) malloc(fnlen); strcpy(next->name, dp->d_name); next->next = NULL; } } } i++; rewinddir(dirp); name = pattern[i]; } return list;}void freeList(FILELIST *list){ FILELIST *next; while (list) { next = list->next; if (list->name) free(list->name); free(list); list = next; }}int getListCount(FILELIST *list){ int i; i = 0; while (list) { i++; list = list->next; } return i;}//// Case insensitive match of string against pattern.//// Wildcard rules://// 1) '?' in pattern matches any single character.//// 2) '*' in pattern matches any sequence of characters,// or none at all, until string character matches// the following character in pattern (if any).//int ismatch(char *pattern, char *string){ int i, j, wc_index, ret; char next; char str[MAX_LEN]; char pat[MAX_LEN]; ret = FALSE; wc_index = -1; i = 0; while (pattern[i]) // convert pattern to upper case { pat[i] = toupper(pattern[i]); i++; } pat[i] = '\0'; i = 0; while (string[i]) // convert string to upper case { str[i] = toupper(string[i]); i++; } str[i] = '\0'; i = j = 0; while (1) { if (!pat[i] && !str[j]) // both empty, found a match { ret = TRUE; break; } if (!pat[i] && str[j]) // pattern empty, but string isn't break; // no match if (!str[j]) // string is empty { next = pat[i + 1]; // check next in pattern if (!next && (pat[i] == '?' || pat[i] == '*')) ret = TRUE; // match if next is wildcard break; } if (pat[i] == '?') // match any single character { i++; j++; } else if (pat[i] == '*') // match any sequence of chars { wc_index = i; // save current index of wildcard next = pat[i + 1]; // get next pattern character while (str[j] && str[j] != next) j++; // go through string til next match i++; // then increment pattern index } else // match next character { if (pat[i] != str[j]) // no match { if (wc_index >= 0) // if we had a wildcard, i = wc_index - 1; // back up to it else // else match failed break; } i++; // next pattern character j++; // next string character } } return ret;}// Sort directory or file listFILELIST *sortList(FILELIST *list){ int i, n; FILELIST *a, *b, *todo, *t; a = list; while (a->next) // set list tail for sort a = a->next; a->next = listTail; listHead->next = list; a = listTail; for (n=1; a != listHead->next; n = n + n) { todo = listHead->next; list = listHead; while (todo != listTail) { t = todo; a = t; for (i=1; i<n; i++) t = t->next; b = t->next; t->next = listTail; t = b; for (i=1; i<n; i++) t = t->next; todo = t->next; t->next = listTail; list->next = mergeList(a, b); for (i=1; i<=n+n; i++) list = list->next; } } a = listHead->next; while (a->next) // null terminate sorted list { if (a->next == listTail) a->next = NULL; else a = a->next; } return(listHead->next);}// Merge directory or file sublistsFILELIST *mergeList(FILELIST *a, FILELIST *b){ int i; FILELIST *c; c = listTail; do { i = strcasecmp(a->name, b->name); if (i <= 0) { c->next = a; c = a; a = a->next; } else { c->next = b; c = b; b = b->next; } } while (c != listTail); c = listTail->next; listTail->next = listTail; return(c);}// end of file
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -