Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Sean McEligot http://seanmceligot.blogspot.com/

« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS 

changewindows desktop background color every 30 seconds

Keep the eyes alert during long coding sessions. Compiled and tested with cygwin.

   1  
   2  #include "windows.h"
   3  #include <stdio.h>
   4  #include <stdlib.h>
   5  #include <sys/types.h>
   6  
   7  // gcc -g  -Wall    eyesaver.c   -o eyesaver
   8  
   9  void setcolor () {
  10  	int elements[3];
  11  	DWORD color[3];
  12  	elements[0] = COLOR_DESKTOP;
  13  	elements[1] = COLOR_ACTIVECAPTION;
  14  	elements[2] = COLOR_SCROLLBAR;
  15    color[0] = RGB (rand () % 0xFF, rand () % 0xFF, rand () % 0xFF);
  16    color[1] = RGB (rand () % 0xFF, rand () % 0xFF, rand () % 0xFF);
  17    color[2] = RGB (rand () % 0xFF, rand () % 0xFF, rand () % 0xFF);
  18    SetSysColors (3, elements, color);
  19  }
  20  
  21  void showcolor () {
  22    int color;
  23    color = GetSysColor (COLOR_DESKTOP);
  24    fprintf (stdout, "%X\n", color);
  25  }
  26  
  27  int main (int argc, char **argv) {
  28  	time_t now;
  29  	time(&now);
  30  
  31    srand (now);
  32    for (;;) {
  33      setcolor ();
  34      showcolor ();
  35      Sleep (30 * 1000);
  36    }
  37    return 0;
  38  }

edit compressed/encrypted files

This script will allow you to edit .gz, .bz2, .gpg files with vi or whatever the EDITOR environment variable points to. If the file is modified, it will be re-encode. It's real easy to add new file types.

   1  
   2  #! /usr/bin/env ruby
   3  require 'tempfile'
   4  require "fileutils"
   5  
   6  class Coder 
   7  	def initialize(encode_cmd, decode_cmd) 
   8  		@encode_cmd = encode_cmd
   9  		@decode_cmd = decode_cmd	
  10  	end
  11  	def encode(infile, outfile) 
  12  		cmd = @encode_cmd
  13  		cmd = cmd.sub("INFILE", infile).sub("OUTFILE", outfile)
  14  		print cmd, "\n"
  15  		system(cmd)
  16  	end
  17  	def decode(infile, outfile) 
  18  		cmd = @decode_cmd
  19  		cmd = cmd.sub("INFILE", infile).sub("OUTFILE", outfile)
  20  		print cmd, "\n"
  21  		system(cmd)
  22  	end
  23  	def to_s 
  24  		"encode #{@encode_cmd} decode #{@decode_cmd}"
  25  	end
  26  end
  27  
  28  $map ={ 
  29  	".gz"=>Coder.new("gzip < INFILE > OUTFILE", "gunzip < INFILE > OUTFILE"),
  30  	".bz2"=>Coder.new("bzip2 < INFILE > OUTFILE", "bunzip2 < INFILE > OUTFILE"),
  31  	".gpg"=>Coder.new("gpg -c < INFILE > OUTFILE", "gpg -d < INFILE > OUTFILE")
  32  }
  33  $editor = ENV["EDITOR"] || "vi"
  34  
  35  class Edit
  36  	def edit(encoded)
  37  		ext = File.extname(encoded)
  38  		coder = $map[ext]
  39  		p ext
  40  		p coder
  41  		tempfile = Tempfile.new("t")
  42  		editfile = tempfile.path
  43  		coder.decode(encoded, editfile)
  44  		before = File.stat(editfile).mtime
  45  		system("#{$editor} #{editfile}")
  46  		after = File.stat(editfile).mtime
  47  		if (before != after) 
  48  			# editfile was modified"
  49  			print "mv #{encoded} #{encoded+".bak"}\n"
  50  			FileUtils.mv encoded, encoded+".bak"
  51  			coder.encode( editfile, encoded)
  52  		end
  53  	end
  54  end
  55  
  56  if __FILE__ == $0 then
  57  	edit = Edit.new
  58  	ARGV.each do |filename|
  59  		edit.edit(filename)
  60  	end
  61  end

tar2zip.rb

Converts the given tarfile to a zipfile. You must have tar and zip in the system PATH. You will need to uncompress the tarfile or modify the script if you tar is compressed.

   1  
   2  #! /usr/bin/ruby
   3  class Tar2Zip
   4  	def run(dir, cmd)
   5  		pwd = Dir.getwd
   6  		print "cd ",dir, "\n"	
   7  		Dir.chdir(dir)
   8  		print cmd, "\n"
   9  		system(cmd)
  10  		print "cd ",pwd, "\n"	
  11  		Dir.chdir(pwd)
  12  	end
  13  	def main(argv)
  14  		tarfile=File.expand_path(argv[0])
  15  		zipfile= File.join(File.dirname(tarfile), File.basename(tarfile,".tar"))+".zip"
  16  		if File.exists?(zipfile)
  17  			print zipfile, " already exists\n"
  18  			return
  19  		end	
  20  		pwd = Dir.getwd
  21  		basename = File.basename(tarfile)
  22  		tmpdir = File.join("/tmp", basename)
  23  		Dir.mkdir(tmpdir)
  24  		run(tmpdir, "tar -xvf #{tarfile}")
  25  		run(tmpdir, "zip -rm #{zipfile} .")
  26  		Dir.unlink(tmpdir)
  27  	end
  28  end
  29  Tar2Zip.new.main(ARGV)

