//------------------------------------------------------------------------------ // Set slot functions //------------------------------------------------------------------------------ bool StoresMgr::setSlotStores(const base::PairStream* const msg) { // First let our base class do everything that it needs to. BaseClass::setSlotStores(msg); // --- // Clear all previous stores and assigned weapons // --- weaponsList = nullptr; externalList = nullptr; fuelList = nullptr; gunPtr = nullptr; // --- // Use the stores list that the Stores class just processed. base::PairStream* stores = getStores(); if (stores != nullptr){ // Create the new weapons list that contains all weapons { base::PairStream* newWeapons = new base::PairStream(); searchAndAdd(stores, typeid(Weapon), newWeapons); if (newWeapons->entries() > 0) weaponsList = newWeapons; newWeapons->unref(); } // Create the new external stores list that contains all // non-weapon, external stores (e.g., fuel tanks, pods, guns) { base::PairStream* newExternal = new base::PairStream(); searchAndAdd(stores, typeid(ExternalStore), newExternal); if (newExternal->entries() > 0) externalList = newExternal; newExternal->unref(); } // Create the new fuel tank list that contains all fuel tanks { base::PairStream* newFuel = new base::PairStream(); searchAndAdd(stores, typeid(FuelTank), newFuel); if (newFuel->entries() > 0) fuelList = newFuel; newFuel->unref(); } // Find the primary gun; i.e., the first gun found on our stores base::List::Item* item = stores->getFirstItem(); while (item != nullptr && gunPtr == nullptr) { base::Pair* pair = static_cast<base::Pair*>(item->getValue()); Gun* p = dynamic_cast<Gun*>(pair->object()); if (p != nullptr) gunPtr = p; item = item->getNext(); } stores->unref(); stores = nullptr; } return true; }
//------------------------------------------------------------------------------ // Search all of the objects in the main list for objects of 'type' and add // them to the sublist. Also check all Stores type objects for any 'type' objects. //------------------------------------------------------------------------------ void StoresMgr::searchAndAdd(base::PairStream* const mainList, const std::type_info& type, base::PairStream* sublist) { if (mainList != nullptr && sublist != nullptr) { const base::List::Item* item = mainList->getFirstItem(); while (item != nullptr) { const base::Pair* pair = static_cast<const base::Pair*>(item->getValue()); const base::Component* p = static_cast<const base::Component*>(pair->object()); // Check the type and add to the list bool isType = p->isClassType(type); if (isType) sublist->put(const_cast<base::Pair*>(pair)); // If this is a Stores object then check its stores for 'type' objects as well const Stores* sp = dynamic_cast<const Stores*>(p); if ( sp != nullptr ) { const base::PairStream* pstores = sp->getStores(); if (pstores != nullptr) { searchAndAdd(const_cast<base::PairStream*>(pstores), type, sublist); pstores->unref(); } } item = item->getNext(); } } }
//------------------------------------------------------------------------------ // Search all of the objects in the main list for objects of 'type' and add // them to the sublist. Also check all Stores type objects for any 'type' objects. //------------------------------------------------------------------------------ void StoresMgr::searchAndAdd(Basic::PairStream* const mainList, const std::type_info& type, Basic::PairStream* sublist) { if (mainList != 0 && sublist != 0) { const Basic::List::Item* item = mainList->getFirstItem(); while (item != 0) { Basic::Pair* pair = (Basic::Pair*)(item->getValue()); Basic::Component* p = (Basic::Component*)( pair->object() ); // Check the type and add to the list bool isType = p->isClassType(type); if (isType) sublist->put(pair); // If this is a Stores object then check its stores for 'type' objects as well Stores* sp = dynamic_cast<Stores*>(p); if ( sp != 0 ) { Basic::PairStream* pstores = sp->getStores(); searchAndAdd(pstores, type, sublist); pstores->unref(); } item = item->getNext(); } } }
int main(int argc, char *argv[]){ if(argc == 1){ printf("Very Few Arguments\n"); printf("File Name Missing\n"); exit(1); } list *l = (list *)malloc(sizeof(list)); init(l); path *paths; FILE *fp; int i, j, flag = 0, SIZE = 1024, USIZE = 1024, uCount, uFlag = 0, uFlag_1 = 0, uTmp; char zipFileName[32] = "", *buffer, *binary, *output, *huffList, character, dflag, cnt, *saket; for(i = 0; argv[1][i] != '\0'; i++){ zipFileName[i] = argv[1][i]; } zipFileName[i++] = '.'; zipFileName[i++] = 's'; zipFileName[i++] = 'a'; zipFileName[i++] = 'k'; fp = fopen(argv[1], "r"); if(!fp){ printf("File Doesn't Exist\n"); exit(2); } buffer = (char *)malloc(SIZE * sizeof(char)); i = 0; while((character = getc(fp)) != EOF){ buffer[i] = character; if(i >= (SIZE - 1)){ SIZE *= 2; buffer = (char *)realloc(buffer, SIZE); } if(flag == 0){ append(l, character); flag = 1; } else searchAndAdd(l, character); i++; } buffer[i] = '\0'; fclose(fp); bubbleSort(l); //Initial Sorting According to Weight. l = huffmanTree(l); assignPath(l); paths = (path *)malloc(l->cnt * sizeof(path)); storePath(l->head, paths); huffList = (char *)malloc(SIZE * sizeof(char)); storeHuffList(huffList, paths, l->cnt); binary = (char *)malloc(SIZE * sizeof(char)); binaryConversion(buffer, binary, paths); printf("%s\n", binary); i = strlen(binary); printf("%d\n", i); free(buffer); free(paths); appendRedundantBits(binary); printf("%s\n", binary); output = (char *)malloc(SIZE * sizeof(char)); binToAscii(binary, output); printf("%s\n", output); free(binary); dflag = (char)252; //11111100 cnt = (char)l->cnt;//nodeCount fp = fopen(zipFileName, "w+"); fprintf(fp, "%c", cnt); fprintf(fp, "%s", huffList); fprintf(fp, "%c", dflag); fprintf(fp, "%s", output); free(huffList); free(output); fclose(fp); printf("\nCompressed\n\t-------------------------------------------------------------------------------------------------------------\n"); return 0; }