hlVoid List(FILE *pFile, HLDirectoryItem *pItem, hlBool bListFolders, hlBool bListFiles) { hlUInt i, uiItemCount; hlChar lpPath[512] = ""; switch(hlItemGetType(pItem)) { case HL_ITEM_FOLDER: if(bListFolders) { hlItemGetPath(pItem, lpPath, sizeof(lpPath)); fprintf(pFile, "%s\n", lpPath); } uiItemCount = hlFolderGetCount(pItem); for(i = 0; i < uiItemCount; i++) { List(pFile, hlFolderGetItem(pItem, i), bListFolders, bListFiles); } break; case HL_ITEM_FILE: if(bListFiles) { hlItemGetPath(pItem, lpPath, sizeof(lpPath)); fprintf(pFile, "%s\n", lpPath); } break; } }
static PyObject * Package_listdir(PackageObject *self, PyObject *args) { hlUInt i, iCount; hlChar *lpPath; const hlChar *lpItemName; HLDirectoryItem *pItem = 0; HLDirectoryItem *pSubItem = 0; PyObject *d, *v; if (!PyArg_ParseTuple(args, "s", &lpPath)) { return NULL; } // Find the item. pItem = hlFolderGetItemByPath(hlPackageGetRoot(), lpPath, HL_FIND_ALL); if(pItem == 0) { PyErr_SetString(PyExc_ValueError, "x not found in package.\n"); return NULL; } // Allocate a list if ((d = PyList_New(0)) == NULL) { return NULL; } // List items iCount = hlFolderGetCount(pItem); for( i = 0; i < iCount; i++ ) { pSubItem = hlFolderGetItem(pItem, i); lpItemName = hlItemGetName(pSubItem); v = PyString_FromString(lpItemName); if (v == NULL) { Py_DECREF(d); d = NULL; break; } if (PyList_Append(d, v) != 0) { Py_DECREF(v); Py_DECREF(d); d = NULL; break; } Py_DECREF(v); } return d; }
hlVoid EnterConsole(hlUInt uiPackage, hlUInt uiConsoleCommands, hlChar *lpConsoleCommands[]) { hlUInt i; hlChar lpBuffer[BUFFER_SIZE]; // Input string. hlChar lpCommand[BUFFER_SIZE]; // Input command (i.e. first word in input string). hlChar lpArgument[BUFFER_SIZE]; // Input argument (i.e. rest of input string). hlChar *lpTemp; hlChar lpTempBuffer[BUFFER_SIZE]; hlUInt16 uiColor; HLDirectoryItem *pItem = 0, *pSubItem = 0; hlBool bFound; hlUInt uiItemCount, uiFolderCount, uiFileCount; hlChar iChar; HLStream *pStream = 0; HLAttribute Attribute; HLPackageType ePackageType = HL_PACKAGE_NONE; hlUInt uiSubPackage = HL_ID_INVALID; pItem = hlPackageGetRoot(); while(hlTrue) { uiColor = GetColor(); SetColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY); if(uiConsoleCommands > 0) { printf("%s>%s\n", hlItemGetName(pItem), *lpConsoleCommands); strncpy(lpBuffer, *lpConsoleCommands, sizeof(lpBuffer)); lpBuffer[sizeof(lpBuffer) - 1] = '\0'; uiConsoleCommands--; lpConsoleCommands++; } else { // Command prompt. printf("%s>", hlItemGetName(pItem)); // Get and parse line. fgets(lpBuffer, sizeof(lpBuffer), stdin); } SetColor(uiColor); i = (hlUInt)strlen(lpBuffer); while(i > 0 && (lpBuffer[i - 1] == '\r' || lpBuffer[i - 1] == '\n')) { i--; lpBuffer[i] = '\0'; } *lpCommand = *lpArgument = 0; strcpy(lpCommand, lpBuffer); lpTemp = strchr(lpCommand, ' '); if(lpTemp != 0) { strcpy(lpArgument, lpTemp + 1); *lpTemp = 0; } // Cycle through commands. // // Directory listing. // Good example of CDirectoryItem::GetType(). // #ifdef _WIN32 if(stricmp(lpCommand, "dir") == 0) #else if(stricmp(lpCommand, "ls") == 0) #endif { uiItemCount = hlFolderGetCount(pItem); uiFolderCount = 0, uiFileCount = 0; *lpTempBuffer = 0; hlItemGetPath(pItem, lpTempBuffer, sizeof(lpTempBuffer)); printf("Directory of %s:\n", lpTempBuffer); printf("\n"); if(*lpArgument == 0) { // List all items in the current folder. for(i = 0; i < uiItemCount; i++) { pSubItem = hlFolderGetItem(pItem, i); if(hlItemGetType(pSubItem) == HL_ITEM_FOLDER) { uiFolderCount++; printf(" <%s>\n", hlItemGetName(pSubItem)); } else if(hlItemGetType(pSubItem) == HL_ITEM_FILE) { uiFileCount++; printf(" %s\n", hlItemGetName(pSubItem)); } } } else { pSubItem = hlFolderFindFirst(pItem, lpArgument, HL_FIND_ALL | HL_FIND_NO_RECURSE); while(pSubItem) { if(hlItemGetType(pSubItem) == HL_ITEM_FOLDER) { uiFolderCount++; printf(" <%s>\n", hlItemGetName(pSubItem)); } else if(hlItemGetType(pSubItem) == HL_ITEM_FILE) { uiFileCount++; printf(" %s\n", hlItemGetName(pSubItem)); } pSubItem = hlFolderFindNext(pItem, pSubItem, lpArgument, HL_FIND_ALL | HL_FIND_NO_RECURSE); } } printf("\n"); // Could also have used hlFolderGetFolderCount() and // hlFolderGetFileCount(). printf("Summary:\n"); printf("\n"); printf(" %u Folder%s.\n", uiFolderCount, uiFolderCount != 1 ? "s" : ""); printf(" %u File%s.\n", uiFileCount, uiFileCount != 1 ? "s" : ""); printf("\n"); } // // Change directory. // Good example of CDirectoryFolder::GetParent() and item casting. // else if(stricmp(lpCommand, "cd") == 0) { if(*lpArgument == 0) { printf("No argument for command cd supplied.\n"); } else { if(stricmp(lpArgument, ".") == 0) { } else if(stricmp(lpArgument, "..") == 0) { if(hlItemGetParent(pItem) != 0) { pItem = hlItemGetParent(pItem); } else { printf("Folder does not have a parent.\n"); } } else { bFound = hlFalse; uiItemCount = hlFolderGetCount(pItem); for(i = 0; i < uiItemCount; i++) { pSubItem = hlFolderGetItem(pItem, i); if(hlItemGetType(pSubItem) == HL_ITEM_FOLDER && stricmp(lpArgument, hlItemGetName(pSubItem)) == 0) { bFound = hlTrue; pItem = pSubItem; break; } } if(!bFound) { printf("%s not found.\n", lpArgument); } } } } // // Go to the root folder. // else if(stricmp(lpCommand, "root") == 0) { pItem = hlPackageGetRoot(); } // // Item information. // Good example of CPackageUtility helper functions. // else if(stricmp(lpCommand, "info") == 0) { if(*lpArgument == 0) { printf("No argument for command info supplied.\n"); } else { pSubItem = hlFolderGetItemByPath(pItem, lpArgument, HL_FIND_ALL); if(pSubItem != 0) { *lpTempBuffer = 0; hlItemGetPath(pSubItem, lpTempBuffer, sizeof(lpTempBuffer)); printf("Information for %s:\n", lpTempBuffer); printf("\n"); switch(hlItemGetType(pSubItem)) { case HL_ITEM_FOLDER: printf(" Type: Folder\n"); #ifdef _WIN32 printf(" Size: %I64u B\n", hlFolderGetSizeEx(pSubItem, hlTrue)); printf(" Size On Disk: %I64u B\n", hlFolderGetSizeOnDiskEx(pSubItem, hlTrue)); #else printf(" Size: %llu B\n", hlFolderGetSizeEx(pSubItem, hlTrue)); printf(" Size On Disk: %llu B\n", hlFolderGetSizeOnDiskEx(pSubItem, hlTrue)); #endif printf(" Folders: %u\n", hlFolderGetFolderCount(pSubItem, hlTrue)); printf(" Files: %u\n", hlFolderGetFileCount(pSubItem, hlTrue)); break; case HL_ITEM_FILE: printf(" Type: File\n"); printf(" Extractable: %s\n", hlFileGetExtractable(pSubItem) ? "True" : "False"); //printf(" Validates: %s\n", hlFileGetValidates(pSubItem) ? "True" : "False"); printf(" Size: %u B\n", hlFileGetSize(pSubItem)); printf(" Size On Disk: %u B\n", hlFileGetSizeOnDisk(pSubItem)); break; } uiItemCount = hlPackageGetItemAttributeCount(); for(i = 0; i < uiItemCount; i++) { if(hlPackageGetItemAttribute(pSubItem, i, &Attribute)) { PrintAttribute(" ", &Attribute, ""); } } printf("\n"); } else { printf("%s not found.\n", lpArgument); } } } // // Extract item. // Good example of CPackageUtility extract functions. // else if(stricmp(lpCommand, "extract") == 0) { if(*lpArgument == 0) { printf("No argument for command extract supplied.\n"); } else { if(stricmp(lpArgument, ".") == 0) { pSubItem = pItem; } else { pSubItem = hlFolderGetItemByName(pItem, lpArgument, HL_FIND_ALL); } if(pSubItem) { // Extract the item. // Item is extracted to cDestination\Item->GetName(). if(!bSilent) { printf("Extracting %s...\n", hlItemGetName(pSubItem)); printf("\n"); } hlItemExtract(pSubItem, lpDestination); if(!bSilent) { printf("\n"); printf("Done.\n"); } } else { printf("%s not found.\n", lpArgument); } } } // // Validate item. // Validates the checksums of each item. // else if(stricmp(lpCommand, "validate") == 0) { if(*lpArgument == 0) { printf("No argument for command extract supplied.\n"); } else { if(stricmp(lpArgument, ".") == 0) { pSubItem = pItem; } else { pSubItem = hlFolderGetItemByName(pItem, lpArgument, HL_FIND_ALL); } if(pSubItem) { if(!bSilent) { printf("Validating %s...\n", hlItemGetName(pSubItem)); printf("\n"); } Validate(pSubItem); if(!bSilent) { printf("\n"); printf("Done.\n"); } } else { printf("%s not found.\n", lpArgument); } } } // // Find items. // Good example of recursive directory navigation (Search() function). // else if(stricmp(lpCommand, "find") == 0) { if(*lpArgument == 0) { printf("No argument for command find supplied.\n"); } else { // Search for the requested items. if(!bSilent) { printf("Searching for %s...\n", lpArgument); printf("\n"); } uiItemCount = 0; pSubItem = hlFolderFindFirst(pItem, lpArgument, HL_FIND_ALL); while(pSubItem) { hlItemGetPath(pSubItem, lpTempBuffer, sizeof(lpTempBuffer)); // Print the path. uiItemCount++; Print(hlItemGetType(pSubItem) == HL_ITEM_FILE ? FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY : GetColor(), " Found %s: %s\n", hlItemGetType(pSubItem) == HL_ITEM_FOLDER ? "folder" : "file", lpTempBuffer); pSubItem = hlFolderFindNext(pItem, pSubItem, lpArgument, HL_FIND_ALL); } if(!bSilent) { if(uiItemCount != 0) { printf("\n"); } printf(" %u item%s found.\n", uiItemCount, uiItemCount != 1 ? "s" : ""); printf("\n"); } } } // // Type files. // Good example of reading files into memory. // else if(stricmp(lpCommand, "type") == 0) { if(*lpArgument == 0) { printf("No argument for command type supplied.\n"); } else { pSubItem = hlFolderGetItemByName(pItem, lpArgument, HL_FIND_FILES); if(pSubItem) { *lpTempBuffer = 0; hlItemGetPath(pSubItem, lpTempBuffer, sizeof(lpTempBuffer)); if(!bSilent) { printf("Type for %s:\n", lpTempBuffer); printf("\n"); } if(hlFileCreateStream(pSubItem, &pStream)) { if(hlStreamOpen(pStream, HL_MODE_READ)) { uiColor = GetColor(); SetColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY); while(hlStreamReadChar(pStream, &iChar)) { if((iChar >= ' ' && iChar <= '~') || iChar == '\n' || iChar == '\t') { putc(iChar, stdout); } } SetColor(uiColor); hlStreamClose(pStream); } else { Print(FOREGROUND_RED | FOREGROUND_INTENSITY, "Error typing %s:\n%s\n", hlItemGetName(pSubItem), hlGetString(HL_ERROR_SHORT_FORMATED)); } hlFileReleaseStream(pSubItem, pStream); pStream = 0; } else { Print(FOREGROUND_RED | FOREGROUND_INTENSITY, "Error typing %s:\n%s\n", hlItemGetName(pSubItem), hlGetString(HL_ERROR_SHORT_FORMATED)); } if(!bSilent) { printf("\n"); printf("Done.\n"); } } else { printf("%s not found.\n", lpArgument); } } } // // Open item. // Good example of opening packages inside packages. // else if(stricmp(lpCommand, "open") == 0) { if(*lpArgument == 0) { printf("No argument for command open supplied.\n"); } else { pSubItem = hlFolderGetItemByName(pItem, lpArgument, HL_FIND_FILES); if(pSubItem) { if(hlFileCreateStream(pSubItem, &pStream)) { if(hlStreamOpen(pStream, HL_MODE_READ)) { ePackageType = hlGetPackageTypeFromStream(pStream); if(hlCreatePackage(ePackageType, &uiSubPackage)) { hlBindPackage(uiSubPackage); if(hlPackageOpenStream(pStream, HL_MODE_READ)) { if(!bSilent) Print(FOREGROUND_GREEN | FOREGROUND_INTENSITY, "%s opened.\n", hlItemGetName(pSubItem)); EnterConsole(uiSubPackage, uiConsoleCommands, lpConsoleCommands); hlPackageClose(); if(!bSilent) Print(FOREGROUND_GREEN | FOREGROUND_INTENSITY, "%s closed.\n", hlItemGetName(pSubItem)); } else { Print(FOREGROUND_RED | FOREGROUND_INTENSITY, "Error opening %s:\n%s\n", hlItemGetName(pSubItem), hlGetString(HL_ERROR_SHORT_FORMATED)); } hlDeletePackage(uiSubPackage); hlBindPackage(uiPackage); } else { Print(FOREGROUND_RED | FOREGROUND_INTENSITY, "Error opening %s:\n%s\n", hlItemGetName(pSubItem), hlGetString(HL_ERROR_SHORT_FORMATED)); } hlStreamClose(pStream); } else { Print(FOREGROUND_RED | FOREGROUND_INTENSITY, "Error opening %s:\n%s\n", hlItemGetName(pSubItem), hlGetString(HL_ERROR_SHORT_FORMATED)); } hlFileReleaseStream(pSubItem, pStream); pStream = 0; } else { Print(FOREGROUND_RED | FOREGROUND_INTENSITY, "Error opening %s:\n%s\n", hlItemGetName(pSubItem), hlGetString(HL_ERROR_SHORT_FORMATED)); } } else { printf("%s not found.\n", lpArgument); } } } // // Clear screen. // else if(stricmp(lpCommand, "status") == 0) { #ifdef _WIN32 printf("Total size: %I64u B\n", hlGetUnsignedLongLong(HL_PACKAGE_SIZE)); #else printf("Total size: %llu B\n", hlGetUnsignedLongLong(HL_PACKAGE_SIZE)); #endif printf("Total mapping allocations: %u\n", hlGetUnsignedInteger(HL_PACKAGE_TOTAL_ALLOCATIONS)); #ifdef _WIN32 printf("Total mapping memory allocated: %I64u B\n", hlGetUnsignedLongLong(HL_PACKAGE_TOTAL_MEMORY_ALLOCATED)); printf("Total mapping memory used: %I64u B\n", hlGetUnsignedLongLong(HL_PACKAGE_TOTAL_MEMORY_USED)); #else printf("Total mapping memory allocated: %llu B\n", hlGetUnsignedLongLong(HL_PACKAGE_TOTAL_MEMORY_ALLOCATED)); printf("Total mapping memory used: %llu B\n", hlGetUnsignedLongLong(HL_PACKAGE_TOTAL_MEMORY_USED)); #endif uiItemCount = hlPackageGetAttributeCount(); for(i = 0; i < uiItemCount; i++) { if(hlPackageGetAttribute(i, &Attribute)) { PrintAttribute("", &Attribute, ""); } } } #ifdef _WIN32 else if(stricmp(lpCommand, "cls") == 0) { system("cls"); } #endif else if(stricmp(lpCommand, "help") == 0) { printf("Valid commands:\n"); printf("\n"); #ifdef _WIN32 printf("dir <filter> (Directory list.)\n"); #else printf("ls <filter> (Directory list.)\n"); #endif printf("cd <folder> (Change directroy.)\n"); printf("info <item> (Item information.)\n"); printf("extract <item> (Extract item.)\n"); printf("validate <item> (Validate item.)\n"); printf("find <filter> (Find item.)\n"); printf("type <file> (Type a file.)\n"); printf("open <file> (Open a nested package.)\n"); printf("root (Go to the root folder.)\n"); printf("status (Package information.)\n"); #ifdef _WIN32 printf("cls (Clear the screen.)\n"); #endif printf("help (Program help.)\n"); printf("exit (Quit program.)\n"); printf("\n"); } else if(stricmp(lpCommand, "exit") == 0) { break; } else { printf("Unkown command: %s\n", lpCommand); } } }
HLValidation Validate(HLDirectoryItem *pItem) { hlUInt i, uiItemCount; hlChar lpPath[512] = ""; HLValidation eValidation = HL_VALIDATES_OK, eTest; switch(hlItemGetType(pItem)) { case HL_ITEM_FOLDER: if(!bSilent) { Print(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY, " Validating %s:\n", hlItemGetName(pItem)); } uiItemCount = hlFolderGetCount(pItem); for(i = 0; i < uiItemCount; i++) { eTest = Validate(hlFolderGetItem(pItem, i)); if(eTest > eValidation) { eValidation = eTest; } } if(!bSilent) { Print(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY, " Done %s: ", hlItemGetName(pItem)); PrintValidation(eValidation); printf("\n"); } break; case HL_ITEM_FILE: if(!bSilent) { printf(" Validating %s: ", hlItemGetName(pItem)); ProgressStart(); } eValidation = hlFileGetValidation(pItem); if(bSilent) { switch(eValidation) { case HL_VALIDATES_INCOMPLETE: case HL_VALIDATES_CORRUPT: hlItemGetPath(pItem, lpPath, sizeof(lpPath)); printf(" Validating %s: ", lpPath); PrintValidation(eValidation); printf("\n"); break; } } else { PrintValidation(eValidation); printf(" \n"); } break; } return eValidation; }
HLValidation Validate(PackageObject *self, HLDirectoryItem *pItem) { hlUInt i, uiItemCount; hlChar lpPath[512] = ""; HLValidation eValidation = HL_VALIDATES_OK, eTest; switch(hlItemGetType(pItem)) { case HL_ITEM_FOLDER: if(!self->bSilent) { PySys_WriteStdout(" Validating %s:\n", hlItemGetName(pItem)); } uiItemCount = hlFolderGetCount(pItem); for(i = 0; i < uiItemCount; i++) { eTest = Validate(self, hlFolderGetItem(pItem, i)); if(eTest > eValidation) { eValidation = eTest; } } if(!self->bSilent) { PySys_WriteStdout(" Done %s: ", hlItemGetName(pItem)); PrintValidation(eValidation); PySys_WriteStdout("\n"); } break; case HL_ITEM_FILE: if(!self->bSilent) { PySys_WriteStdout(" Validating %s: ", hlItemGetName(pItem)); //ProgressStart(); } eValidation = hlFileGetValidation(pItem); if(self->bSilent) { switch(eValidation) { case HL_VALIDATES_INCOMPLETE: case HL_VALIDATES_CORRUPT: hlItemGetPath(pItem, lpPath, sizeof(lpPath)); PySys_WriteStdout(" Validating %s: ", lpPath); PrintValidation(eValidation); PySys_WriteStdout("\n"); break; } } else { PrintValidation(eValidation); PySys_WriteStdout(" \n"); } break; } return eValidation; }