CS315: Algorithm Design PA 2

Anything related in any way to game development as a whole is welcome here. Tell us about your game, grace us with your project, show us your new YouTube video, etc.

Moderator: PC Supremacists

Post Reply
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

CS315: Algorithm Design PA 2

Post by MarauderIIC »

Programming assignment 2. Bash script. Feel free to fix anything if you want, I taught myself bash scripting like, yesterday :P
Click here to see the hidden message (It might contain spoilers)

Code: Select all

cat makeAlbum.bash
#!/usr/bin/env bash
# HEADER GOES HERE
# CS 315-001 S08
# Steven A Wilson (Alex)

# Global counters =p
declare totalDirs=0
declare totalFiles=0
declare totalHeight=0
declare runDir=`pwd`
# Get everything after the user
runDir=${runDir##$HOME}
echo $runDir
DJPEG="convert"
FONTNAME="\"Courier\""

# Recursively counts the number of dirs in the dir structure
# - Keeps track of a max depth while doing so
# Recursively counts the number of files in the dir structure
# Stores each in their appropriate (global) counters
count_stuff () {

        declare localHeight=0
        let localHeight=$1

        for i in *; do

                # Count all the directories
                if [ -d "$i" ]; then
                        let totalDirs=totalDirs+1
                        # Push & pop so we can count tree height
                        # Hide the output so it's not written to our site
                        pushd $i > /dev/null
                        localHeight=`dirs -p | wc -l`
                        if [ $localHeight -gt $totalHeight ]; then
                                let totalHeight=localHeight
                        fi;
                        # As we come across a dir, count the stuff in it too
                        count_stuff $localHeight
                        popd > /dev/null
                fi;

                # Count all the files
                if [ -f "$i" ]; then
                        let totalFiles=totalFiles+1
                fi;
        done;
}

# General fn to output the header
# Figured it read better this way than in a global const
echo_header () {
        echo "<HTML><BODY><FONT>"
}

# General fn to output the footer
# Figured it read better this way than in a global const
echo_footer () {
        echo "</FONT></BODY></HTML>"
}

# Special fn to generate the main index file at dir structure's head
# Prints the header, counts everything, outputs the structure analysis,
# then calls the regular index gen w/ a no-header flag (so it doesnt write
# the header again) to output the directory contents.
generate_first_index () {
        echo_header
        count_stuff 0
        echo "Main index. Tree readout: <BR>";
        echo "$totalDirs directories with $totalFiles files, ";
        echo "maxxing out at $totalHeight levels deep.<BR>";
        generate_index no-header
}

# Outputs the contents of the directory (to make the .html, redirect)
generate_index () {

        # Essentially only the topmost index.html will cause this to happen
        # since it has special text, it writes its own header.
        if [ "$1" != "no-header" ]; then
                echo_header
        fi;

        # Holds the directory path (we compile it reverse of how it
        # is printed so that we have the deepest dir -- current dir --
        # on the bottom.)
        local dirList

        echo "<HR>"
        # Print path to this directory with current directory on bottom
        # Fixme to link each part of the dir, ex /~sawils7 is a link
        # and /project2 is a link, etc.
        for i in ${DIRSTACK[*]}; do
                local val
                local text
                local dots
                dots=""
                # Eat the dir we're running this from
                val=${i/~${runDir}}
                # Strip the leading slash...
                val=${val:1}
                text=${val##*/}
                if [ $i = ${DIRSTACK[${#DIRSTACK[*]}-1]} ]; then
                        text="${text}root"
                fi;
                # Make the path relative
                for (( j=0 ; j < ${#DIRSTACK[*]}-1 ; j=j+1 )) ; do
                        dots="${dots}../"
                done;
                dirList="<A HREF="${dots}${val}\">/$text</A>${dirList}"
        done;
        echo "$dirList<BR>"
        echo "<HR>"
        echo "Contents:<BR>"

        local subDirs
        local theFiles
        echo "&nbsp.<BR>&nbsp&nbsp Directories:<BR>"
        # Print all the directories in order of incr depth
        for i in *; do
                if [ -d "$i" ]; then
                        subDirs="${subDirs}&nbsp&nbsp|&gt <A HREF="$i\">$i</A><BR>"
                fi;
        done;
        echo $subDirs

        echo "&nbsp&nbsp| Files:<BR>"

        # Print all the files (so that everything isn't all jumbled together)
        ## Print everything but image files
        for i in *; do
                if [ -f "$i" ]; then
                        # Skip all the image files
                        # (As far as I could find out, if doesnt support
                        # the 'words' type, hence the case)
                        case $i in
                          *.tiff | *.tif | *.jpg | *.jpeg | *.JPG | *.JPEG)
                                  echo "";;
                          * )
                                  echo "&nbsp&nbsp|- <A HREF="$i\">$i</A><BR>"
                        esac
                fi;
        done;

        ## Print image files with a thumbnail
        local tableWidth
        local tableCount
        tableWidth=2
        tableCount=0
        echo "Pictures:<BR>"
        echo "<TABLE><TR>"
        # Remove old thumbnails
        rm -rf thumb.*.jpg
        for i in *.tiff *.tif *.jpg *.jpeg *.JPG *.JPEG; do
                if [ -f "$i" ]; then
                        let tableCount=tableCount+1
                        echo "<TD>"
                        ${DJPEG} "$i" -resize 120x120 "thumb.$i.jpg"
                        echo "<A HREF="$i\"><image></A>"
                        echo "<BR>$i"
                        echo "</TD>"
                        if [ $tableCount -eq $tableWidth ]; then
                                echo "</TR><TR>"
                        fi;
                fi;
        done;
        echo "</TR>"
        echo_footer
}

traverse () {
        for i in *; do
                if [ -d "$i" ]; then
                        pushd $i
                        traverse
                        # Remove any old one
                        rm -f ./index.html
                        generate_index > index.html
                        popd
                fi;
        done;
}

# Remove any old indices so that we can make new ones no problem.
rm -f ./index.html
generate_first_index >> index.html
traverse
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
Post Reply