void processCMD(void) { char *cwd = (char *) malloc(512); //allocate space for current dir string char *dir; int childPID; //child process ID getcwd(cwd, 512); //store current dir in cwd variable //dir = strcat(cwd, argv[1]); //PWD--------------------------------------------------------------------------- if (strcmp("pwd\n", argv[0]) == 0) { //user tped the pwd command printf("%s\n",cwd); //CD COMMANDS------------------------------------------------------------------- } else if(strcmp("cd\n", argv[0]) == 0) { //user typed the cd command //cd to home directory dir = calcPath(cwd, 0); //calculate root directory path chdir(dir); } else if(strcmp("cd", argv[0]) == 0) { //user typed the cd command with an argument //CD to parent directory if (strcmp(stripNewLine(argv[1]),"..") == 0) { dir = calcPath(cwd, 1); //calculate parent directory path chdir(dir); //CD function to a new directory } else if(chdir(stripNewLine(argv[1])) != 0) { //chdir returns a value of 0 if successful printf("Invalid directory\n"); } //EXECUTE PROGRAM---------------------------------------------------------------- } else { //user wants to execute a program childPID = fork(); //get child process ID argv[argc-1] = stripNewLine(argv[argc-1]); if (childPID == 0) { //execute process //if exec returns, an error has occured -> errno is set to -1 execvp(argv[0], argv); //path, ./ check compared } else if (childPID < 0) { printf("process error"); //exit(1); } //signal(SIGINT, SIG_IGN); //PUT KILL PROCESS FUNCTION IN //wait() waits for any child process to complete //calling wait with an argument of 0 means that the caller does not want th return code while(wait(NULL)!=childPID); } free(cwd); }
void Message::ParseIncomingMessage(string msg) { //messages go in as :NICK!USER@HOST TYPE PARAMETERS //whereby PARAMETERS might be lead by a : to indicate that it's multiple words //and PARAMETERS might be anything like target channel or target name or a command or text or shit //or they can go in as COMMAND PARAMETER //whereby PARAMETER can be fully optional int Offset = 0; //let's get rid of the stupid \r\n stripNewLine(msg); if(msg[0] == ':') { string tempmsg = msg; Offset++; this->HasNickUserHost = true; int MaxOffset = tempmsg.find(' '); Offset = tempmsg.find('!'); if(Offset == string::npos || Offset >= MaxOffset) goto ThingsWentBad; this->Nick = tempmsg.substr(1,Offset-1); tempmsg = tempmsg.substr(Offset+1); Offset = tempmsg.find('@'); if(Offset == string::npos || Offset >= MaxOffset) goto ThingsWentBad; this->User = tempmsg.substr(0,Offset); tempmsg = tempmsg.substr(Offset+1); Offset = tempmsg.find(' '); if(Offset == string::npos) goto ThingsWentBad; this->Host = tempmsg.substr(0,Offset); tempmsg = tempmsg.substr(Offset+1); ParseParams(tempmsg); }else{ //I know this isn't strictly correct, but it does the job goto ThingsWentBad; } if(StringBeginEqual(this->ParameterArray[0],"PRIVMSG") || StringBeginEqual(this->ParameterArray[0],"NOTICE")) this->isNormalMessage = true; else this->isNormalMessage = false; return; ThingsWentBad: //wtf? this->HasNickUserHost = false; //shit is weird so we just toss out unparsed //Offset = msg.find(" ")+1; ParseParams(msg); return; }
/* Given a flight system and a file of airport names, each on a line, add the airports to the flight system. */ void parseAirports(flightSys_t* s, FILE* airportFile) { char name[MAX_NAME_LEN]; while(fgets(name, MAX_NAME_LEN, airportFile)) { stripNewLine(name); addAirport(s,name); } }
/* Load playlist */ void bshAudioPlaylist::loadPlayList(char* szPlaylistFileName) { FILE* plFile; int i; szString temp; char filePath[1024]; gApplicationLog.printf("\nbshAudioPlaylist::loadPlayList() - Loading playlist"); plFile = fopen(szPlaylistFileName,"rt"); fscanf(plFile,"%d",&m_trackCount); fscanf(plFile,"\n"); for(i = 0; i < m_trackCount; i++) { fgets(temp,1024,plFile); if (i < m_trackCount - 1) temp[strlen(temp) - 1] = 0; gApplicationLog.printf("\nbshAudioPlaylist::loadPlayList() - (%d/%d) %s",i + 1,m_trackCount,temp); stripNewLine(temp); strcpy(m_trackList[i],temp); // Load audio track sprintf(filePath,"he-music/%s",m_trackList[i]); m_pTrack[i] = Mix_LoadMUS(filePath); if(m_pTrack[i] == nullptr) gApplicationLog.printf("\nbshAudioPlaylist::loadPlayList() - ERROR(%s)",Mix_GetError()); } fclose(plFile); m_currentTrack = 0; gApplicationLog.printf("\nbshAudioPlaylist::loadPlayList() - DONE!"); SetVolume(gSoundVolume); }
void client(char *bufferRead, int buffer_size, int portno, char * cmd) { // Initialize the logger file openlog("mcu.log", LOG_CONS | LOG_PERROR, LOG_USER); // Prepare socket init_socket(portno); // Send command down the socket char *str_to_send = addCarriageChar(stripNewLine(cmd)); int n = write(sockfd, str_to_send, strlen(str_to_send)); if (n == -1) { syslog(LOG_ERR, "error writing to socket"); } // Listen for confirmation bzero(bufferRead, buffer_size); n = timeout_serial_read(sockfd); if (n == -1) { syslog(LOG_ERR, "error reading from socket"); } else if (n == 0) { //syslog(LOG_ERR, "timeout to read response from cmd"); } else { n = read(sockfd, bufferRead, buffer_size); if (n == -1) { syslog(LOG_ERR, "error reading from socket"); } } // free memory free(str_to_send); // Close socket and quit close(sockfd); }
/* Given a flight system with airports, and a file of schedules for those airports, add flight times to the system. */ void parseSchedule(flightSys_t* s, FILE* schedule) { char* newAirportPrefix = "AIRPORT: "; size_t prefixLen = strlen(newAirportPrefix); airport_t* curAirport = NULL; /* curAirport is the airport we are currently adding a schedule for. In other words, it is the source airport. The while loop parses the file line by line and sets a new curAirport when a line that starts with "STATION: " is found. It'll also echo the schedule of the previous airport. Otherwise, it treats the current line as a new entry in the schedule of the curAirport and calls addFlight. */ char line[MAX_LINE_LEN]; while(fgets(line, MAX_LINE_LEN, schedule)) { if(!strcmp(line,"\n")) continue; else if(strncmp(line,newAirportPrefix,prefixLen) == 0){ if(curAirport) printSchedule(curAirport); //done with previous schedule so print it stripNewLine(line); char* srcName = line+prefixLen; curAirport = getAirport(s,srcName); if(curAirport) printf("Adding schedule for airport %s\n",srcName); else printf("Cannot find airport %s\n",srcName); } else if (curAirport) { char dstName[MAX_LINE_LEN]; char departureStr[MAX_LINE_LEN]; char arrivalStr[MAX_LINE_LEN]; char priceStr[MAX_LINE_LEN]; if(4!=sscanf(line,"%s %s %s $%s",dstName,departureStr,arrivalStr, priceStr)) { //parses the line as destination airport name, departure time, arrival time, and price printf("Skipping line: %s\n",line); continue; } airport_t* dst = getAirport(s,dstName); if(dst==NULL) { printf("Cannot find airport %s\n",dstName); continue; } timeHM_t arrival; timeHM_t departure; int cost; char* endptr; cost = (int) strtol(priceStr,&endptr,10); if(!stringToTime(arrivalStr, &arrival) || !stringToTime(departureStr, &departure) || *endptr) { printf("Skipping line: %s\n",line); continue; } addFlight(curAirport,dst,&departure,&arrival, cost); } } if(curAirport) printSchedule(curAirport); }
char *calcPath(char *cwd, int path) { char *slash = stripNewLine("/"); char *dir = (char *)malloc(512); char *currdir; char *usrsFol; char *usr; if (path == 0) { //calculate root path usrsFol = strtok(cwd, slash); //get user folder name usr = strtok(NULL, slash); //get user ID strcat(strcat(strcat(strcpy(dir, slash),usrsFol),slash), usr); //build root path including slashes } else if (path == 1) { //calculate parent path currdir = strrchr(cwd, '/'); //get pointer to current directory strncpy(dir, cwd, strlen(cwd)-strlen(currdir)); //copy entire path except the num chars in currdir } return dir; }
int packAdd(const char* packDir, int isDLC) { char* buf = malloc(sizeof(char)*2048); char* buf2= malloc(sizeof(char)*1024); char* val = malloc(sizeof(char)*1024); char* set = malloc(sizeof(char)*1024); //This block is for playlists list_t* playList=0; listItem* li=0; playListItem* pli=0; int i; //Counter FILE* f=0; packInfoType* ti = malloc(sizeof(packInfoType)); ti->lives=3; //Default 3 lives, if pack do not define another number. ti->isDLC=isDLC; //Any levels? (Packs are invalid without a levels folder and atleast one level) sprintf(buf, "%s/levels/level000.wzp", packDir); //Initialize list for playlist playList = listInit(_freePlaylistItem); //Open packs/packname/info.ini sprintf(buf, "%s/info.ini", packDir); f = android_fopen(buf, "r"); if(f) { while( fgets(buf, 128, f) ) { stripNewLine(buf); if(splitVals('=',buf,set,val)) { if(strcmp("author", set)==0) { ti->author = malloc( sizeof(char)*(strlen(val)+1+3) ); sprintf(ti->author, "By %s", val); } else if(strcmp("packname", set)==0) { ti->name = malloc( sizeof(char)*(strlen(val)+1) ); strcpy(ti->name,val); } else if(strcmp("comment", set)==0) { ti->comment = malloc( sizeof(char)*(strlen(val)+1+1) ); sprintf(ti->comment, "-%s", val); } else if(strcmp("mus", set)==0) { //mus=00,song if( splitVals(',',val, buf,set ) && splitVals('-',buf,val,buf2) ) { //val= from buf2=to set=song name pli = malloc( sizeof( playListItem ) ); pli->from=atoi(val); pli->to=atoi(buf2); pli->song=malloc(sizeof(char)*strlen(set)+1); strcpy( pli->song, set ); listAppendData( playList, (void*)pli ); } else { SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, " Playlist entry format is mus=XX-XX,song name.ogg where XX-XX is a level range.\n"); } } else if(strcmp("lives", set)==0) { ti->lives = atoi(val); } } //Found = } //reading file //Close the info file. fclose(f); } else { //Fall back if no file was found. SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "Warning: '%s' not found, using defaults.\n",buf); ti->author = malloc( sizeof(char)* 20 ); strcpy(ti->author, "info.ini not found"); ti->comment=ti->author; ti->name = malloc( sizeof(char)*(strlen(packDir)+1) ); strcpy(ti->name, packDir); } //Set path ti->path = malloc( sizeof(char)*(strlen(packDir)+1) ); strcpy(ti->path,packDir); //Set pack icon sprintf(buf, "%s/icon.png", packDir); ti->icon = loadImg(buf); if(!ti->icon) { ti->icon = loadImg( "data/noicon.png" ); } //Check if pack have a "finished" icon. sprintf(buf, "%s/finished.png", packDir); //Set ps.cp before entering makeLevelist, it makes use of packGetFile ps.cp = ti; //Add levels. ti->levels=0; ti->levels=makeLevelList(packDir); //makeLevelList looks in packDir/levels/ //set number of levels in pack ti->numLevels = ti->levels->count - 1 ; //The last level does not count (because it is just a "completed" screen). //Add to list of packages listAppendData( ps.packs, (void*)ti ); //Increase number of available packages ps.numPacks++; //Put playlist songs into levelfiles. li=&playList->begin; while( LISTFWD(playList,li) ) { pli=(playListItem*)li->data; for(i=0; i<ti->numLevels;i++) { if(i >= pli->from && i <= pli->to) { levelInfo(i)->musicFile = malloc( sizeof(char)*strlen(pli->song)+1 ); strcpy(levelInfo(i)->musicFile, pli->song); } } } //Clear playlist data listFree( playList ); free(buf); free(buf2); free(val); free(set); return(ps.numPacks-1); }
void loadSettings() { char* buf = malloc(sizeof(char)*2048); char* set = malloc(sizeof(char)*1024); char* val = malloc(sizeof(char)*1024); settings.bgPos.x = HSCREENW-160; settings.bgPos.y = HSCREENH-120; //Set defaults settings.soundVol=64; settings.musicVol=72; settings.wizClock=450; settings.showFps=0; settings.arcadeMode=0; settings.particles=1; settings.userMusic=0; settings.disableMusic=0; settings.wizVol=52; settings.glWidth=-1; settings.glHeight=-1; settings.glEnable=1; settings.glFilter=0; settings.fullScreen=0; settings.showWeb=0; //Not written to file settings.rift=0; settings.scaleFactor=1.0; settings.session=0; settings.solvedWorldWide=0; settings.firstRun=1; settings.uploadStats=-1; settings.online=0; //This is what will be checked for in the rest of the code //regardless of "uploadStats", it will only be 1 if //"uploadStats" is 1, because the check that can set it 1 is only //executed if it's enabled. //Free ptrs. if(settings.packDir) free(settings.packDir); if(settings.playerName) free(settings.playerName); settings.packDir = malloc(sizeof(char)*(strlen(DATADIR"packs/000_wizznic")+1) ); strcpy(settings.packDir, DATADIR"packs/000_wizznic"); settings.playerName = malloc(sizeof(char)*11 ); strcpy(settings.playerName, "player"); settings.musicDir = cwd( NULL, 0 ); if( !settings.musicDir ) printf("Out of memory, will crash soon.\n"); sprintf( buf, "%s/settings.ini", getConfigDir() ); FILE *f = fopen(buf, "r"); if(f) { while( fgets(buf, 128, f) ) { stripNewLine(buf); if(splitVals('=',buf, set, val)) { if( strcmp("soundvol", set)==0 ) { settings.soundVol = atoi(val); } else if( strcmp("musicvol", set)==0 ) { settings.musicVol = atoi(val); } else if( strcmp("wizclock", set)==0 ) { settings.wizClock = atoi(val); } else if( strcmp("wizvolume", set)==0 ) { settings.wizVol = atoi(val); } else if( strcmp("showfps", set)==0 ) { settings.showFps = atoi(val); } else if( strcmp("arcademode", set)==0 ) { settings.arcadeMode = atoi(val); } else if( strcmp("particles", set)==0 ) { settings.particles = atoi(val); } else if( strcmp("packdir", set)==0 ) { free(settings.packDir); settings.packDir = malloc(sizeof(char)*(strlen(val)+1) ); strcpy(settings.packDir,val); } else if( strcmp("playername", set)==0 ) { if( strlen(set) < 11 ) { strcpy(settings.playerName,val); } else { printf("Error, name: '%s' too long, max length is 10.\n",set); } } else if( strcmp("musicdir", set)==0 ) { //We check if it starts with . it now has to be a full path. if( val[0] != '.' ) { free(settings.musicDir); settings.musicDir = malloc(sizeof(char)*(strlen(val)+1) ); strcpy(settings.musicDir, val); } else { printf("Using '%s' as music directory instead of '%s'.\n", settings.musicDir, val); } } else if( strcmp("usermusic", set)==0 ) { settings.userMusic=atoi(val); } else if( strcmp("disablemusic",set)==0 ) { settings.disableMusic=atoi(val); } else if( strcmp("allowonline",set)==0 ) { //Only if the option is in the file, are we sure they had the choice. settings.uploadStats=atoi(val); if( settings.uploadStats!=-1 ) { settings.firstRun=0; } } else if( strcmp("glwidth",set)==0 ) { settings.glWidth=atoi(val); } else if( strcmp("glheight",set)==0 ) { settings.glHeight=atoi(val); } else if( strcmp("glenable",set)==0 ) { settings.glEnable=atoi(val); } else if( strcmp("glfilter",set)==0 ) { settings.glFilter=atoi(val); } else if( strcmp("fullscreen",set)==0 ) { settings.fullScreen=atoi(val); } else if( strcmp("showweb",set)==0 ) { settings.showWeb=atoi(val); } } } //Close file fclose( f ); } //Free the textbuffers free(buf); free(set); free(val); }
//Returns pointr to levelInfo_t if level successfully opened, returns nullptr if not. levelInfo_t* mkLevelInfo(const char* fileName) { int gotData=0; //If this is still 0 after the loop, level has no [data] FILE *f; levelInfo_t* tl; //Temp levelInfo. char* buf = malloc(sizeof(char)*256); //Read line buffer char* set = malloc(sizeof(char)*128); //Buffer for storing setting char* val = malloc(sizeof(char)*128); //Buffer for storing value tl=0; //Return null ptr if no file is found (malloc won't get called then) f = fopen(fileName, "r"); if(f) { //Allocate memory for level info. tl=malloc(sizeof(levelInfo_t)); //Set everything 0. memset(tl, 0, sizeof(levelInfo_t)); //Level file name tl->file=malloc( sizeof(char)*( strlen(fileName)+1 ) ); strcpy(tl->file, fileName); //preview file name sprintf( buf, "%s.png", fileName); tl->imgFile=malloc( sizeof(char)*( strlen(buf)+1 ) ); strcpy(tl->imgFile, buf); //default char map strcpy( buf, "charmap" ); tl->fontName=malloc( sizeof(char)*( strlen(buf)+1) ); strcpy( tl->fontName, buf ); //Default cursor strcpy( buf, "cursor.png" ); tl->cursorFile=malloc( sizeof(char)*(strlen(buf)+1) ); strcpy( tl->cursorFile, buf ); //start/stop images tl->startImg=0; tl->stopImg=0; //Default brick die time tl->brick_die_ticks=500; tl->brickDieParticles=1; //Initialize teleList tl->teleList = initList(); //Initialize switchlist tl->switchList = initList(); //Loop through file while(fgets(buf, 255, f)) { //We don't want \r or \n in the end of the string. stripNewLine(buf); //Stop reading when we reach [data] if(strcmp(buf,"[data]")==0) { gotData=1; break; } else { //Try and split string at = if(splitVals('=',buf, set,val) ) { //Check what we got. //Time left? if(strcmp("seconds",set)==0) { tl->time=atoi(val); } else if(strcmp("bgfile",set)==0) { tl->bgFile=malloc( sizeof(char)*( strlen(val)+1 ) ); strcpy(tl->bgFile, val); } else if(strcmp("tilebase",set)==0) { tl->tileBase=malloc( sizeof(char)*( strlen(val)+1 ) ); strcpy(tl->tileBase, val); } else if(strcmp("explbase",set)==0) { tl->explBase=malloc( sizeof(char)*( strlen(val)+1 ) ); strcpy(tl->explBase, val); } else if(strcmp("wallbase",set)==0) { tl->wallBase=malloc( sizeof(char)*( strlen(val)+1 ) ); strcpy(tl->wallBase, val); } else if(strcmp("author",set)==0) { tl->author=malloc( sizeof(char)*( strlen(val)+1 ) ); strcpy(tl->author, val); } else if(strcmp("levelname",set)==0) { tl->levelName=malloc( sizeof(char)*( strlen(val)+1 ) ); strcpy(tl->levelName, val); } else if(strcmp("sounddir",set)==0) { tl->soundDir=malloc( sizeof(char)*( strlen(val)+1 ) ); strcpy(tl->soundDir, val); } else if(strcmp("charbase",set)==0) { free(tl->fontName); tl->fontName=malloc( sizeof(char)*( strlen(val)+1 ) ); strcpy(tl->fontName, val); } else if(strcmp("cursorfile",set)==0) { free( tl->cursorFile ); tl->cursorFile=malloc( sizeof(char)*( strlen(val)+1 ) ); strcpy(tl->cursorFile, val); } else if(strcmp("startimage",set)==0) { //Ignore none keyword for start image if( strcmp( "none", val) != 0) { tl->startImg=malloc( sizeof(char)*( strlen(val)+1 ) ); strcpy(tl->startImg, val); } } else if(strcmp("stopimage", set)==0) { //Ignore none keyword for stop image if( strcmp( "none", val) != 0) { tl->stopImg=malloc( sizeof(char)*( strlen(val)+1 ) ); strcpy(tl->stopImg, val); } } else if(strcmp("brickdietime", set)==0) { tl->brick_die_ticks=atoi(val); } else if(strcmp("brickdieparticles", set)==0) { tl->brickDieParticles=atoi(val); } else if(strcmp("teleport", set)==0) { teleAddFromString(tl->teleList, val); } else if(strcmp("switch", set)==0) { //Yes, it's the same format, how neat. teleAddFromString(tl->switchList, val); } } //Got a = in the line } //Not [data] } //Reading file //Close file fclose(f); } if(!gotData) { //The reason we don't tell that there's no [data] section is because this //function is also used to check for the existance of levels, so it'd always //Return "no [data] found for the levelfile name just after the last level in a pack. free(tl); tl=0; } free(set); free(val); free(buf); set=0; val=0; buf=0; //Return ptr, is null if file couldnt be opened. return(tl); }
/* Given a file with routes and a flight system, parses the file and determine (and print) the earliest time each route can be completed. Lines of bad form are skipped while new lines are ignored. */ void calcRouteTimes(flightSys_t* s, FILE* routes) { char* newRoutePrefix = "ROUTE: "; size_t prefixLen = strlen(newRoutePrefix); char line[MAX_LINE_LEN]; timeHM_t now; char route[MAX_LINE_LEN]; char timeBuf[MAX_LINE_LEN]; char airportBuf[MAX_LINE_LEN]; airport_t* curAirport = NULL; int curCost = 0; int totalCost = 0; /* This while loop parses the file line by line and calculate the route times as it goes. If a new route line is found, the current route (if it exists) would be done and the result is printed. A current route exists if curAirport is not null. curAirport tells us where we are on the current route. now tells us the current time (the earliest time we can reach the curAirport on the current route). */ while(fgets(line, MAX_LINE_LEN,routes)) { stripNewLine(line); if(!strlen(line)) continue; //ignore line if it's empty else if(strncmp(line,newRoutePrefix,prefixLen) == 0) { //if beginning of line starts with "ROUTE: ", we are calculating for a new route if(curAirport) printCompleteRoute(route,&now, totalCost); //if curAirport is not NULL, we are done with the previous route so print the result curAirport = NULL; totalCost = 0; if(3!=sscanf(line+prefixLen,"%s %s %s",route,airportBuf,timeBuf) //parse rest of new route line into route name, airport name, and start time || !stringToTime(timeBuf,&now) //ensure time is valid || !(curAirport = getAirport(s,airportBuf))) { //ensure airport exists and sets curAirport to the starting airport printf("Skipping line: %s\n",line); continue; } } else if (curAirport) { /* This is the case when we are in the middle of calculating for a route, and the line should just be next airport we have to get to. Here, we use getNextFlight to determine the flight to take to the next airport. If there are no possible flights, the route cannot be completed. */ airport_t* next = getAirport(s,line); if(!next) { printf("Skipping line: %s\n",line); continue; } timeHM_t depart; timeHM_t arrival; if(!getNextFlight(curAirport,next,&now,&depart,&arrival,&curCost)) { curAirport = NULL; totalCost = 0; printf("Route %s cannot be completed\n",route); continue; } now = arrival; curAirport = next; totalCost += curCost; } } if(curAirport) printCompleteRoute(route,&now,totalCost); }
void loadSettings() { char* buf = malloc(sizeof(char)*2048); char* set = malloc(sizeof(char)*1024); char* val = malloc(sizeof(char)*1024); settings.bgPos.x = HSCREENW-160; settings.bgPos.y = HSCREENH-120; //Set defaults settings.soundVol=64; settings.musicVol=72; settings.wizClock=533; settings.showFps=0; settings.arcadeMode=0; settings.particles=1; settings.userMusic=0; settings.disableMusic=0; settings.wizVol=52; settings.glWidth=-1; settings.glHeight=-1; settings.glEnable=1; settings.glFilter=0; //Not written to file settings.scaleFactor=1.0; settings.session=0; settings.solvedWorldWide=0; settings.firstRun=1; settings.uploadStats=0; settings.online=0; //This is what will be checked for in the rest of the code //regardless of "uploadStats", it will only be 1 if //"uploadStats" is 1, because the check that can set it 1 is only //éxecuted if it's enabled. //Free ptrs. if(settings.packDir) free(settings.packDir); if(settings.playerName) free(settings.playerName); settings.packDir = malloc(sizeof(char)*(strlen("packs/wizznic")+1) ); strcpy(settings.packDir, "packs/wizznic"); settings.playerName = malloc(sizeof(char)*11 ); strcpy(settings.playerName, "player"); settings.musicDir = (char*)malloc(sizeof(char)*(strlen(".")+1) ); strcpy(settings.musicDir, "."); sprintf( buf, "%s/settings.ini", getConfigDir() ); FILE *f = fopen(buf, "r"); if(f) { while( fgets(buf, 128, f) ) { stripNewLine(buf); if(splitVals('=',buf, set, val)) { if( strcmp("soundvol", set)==0 ) { settings.soundVol = atoi(val); } else if( strcmp("musicvol", set)==0 ) { settings.musicVol = atoi(val); } else if( strcmp("wizclock", set)==0 ) { settings.wizClock = atoi(val); } else if( strcmp("wizvolume", set)==0 ) { settings.wizVol = atoi(val); } else if( strcmp("showfps", set)==0 ) { settings.showFps = atoi(val); } else if( strcmp("arcademode", set)==0 ) { settings.arcadeMode = atoi(val); } else if( strcmp("particles", set)==0 ) { settings.particles = atoi(val); } else if( strcmp("packdir", set)==0 ) { free(settings.packDir); settings.packDir = malloc(sizeof(char)*(strlen(val)+1) ); strcpy(settings.packDir,val); } else if( strcmp("playername", set)==0 ) { free(settings.playerName); settings.playerName = malloc(sizeof(char)*(strlen(val)+1) ); strcpy(settings.playerName,val); settings.playerName[10] = '\0'; //In case user edits file and make a longer than 10 chars name. } else if( strcmp("musicdir", set)==0 ) { free(settings.musicDir); settings.musicDir = malloc(sizeof(char)*(strlen(val)+1) ); strcpy(settings.musicDir, val); } else if( strcmp("usermusic", set)==0 ) { settings.userMusic=atoi(val); } else if( strcmp("disablemusic",set)==0 ) { settings.disableMusic=atoi(val); } else if( strcmp("uploadstats",set)==0 ) { //Only if the option is in the file, are we sure they had the choice. settings.firstRun=0; settings.uploadStats=atoi(val); } else if( strcmp("glwidth",set)==0 ) { settings.glWidth=atoi(val); } else if( strcmp("glheight",set)==0 ) { settings.glHeight=atoi(val); } else if( strcmp("glenable",set)==0 ) { settings.glEnable=atoi(val); } else if( strcmp("glfilter",set)==0 ) { settings.glFilter=atoi(val); } } } //Close file fclose( f ); } //Free the textbuffers free(buf); free(set); free(val); }
listItem* eoLoadScene( const char* fileName, engObjInitFunc objInit ) { char line[2048]; char set[1024]; char val[1024]; listItem* objs = NULL; engObj_s* obj = NULL; int i=0; char **vec; FILE* fp = fopen( fileName, "r" ); //Open file if( fp ) { objs = initList(); eoPrint("Loading %s ...", fileName); while( fgets(line, 1023, fp) ) { stripNewLine(line); if( strlen(line) > 2 ) { if( strcmp(line,"[model]") == 0 ) { obj = eoObjCreate( ENGOBJ_MODEL ); } else if( strcmp(line,"[sprite]" ) == 0 ) { eoPrint("Created Sprite entity."); obj = eoObjCreate( ENGOBJ_SPRITE ); } else if( strcmp(line,"[emitter]") == 0 ) { eoPrint("Created Emitter entity."); obj = eoObjCreate( ENGOBJ_PAREMIT ); } else if( strcmp(line, "[sound]" ) == 0 ) { eoPrint("Created Sound entity."); obj = eoObjCreate( ENGOBJ_SOUND ); } else if( strcmp(line,"[end]") == 0 ) { if( objInit ) { objInit(obj); } listAddData( objs, (void*)obj ); } else { if( splitVals( '=', line, set, val ) ) { if( strcmp(set,"class") == 0 ) { obj->className = malloc(strlen(val)+1); strcpy(obj->className,val); } else if( strcmp(set,"rot") == 0 ) { vec = explode(',', val, 3); obj->rot.x=atof(vec[0]); obj->rot.y=atof(vec[1]); obj->rot.z=atof(vec[2]); free(vec[0]); free(vec[1]); free(vec[2]); free(vec); } else if( strcmp(set,"pos") == 0 ) { vec = explode(',', val, 3); obj->pos.x=atof(vec[0]); obj->pos.y=atof(vec[1]); obj->pos.z=atof(vec[2]); free(vec[0]); free(vec[1]); free(vec[2]); free(vec); } else if( strcmp(set,"file") == 0 ) { //Todo: Cache models i=charrpos(val,'/')+1; strcpy(set, val+i); val[i]=0; obj->model = eoModelLoad( val, set ); } else { eoPrint("Unknown data: %s ('%s' and '%s')",line,set,val); } } else eoPrint("Invalid text: %s",line); } } // free(objName); } } else { eoPrint("Error, could not open file %s for reading...", fileName); } return(objs); }
int main(int argc, char* argv[]) { GJCType gjc; int initFlag, dataFlag, exitFlag=FALSE; char c[MAX_OPTION_INPUT+EXTRA_SPACES]; /* Initialise the Gloria Jean's Coffee system to a safe empty state. */ initFlag = systemInit(&gjc); /* Populate the Gloria Jean's Coffee system with data from the data files. */ /* Uncomment this line when you are ready to use command line arguments:*/ if(argv[1]==NULL||argv[2]==NULL){ printf("No argument found\n"); exit(EXIT_SUCCESS); } dataFlag = loadData(&gjc, argv[1], argv[2]); /* Testing to see if both systemInit(.) and loadData(.) are ok */ if (initFlag == FAILURE || dataFlag == FAILURE){ exit(EXIT_FAILURE); } /* Interactive menu providing user with access to the 9 menu options */ while(exitFlag==FALSE){ printf("Main Menu:\n"); printf("(1) Hot Drinks Summary\n"); printf("(2) Cold Drinks Summary\n"); printf("(3) Detailed Menu Report\n"); printf("(4) Add Menu Category\n"); printf("(5) Delete Menu Category\n"); printf("(6) Add Menu Item\n"); printf("(7) Delete Menu Item\n"); printf("(8) Save & Exit\n"); printf("(9) Abort\n"); printf("Select your option (1-9):"); fgets(c,MAX_OPTION_INPUT+EXTRA_SPACES,stdin); if(c[0]=='\n'){ printf("Invalid input\n"); }else if(stringIsInRange(c,MAX_OPTION_INPUT)==FALSE){ readRestOfLine(); printf("Invalid input\n"); }else{ stripNewLine(c); if(strcmp(c,"1")==0){ displaySummary(&gjc,HOT); }else if(strcmp(c,"2")==0){ displaySummary(&gjc,COLD); }else if(strcmp(c,"3")==0){ }else if(strcmp(c,"4")==0){ }else if(strcmp(c,"5")==0){ }else if(strcmp(c,"6")==0){ }else if(strcmp(c,"7")==0){ }else if(strcmp(c,"8")==0){ }else if(strcmp(c,"9")==0){ exitFlag=TRUE; } } } /* Deallocate all dynamically allocated memory. */ systemFree(&gjc); exit(EXIT_SUCCESS); }