static bool cleanOut(const char* dir) { printf("cleanOut(%s)\n", dir); MAHandle list = maFileListStart(dir, "*"); MAT(list); char buf[2048]; int dirLen = strlen(dir); strcpy(buf, dir); int freeBufSpace = sizeof(buf) - dirLen; while(1) { int res; MAHandle fh; char* fileName = buf + dirLen; res = maFileListNext(list, fileName, freeBufSpace); MAT_CUSTOM(res, (_res < 0 || _res >= freeBufSpace)); if(res == 0) return true; if(fileName[res-1] == '/') { if(!cleanOut(buf)) return false; } MAT(fh = maFileOpen(buf, MA_ACCESS_READ_WRITE)); res = maFileDelete(fh); MAASSERT(maFileClose(fh) == 0); MAT(res); } MAASSERT(maFileListClose(list) == 0); return true; }
int main() { printf("0 - exit \n1 - add value to sorted list\n2 - remove value from list\n3 - print list\n\n"); pnode root = NULL; int cmd = 1; int value = 0; while (cmd != 0) { printf("vvedyte comandy\n"); scanf("%d", &cmd); if (cmd == 0) { printf("exit\n"); cleanOut(root); break; } else if (cmd == 1) { printf("vvedyte value "); scanf("%d", &value); addItem(root, value); } else if (cmd == 2) { printf("vvedyte chislo, kotoroe neobhodimo udalit "); scanf("%d", &value); removeItem(root, value); } else if (cmd == 3) printList(root); else if (cmd > 3) printf("ne pravilnaya comanda\n"); } return 0; }
static bool tryToMake(const char* dir) { MAHandle file = maFileOpen(dir, MA_ACCESS_READ_WRITE); MAT(file); int res = maFileExists(file); MAASSERT(res >= 0); if(res) { printf("Dir exists.\n"); // delete everything inside. return cleanOut(dir); } else { printf("Creating dir...\n"); MAT(maFileCreate(file)); } printf("Closing...\n"); res = maFileClose(file); MAASSERT(res == 0); printf("Done: %s\n", dir); return true; }