All pastes #908148 Raw Edit

Stuff

public text v1 · immutable
·published 2008-02-18 04:18 UTC
rendered paste body
  	Sign Out
Compose   Addresses   Folders   Options   Search   Help   Calendar   Fetch  Notes   	Clemson University


Viewing a text attachment - View message
Download this as a file

/* 
 *        Usage:
 *
 *                ./a.out DIRECTORY_NAME
 *  
 *        This program is supposed to go into a directory and change the alpha level of all
*.vmt files
 *        within that directory.  I want it to dive into subdirectories but I haven't yet
implemented that; however,
 *        I think that a recursive approach with the right tactics could get teh job done. 
I have the flags set for
 *        directory regognition already.
 *
 *        Example file :
 *
 *        "LightmappedGeneric"
 *        {
 *                "$basetexture" "Brick/brickwall003d"
 *                "$surfaceprop" "brick"
 *                "%keywords" "c17downtown"
 *                "$alpha"        "0.50"
 *        }
 *  
 *        change the alpha level to "1.00"
 *
 *
 *        Finished File Example:
 *
 *
 *        "LightmappedGeneric"
 *        {
 *                "$basetexture" "Brick/brickwall003d"
 *                "$surfaceprop" "brick"
 *                "%keywords" "c17downtown"
 *                "$alpha"        "1.00"
 *        }
 *
 */


#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <string.h>
#include <unistd.h>

#define MAXLINE 256

int haxUp (FILE *);

int main(int argc, char *argv[]){

        FILE *fp;                                //file stream
        DIR *dirp;                                //pointer to a directory stream
        struct dirent *dp;                //this is the directory information stored by the OS
        struct stat buf;                //each directory/file contains one of these with info in it
        char *REQUEST_DIR;        //string for the directory name
        
        if (argc == 2)        //checks for correct parameters
                REQUEST_DIR = argv[1];
        else {
                fprintf(stderr, "Bad arguments. File path not specified. (use ./a.out DIR_NAME)");
                exit(EXIT_FAILURE);
        }

        dirp = opendir(REQUEST_DIR);        //open the directory stream
        chdir(REQUEST_DIR);                //system function to move the os into that directory
 
        //look at each entry in turn
        while ((dp = readdir(dirp)) != NULL) {

                //access stat the file to get more information
                if (stat(dp->d_name, &buf) == -1)        //places the file name or dir name into buf
                        perror("stat error\n");
                if (S_ISDIR(buf.st_mode))                //checks if the dir/file is a dir
                        printf("%s is a directory\n", dp->d_name);        //this is where the recursion might go
                else if  (S_ISREG(buf.st_mode) && strstr(dp->d_name, ".vmt") != NULL) {        //found a
file
                        fp = fopen( dp->d_name, "rw+s" );                //opens the file stream
                        if(fp != NULL) {        //error handling
                                if (haxUp(fp) == 0)                //do hax here
                                        printf ("Succesfully edited: %s\n", dp->d_name);
                        } else
                                continue;
                }
        }
        
        //good practice here
        (void) closedir(dirp);
        (void) fclose(fp);
        
        return 0;
}


//This is the hax subroutine - it seems to work well
int haxUp (FILE *fp) {
        //lolz i stole this maxline stuff from Harlan
        char line[MAXLINE];
        char var_name[MAXLINE];
        char var_value[MAXLINE];
        char junk[MAXLINE];
        int num_items;
        long int offset;
        while (fgets(line, MAXLINE, fp) != NULL) {        //reads one line at a time
                offset = ftell(fp);        //sets the line position (offset from SEEK_SET which is start)
                num_items = sscanf(line, "%s%s%s", var_name, var_value, junk);                //counts the num
of items scanned in (maybe useful later)
                if (strcmp(var_name, "\"$alpha\"") == 0 && strcmp(var_value,"0.50") != 0)
{        //checks to see if the first value is "$alpha"
                        fseek(fp,offset-(sizeof("\t\"$alpha\"\t\"0.50\"")),SEEK_SET);        //skips back a line
to make sure the alpha line is overwritten
                        printf("changing alpha level.....\n");
                        sprintf(line, "\t\"$alpha\"\t\"0.50\"");                //writes the new data to a string
                        fputs(line,fp);        //string meets file
                        return 0;
                }
        }
        return -1; //returns -1 on no find or if alpha already == "1.00"
}