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

Mathieu Poussin kedare.free.fr

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

Simple base64 encoder/decoder in command-line with D

   1  
   2  import std.base64;
   3  import std.stdio;
   4  import std.array;
   5  void main(string[] args)
   6   {
   7  	try {
   8  		string action = args[1];
   9  		string source = args[2];
  10  		string dest;
  11  		if(action == "encode") {
  12  			dest = std.base64.encode(source);
  13  		}
  14  		else if(action == "decode") {
  15  			dest = std.base64.decode(source);
  16  		}
  17  		else {
  18  			writeln("Error: Bad Action");
  19  			return 1;
  20  		}
  21  		writeln(dest);
  22  		return 0;
  23  	}
  24  	catch(ArrayBoundsError err) {
  25  		writefln("Error: Missing argument (%s action string)", args[0]);
  26  	}
  27   }
  28  


Usage: b64utils [de/en]code mytext

Unix Console Styler

This modue can be used to apply many style on Unix* terminals

   1  
   2  # Unix Console Style
   3  # You can use this module to apply a style on the terminal
   4  #
   5  # <b>DON'T WORK ON WINDOWS (Only on *nix Terminal)</b>
   6  
   7  module UnixConsoleStyler
   8    class StyleNotFoundException < Exception; end
   9    
  10    # Availables Styles
  11    STYLE = {
  12        :default    =>    "\033[0m",
  13      	# styles
  14      	:bold       =>    "\033[1m",
  15      	:underline  =>    "\033[4m",
  16      	:blink      =>    "\033[5m",
  17      	:reverse    =>    "\033[7m",
  18      	:concealed  =>    "\033[8m",
  19      	# font colors
  20      	:black      =>    "\033[30m", 
  21      	:red        =>    "\033[31m",
  22      	:green      =>    "\033[32m",
  23      	:yellow     =>    "\033[33m",
  24      	:blue       =>    "\033[34m",
  25      	:magenta    =>    "\033[35m",
  26      	:cyan       =>    "\033[36m",
  27      	:white      =>    "\033[37m",
  28      	# background colors
  29      	:on_black   =>    "\033[40m", 
  30      	:on_red     =>    "\033[41m",
  31      	:on_green   =>    "\033[42m",
  32      	:on_yellow  =>    "\033[43m",
  33      	:on_blue    =>    "\033[44m",
  34      	:on_magenta =>    "\033[45m",
  35      	:on_cyan    =>    "\033[46m",
  36      	:on_white   =>    "\033[47m" }
  37    
  38    # Methods to use if you want to apply a style
  39    def UnixConsoleStyler::apply_style(style)
  40      if STYLE.has_key? style
  41        STDOUT.write STYLE[style]
  42      else
  43        raise StyleNotFoundException, "Style #{style} not found"
  44      end
  45    end
  46    
  47  end

Gradient in an applet

   1  
   2  import java.awt.*;
   3  import java.applet.*;
   4  import javax.swing.*;
   5  
   6  public class JavaAppDe extends JApplet {
   7  	
   8      public void init() {
   9      }
  10  	
  11      public void paint (Graphics g) {
  12          super.paint(g);
  13  	int base = 0;
  14  	while(base<255) {
  15  		g.setColor(new Color(base,base,base));
  16  		g.drawLine(0,base,255,base);
  17  		base++;
  18  	}
  19      }
  20  }

Avatar Resizer

I go on a lot of Bulletin Board, every has its own limits of size for the avatars, instead of the resize manually I created a script which does it for me with RMagick

   1  
   2  #!/usr/bin/ruby
   3  require "RMagick"
   4  $SIZES = [80 , 100 , 110 , 128]
   5  
   6  if !ARGV[0]
   7    puts "Usage: mk_avatars.rb SourceAvatarPath"
   8    exit
   9  end
  10  
  11  image = Magick::Image.read(ARGV[0]).first
  12  $SIZES.each do |sz|
  13    puts "Generating Avatar : #{sz}"
  14    out = image.thumbnail(sz,sz)
  15    file = "out_#{sz}.#{image.format}"
  16    out.write(file)
  17  end
« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS