// *************************************************************** // Function: LoadPackets.cpp // *************************************************************** #include "PacketMan.h" #ifdef WIN32 #include "dirent.h" //ifdef LINUX #else #include #endif #include "Log.h" #include //************************************ // Name: LoadPackets // // Loads all packets in specified directory and subdirectories. // // Parameters: // directory - Location string // // Return: // //************************************ int PacketMan::LoadPackets(char *directory) { assert(directory!=NULL); DIR *dir=NULL; struct dirent *ds=NULL; dir=opendir(directory); if (dir==NULL) { gCoreLog.LOCATION; gCoreLog.error("Directory for packets does not exist!"); return -1; } //traverse through all the files in the directory, loading them while ((ds=readdir(dir))) { if (ds->d_name[0]!='.' && ds->d_name[0]!='#') { //try to open the file char fn[300]; strcpy(fn, directory); strcat(fn, ds->d_name); assert(strlen(fn)<290); ifstream fin (fn, ios::binary); // in linux this won't fail if its a directory *REVISE* if (fin.fail()) { //try and load this file as a directory strcat(fn, "/"); if (LoadPackets(fn)<0) { gCoreLog.LOCATION; gCoreLog.error("Failed to load file: %s", ds->d_name); return -2; } } else { //calculate the file size int begin=fin.tellg(); fin.seekg(0, ios::end); int end=fin.tellg(); int size=end-begin; #ifndef WIN32 //under linux this is probably a directory if (size == 0) { fin.close(); //try and load this file as a directory strcat(fn, "/"); if (LoadPackets(fn)<0) { gCoreLog.LOCATION; gCoreLog.error("Failed to load file: %s", ds->d_name); return -2; } continue; } #endif fin.seekg(0, ios::beg); //Load the file unsigned char *data=0; data=new unsigned char[size]; assert(data!=NULL); for (int i=0; id_name); strcpy(p.directory, directory); mPackets.push_back(p); gCoreLog.notice("Loaded: %s Size: %d bytes", ds->d_name, size); } //close the file and output success fin.close(); } } } //close the directory closedir(dir); return 0; }