File indexing completed on 2021-12-21 12:50:21

0001 #include <stdio.h>
0002 #include <stdlib.h>
0003 #include <ctype.h>
0004 
0005 #ifdef USE_LIBZ
0006 #include <zlib.h>
0007 #endif
0008 
0009 #include "InternalCollections.h"
0010 
0011 #ifndef LEVELS_INCLUDED
0012 #define LEVELS_INCLUDED 1
0013 #include "levels/data.c"
0014 #endif
0015 #include <KLocalizedString>
0016 
0017 #define BUFSIZE (128*1024)
0018 
0019 // static const int collection_save_id[] = {
0020 //   0, 1, 3, 5, 9, 6, 7, 8, 2, 4
0021 // };
0022 
0023 static const int collection_save_id[] = {
0024   10, 11, 12, 13, 14
0025 };
0026 
0027 int
0028 InternalCollections::configCollection2Real (int collection) {
0029   for (int i=0; i < (int) (sizeof (collection_save_id) / sizeof (int)); i++) {
0030     if (collection_save_id[i] == collection) return i;
0031   }
0032   return 0;
0033 }
0034 
0035 int
0036 InternalCollections::realCollection2Config(int collection) {
0037   assert(collection < (int) (sizeof (collection_save_id) / sizeof (int)));
0038   return collection_save_id[collection];
0039 }
0040 
0041 QString
0042 InternalCollections::collectionName(int _level) {
0043   switch (_level) {
0044   case 0:
0045     return i18n("Sasquatch");
0046     break;
0047 
0048   case 1:
0049     return i18n("Mas Sasquatch");
0050     break;
0051 
0052   case 2:
0053     return i18n("Sasquatch III");
0054     break;
0055 
0056   case 3:
0057     return i18n("Microban (easy)");
0058     break;
0059 
0060   case 4:
0061     return i18n("Sasquatch IV");
0062     break;
0063   }
0064 
0065   assert(false);
0066   return QString();
0067 }
0068 
0069 
0070 InternalCollections::InternalCollections() {
0071   int datasize, levelnum=0;
0072 
0073 #ifdef USE_LIBZ
0074   data_ = (char *) malloc(BUFSIZE);
0075   if (data_ == nullptr) abort();
0076 
0077   datasize = BUFSIZE;
0078   uncompress ((unsigned char *) data_, (long unsigned int *) &datasize, level_data_, sizeof (level_data_));
0079   data_ = (char *) realloc(data_, datasize);
0080   if (data_ == nullptr) abort ();
0081 #else
0082   datasize = sizeof (level_data_);
0083   data_ = (char *) malloc(datasize);
0084   if (data_ == nullptr) abort();
0085   memcpy(data_, level_data_, datasize);
0086 #endif
0087 
0088   int start=0, end=0;
0089 #if 0
0090   int name=0;
0091 #endif
0092   enum {NAME, DATA} state=NAME;
0093   while (end < datasize) {
0094     switch (state) {
0095     case NAME:
0096       if (data_[end] == '\n') {
0097     data_[end] = '\0';
0098     state = DATA;
0099       }
0100       end++;
0101       start = end;
0102       break;
0103 
0104     case DATA:
0105       if (isalpha(data_[end])) {
0106 //  collections_.add(new LevelCollection(data_+start, end-start, data_+name, collection_save_id[levelnum]));
0107     add(new LevelCollection(data_+start, end-start, collectionName(levelnum), collection_save_id[levelnum]));
0108     //printf("Level found: '%s'\n", data_+name);
0109     levelnum++;
0110 #if 0
0111     name = end;
0112 #endif
0113     state = NAME;
0114       }
0115       end++;
0116       break;
0117 
0118     default:
0119       assert(0);
0120     }
0121   }
0122   if (state == DATA) {
0123 //     collections_.add(new LevelCollection(data_+start, end-start, data_+name, collection_save_id[levelnum]));
0124     add(new LevelCollection(data_+start, end-start, collectionName(levelnum), collection_save_id[levelnum]));
0125     //printf("***Level found: '%s'\n", data_+name);
0126   }
0127   //printf("numlevels: %d/%d\n", levelnum+1, collections_.size());
0128 }
0129 
0130 InternalCollections::~InternalCollections() {
0131   for (int i=0; i<collections_.size(); i++) {
0132     delete collections_[i];
0133   }
0134 
0135   free(data_);
0136 }
0137 
0138 int
0139 InternalCollections::collections() {
0140   return collections_.size();
0141 }
0142 
0143 LevelCollection *
0144 InternalCollections::operator[](int n) {
0145   return collections_[n];
0146 }
0147 
0148 void
0149 InternalCollections::add(LevelCollection* c) {
0150    collections_.append(c);
0151 }