cvs printModified, removeUnmodified, chroot

This script perfoms three utilities on CVS working directories.

-m, --printModified print modified cvs files

This prints out the modified files by comparing the timestamps to those in the CVS/Entries file in all subdirectories.

--removeUnmodified delete unmodified cvs files

This deletes all unmodified files. This saves disk space. Later you can run "cvs update" and the unmodified files will be restored.

3. This changes the cvs root in all subdirectories
-d, --chroot [ROOT] change CVS root

-h, --help Show this message

   1  
   2  #! /usr/bin/ruby -W0
   3  
   4  require 'Time'
   5  require 'Find'
   6  require 'optparse'
   7  
   8  def splitEntryLine(line)
   9  	re = /\/(.*)\/(.*)\/(.*)\/(.*)\/(.*)/
  10  	m=re.match(line)
  11  	if m
  12  		return m[1..-1]	
  13  	else
  14  		return nil
  15  	end
  16  end
  17  		
  18  
  19  def parseCvsEntries(dirname, modified)
  20  #print "CVS: ", dirname
  21  		array = []
  22      File.open(File.join(dirname,"CVS", "Entries"), "r") { | entriesFile |
  23          entriesFile.each_line { | line |
  24  					name, ver, date, k, t=splitEntryLine(line)
  25  					if name
  26  							file=File.join(dirname, name)
  27  							if File.exists?(file) and not File.directory?(file)
  28  								orig=Time.parse(date)
  29  								orig = orig+orig.gmt_offset
  30  								mtime = File.new(file).mtime
  31  								if mtime != orig
  32  									if modified
  33  										array.push(file)
  34  									end
  35  								else
  36  									if not modified
  37  										array.push(file)
  38  									end
  39  								end
  40  							end
  41  					end
  42          }
  43       }
  44  	return array
  45  end
  46  
  47  def checkFile(dirname, filename, modified)
  48  	array = []
  49  	path = File.join(dirname, filename)
  50    if File.directory?(path)
  51  		if filename == "CVS"
  52  			array += parseCvsEntries(dirname, modified);
  53  		elsif not filename == "." and not filename == ".."
  54  			array += checkDir(path, modified)
  55  		end	
  56  	end
  57  	return array
  58  end
  59  
  60  def checkDir(dirname, modified)
  61  	array = Array.new
  62  	Dir.entries(dirname).each do |filename|
  63  		array += checkFile(dirname, filename, modified);
  64  	end 
  65  	return array
  66  end
  67  
  68  def removeUnmodified() 
  69  	list = checkDir(".", false)
  70  	list.each { |file|
  71  		cmd = "rm -vf "+ file
  72  		system(cmd)
  73  	}
  74  end
  75  def printModified() 
  76  	list = checkDir(".", true)
  77  	list.each { |file|
  78  		print file, "\n"
  79  	}
  80  end
  81  def change_root(new_root)
  82  	print new_root, "\n"
  83  	Find.find(".") do |path|
  84  		if File.basename(path) == "Root" and File.basename(File.dirname(path)) == "CVS"
  85  			print path, "\n"
  86  			File.open(path, "w") do |fout|
  87  				fout << new_root << "\n"
  88  			end
  89  		end
  90  	end
  91  	print new_root, "\n"
  92  end
  93  def hello()
  94  	print "Hi\n"
  95  end
  96  def usage() 
  97  	puts "cvs.rb"
  98  	puts "  options:"
  99  	puts "    --removeUnmodified"
 100  	puts "    --chroot(-d)"
 101  	puts "    --printModified(-m)"
 102  	puts "    --help(-h)"
 103  	exit 1
 104  end
 105  def main() 
 106  	opts = OptionParser.new {|opts|
 107  		opts.on("-e", "--eval [TEXT]" ,"evaluate ruby code") do |text| 
 108  			eval(text)
 109  		end
 110  		opts.on_tail("-h", "--help", "Show this message") do
 111            puts opts
 112            exit
 113          end
 114  		opts.on("--removeUnmodified","delete unmodified cvs files") do 
 115  			removeUnmodified	
 116  		end
 117  		opts.on("--printModified","-m", "print modified cvs files") do 
 118  			printModified
 119  		end
 120  		opts.on("-d", "--chroot [ROOT]" ,"change CVS root") do |root| 
 121  			change_root(root)
 122  		end
 123  	}.parse!
 124  end
 125  main
« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS