int deleteRecord(EQItemDB* itemDB, uint16_t itemNr, bool force) { int result = 0; if (!force) { printf("Are You Sure you want to delete item: %d '%s'\n", itemNr, (const char*)itemDB->GetItemLoreName(itemNr)); if (!getConfirmation()) return result; } // delete the item from the database if (itemDB->DeleteItem(itemNr)) printf("Deleted Item: %d\n", itemNr); else { result = 1; fprintf(stderr, "%s: Failed to delete item: %d\n", progname, itemNr); } return result; }
INT_PTR CALLBACK InfoPrg(HWND hDlg, UINT wMsg, WPARAM wParam, LPARAM lParam) // Win32 Change { int item, cmd; // short numberErr = 0; static int *dDone; switch (wMsg) { case WM_INITDIALOG: dDone = (int*)lParam; /* drop into PAINT */ case WM_PAINT: return FALSE; break; case WM_COMMAND: item = LOWORD(wParam); // WIN32 Change cmd = HIWORD (wParam); switch (item) { case IDCANCEL: if (cmd == BN_CLICKED) { if (getConfirmation(hDlg, "Are you sure you want to cancel?")==IDYES) { *dDone = TRUE; return TRUE; } } break; case ID_MM: SetDlgItemText( hDlg, 10, (char*)lParam); // stuff string break; case ID_SM: SetDlgItemText( hDlg, 11, (char*)lParam); // stuff string break; default: break; } break; // case WM_COMMAND default: return FALSE; break; } // switch return TRUE; }
void Logger::pop(bool success) { if (!parents_.empty()) { Timer timer = timers_.top(); const string parent = parents_.top(); const bool interrupt = interrupts_.top(); timers_.pop(); parents_.pop(); interrupts_.pop(); if (active_) { if (maxLevel_ < 0 || getCurrentLevel() < maxLevel_) { if (interrupt) { indent(); if (parents_.size() > 0) { cout << " `- "; } cout << parent << " ... "; } if (success) { cout << getConfirmation() << " "; } else { cout << "FAILURE "; exit(EXIT_FAILURE); } printTime(timer); } } } }
int importGDBM(EQItemDB* itemDB, const char* filename, uint16_t itemNr, bool force, bool update) { int result = 0; Datum key, data; int count = 0; // if not forced, make sure the user really means it. if (!force) { if (itemNr == 0) printf("Are You Sure you want to import items from GDBM file '%s'\n", filename); else printf("Are You Sure you want to import item %d from GDBM file '%s'\n", itemNr, filename); if (!getConfirmation()) return result; } if (itemNr != 0) { GDBMConvenience gdbm; // setup key datum to use for query key.size = sizeof(itemNr); key.data = (char*)&itemNr; // retrieve entry from GDBM file if (gdbm.GetEntry(filename, key, data)) { // an entry has been found // make sure the entry is the correct size if (data.size == sizeof(itemStruct)) { // yes, insert it if it doesn't already exist if (itemDB->AddItem((itemStruct*)data.data, update)) count++; // increment count else fprintf(stderr, "%s: Failure on insert of Item %d from file %s\n", progname, itemNr, filename); } else { fprintf(stderr, "%s: Item %d from file '%s' has incorrect size %d (not %d)\n", progname, itemNr, filename, data.size, sizeof(itemStruct)); result = 2; } // release the database data gdbm.Release(data); } else { fprintf(stderr, "%s: Error retrieving item %d from file '%s'\n", progname, itemNr, filename); result = 1; } } else { GDBMIterator gdbmit; bool hasData; int lookedat = 0; // initialize the iterator and retrieve the first key hasData = gdbmit.GetFirstKey(filename, key); if (!hasData) { fprintf(stderr, "%s: Couldn't retrieve GDBM file: %s\n", progname, filename); return 3; } while(hasData) { // retrieve data associated with the key if (gdbmit.GetData(data)) { // increment the count of items that have been looked at... lookedat++; // does the entry have the correct size? if (data.size == sizeof(itemStruct)) { // yes, insert item if it doesn't already exist if (itemDB->AddItem((itemStruct*)data.data, update)) count++; // increment count else fprintf(stderr, "%s: Failure on insert of Item %d from file %s\n", progname, *(uint16_t*)key.data, filename); } else { // no, print warning fprintf(stderr, "Warning Item %d from file '%s' has incorrect size %d (not %d): Not Inserted\n", *(uint16_t*)key.data, filename, data.size, sizeof(itemStruct)); result = 4; } // display a status update if ((lookedat % TOOL_STATUS_UPDATE) == 0) fprintf(stderr, "\tExamined %d items and Imported %d items from file '%s' so far...\n", lookedat, count, filename); // release the data gdbmit.Release(data); } else { // no data associated with key fprintf(stderr, "Warning Item %d from file '%s' doesn't have data\n", *(uint16_t*)key.data, filename); result = 5; } // release the key gdbmit.Release(key); // retrieve the next key hasData = gdbmit.GetNextKey(key); } // release the last key gdbmit.Release(key); // shutdown the iterator gdbmit.Done(); } fprintf(stderr, "%s: Inserted %d item(s)\n", progname, count); return result; }
int importFlatFile(EQItemDB* itemDB, const char* filename, uint16_t itemNr, bool force, bool update) { int result = 0; int count = 0; int lookedat = 0; FILE* idb; itemStruct i; // if not forced, make sure the user really means it. if (!force) { if (itemNr == 0) printf("Are You Sure you want to import items from flat file '%s'\n", filename); else printf("Are You Sure you want to import item %d from flat file '%s'\n", itemNr, filename); if (!getConfirmation()) return result; } // open the old flat file if ((idb = fopen (filename, "r")) != NULL) { int filesize; // set file position to end of file fseek(idb, 0, SEEK_END); // retrieve offset of end of file filesize = ftell(idb); // restore file position to the beginning of the file fseek(idb, 0, SEEK_SET); // the filesize must be a multiple of sizeof(itemStruct) otherwise // it's corrupt if ((filesize % sizeof(itemStruct)) != 0) { fprintf(stderr, "%s: Error Flat File '%s' is corrupt!\n" "\tThe size of the file (%d) is not a multiple of %d!\n", progname, filename, filesize, sizeof(itemStruct)); return 2; } // if an item number was specified, search for and only insert that item if (itemNr != 0) { bool found = false; // iterate through the records in the file while (fread (&i, sizeof (itemStruct), 1, idb)) { // increment looked at count lookedat++; // display a status update if ((lookedat % TOOL_STATUS_UPDATE) == 0) fprintf(stderr, "\tExamined %d items from file '%s' so far...\n", lookedat, filename); // if an item number was specified, check if this record is it if (itemNr != i.itemNr) continue; // not the one, continue // found the entry being searched for found = true; // import item if (itemDB->AddItem(&i, update)) count++; // increment count else fprintf(stderr, "%s: Failure on insert of Item %d from file %s\n", progname, itemNr, filename); // found the one, finished break; } if (!found) { fprintf(stderr, "%s: Flat File '%s' doesn't contain item number %d!\n", progname, filename, itemNr); result = 3; } } else { // insert all records in the file // iterate through the records in the file while (fread (&i, sizeof (itemStruct), 1, idb)) { // import item if (itemDB->AddItem(&i, update)) count++; // increment count else fprintf(stderr, "%s: Failure on insert of Item %d from file '%s'\n", progname, i.itemNr, filename); // display a status update if ((count % TOOL_STATUS_UPDATE) == 0) fprintf(stderr, "\tImported %d items from file '%s' so far...\n", count, filename); } } fprintf(stderr, "%s: Inserted %d item(s)\n", progname, count); // close the file fclose (idb); } else { fprintf(stderr, "%s: Error Opening Flat File: '%s': %s\n", progname, filename, strerror(errno)); result = 1; } return result; }
static void copy(int sourceFd, const char* sourceName, const char* sourcePath, int destFd, const char* destName, const char* destPath, bool force, bool prompt, bool recursive) { struct stat sourceSt, destSt; if (fstatat(sourceFd, sourceName, &sourceSt, 0) < 0) { warn("stat: '%s'", sourcePath); status = 1; return; } bool destExists = true; if (fstatat(destFd, destName, &destSt, 0) < 0) { if (errno != ENOENT) { warn("stat: '%s'", destPath); status = 1; return; } destExists = false; } if (destExists && sourceSt.st_dev == destSt.st_dev && sourceSt.st_ino == destSt.st_ino) { warnx("'%s' and '%s' are the same file", sourcePath, destPath); status = 1; return; } if (S_ISDIR(sourceSt.st_mode)) { if (!recursive) { warnx("omitting directory '%s' because -R is not specified", sourcePath); status = 1; return; } if (destExists && !S_ISDIR(destSt.st_mode)) { warnx("cannot overwrite '%s' with directory '%s'", destPath, sourcePath); status = 1; return; } if (!destExists) { if (mkdirat(destFd, destName, S_IRWXU) < 0) { warn("mkdir: '%s'", destPath); status = 1; return; } if (fstatat(destFd, destName, &destSt, 0) < 0) { warn("stat: '%s'", destPath); status = 1; return; } destExists = true; } int newSourceFd = openat(sourceFd, sourceName, O_SEARCH | O_DIRECTORY); if (newSourceFd < 0) { warn("open: '%s'", sourcePath); status = 1; return; } DIR* dir = fdopendir(newSourceFd); if (!dir) { warn("fdopendir: '%s'", sourcePath); close(newSourceFd); status = 1; return; } int newDestFd = openat(destFd, destName, O_SEARCH | O_DIRECTORY); if (newDestFd < 0) { warn("open: '%s'", destPath); closedir(dir); status = 1; return; } struct dirent* dirent = readdir(dir); while (dirent) { if (strcmp(dirent->d_name, ".") == 0 || strcmp(dirent->d_name, "..") == 0) { dirent = readdir(dir); continue; } char* newSourcePath = malloc(strlen(sourcePath) + strlen(dirent->d_name) + 2); if (!newSourcePath) err(1, "malloc"); stpcpy(stpcpy(stpcpy(newSourcePath, sourcePath), "/"), dirent->d_name); char* newDestPath = malloc(strlen(destPath) + strlen(dirent->d_name) + 2); if (!newDestPath) err(1, "malloc"); stpcpy(stpcpy(stpcpy(newDestPath, destPath), "/"), dirent->d_name); if (destExists && dirent->d_dev == destSt.st_dev && dirent->d_ino == destSt.st_ino) { warnx("cannot copy directory '%s' into itself '%s'", newSourcePath, destPath); } else { copy(newSourceFd, dirent->d_name, newSourcePath, newDestFd, dirent->d_name, newDestPath, force, prompt, recursive); } free(newSourcePath); free(newDestPath); dirent = readdir(dir); } closedir(dir); close(newDestFd); } else if (S_ISREG(sourceSt.st_mode)) { int newDestFd; if (destExists) { if (prompt) { fprintf(stderr, "%s: overwrite '%s'? ", program_invocation_short_name, destPath); if (!getConfirmation()) return; } newDestFd = openat(destFd, destName, O_WRONLY | O_TRUNC); if (newDestFd < 0) { if (force) { if (unlinkat(destFd, destName, 0) < 0) { warn("unlinkat: '%s'", destPath); status = 1; return; } } else { warn("open: '%s'", destPath); status = 1; return; } } else { destExists = true; } } if (!destExists) { newDestFd = openat(destFd, destName, O_WRONLY | O_CREAT, sourceSt.st_mode & 0777); if (newDestFd < 0) { warn("open: '%s'", destPath); status = 1; return; } } int newSourceFd = openat(sourceFd, sourceName, O_RDONLY); if (newSourceFd < 0) { warn("open: '%s'", sourcePath); close(newDestFd); status = 1; return; } copyFile(newSourceFd, sourcePath, newDestFd, destPath); close(newDestFd); close(newSourceFd); } else { warnx("unsupported file type: '%s'", sourcePath); status = 1; } }
INT_PTR CALLBACK DispPrg(HWND hDlg, UINT wMsg, WPARAM wParam, LPARAM lParam) // Win32 Change { int item, cmd; // short numberErr = 0; char message[128]; char bar[128]; long percent; int i; int memUsage; static int *dDone; switch (wMsg) { case WM_INITDIALOG: dDone = (int*)lParam; /* drop into PAINT */ case WM_PAINT: return FALSE; break; case WM_COMMAND: item = LOWORD(wParam); // WIN32 Change cmd = HIWORD (wParam); switch (item) { case IDCANCEL: if (cmd == BN_CLICKED) { if (getConfirmation(hDlg, "Are you sure you want to cancel?")==IDYES) { *dDone = TRUE; return TRUE; } } break; case ID_PRG: sprintf(message,"Progress: %ld %%",(long)lParam); SetDlgItemText( hDlg, CG_IDC_PROGDLG_PERCENT, message); // stuff string //Show some statistics on memory allocation requests sprintf(message,"Memory Requests (Last/Max): %.1lf / %.1lf MB", (double)kBytesAlloced/1024.0, (double)maxKBytesAlloced/1024.0); SetDlgItemText( hDlg, CG_IDC_PROGDLG_MEM_REQUEST, message); // stuff string //Show some statistics on current/peak memory usage memUsage = countMemUsage(); if (memUsage > maxKBytesUsage) maxKBytesUsage = memUsage; sprintf(message,"Memory Usage (Current/Max): %.1lf / %.1lf MB", (double)memUsage/1024.0, (double)maxKBytesUsage/1024.0); SetDlgItemText( hDlg, CG_IDC_PROGDLG_MEM_USAGE, message); // stuff string percent = (long)lParam; if( percent > 100 ) percent = 100; for(i=0; i<percent; i++) bar[i] = '|'; bar[percent] = 0; SetDlgItemText( hDlg, CG_IDC_PROGDLG_PROGRESS, bar); // stuff string break; default: break; } break; // case WM_COMMAND default: return FALSE; break; } // switch return TRUE; }