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

Adios , T. http://adios.mine.nu/blog

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

CSS Hacks for opacity

in Internet Explorer 4, Gecko-based browsers, and Safari.
   1  
   2  #transparent {
   3      filter: alpha(opacity=50);
   4      -moz-opacity: 0.5;
   5      opacity: 0.5;
   6  }

fibonacchi example for ruby yield

   1  
   2  def fibUpTo(max)
   3    i1, i2 = 1, 1        # 平行賦值
   4    while i1 <= max
   5      yield i1
   6      i1, i2 = i2, i1+i2
   7    end
   8  end
   9  fibUpTo(1000) { |f| print f, " " }

Dirty replace implement in C

// description of your code here

   1  
   2  while (feof (fp) == 0)
   3  {	
   4  	memset (file_buf, 0, file_size * sizeof (char));
   5  	memset (line_buf, 0, sizeof (line_buf));
   6  
   7  	if (fgets (line_buf, sizeof (line_buf), fp) == NULL && ferror (fp))
   8  	{
   9  		perror (file_path);
  10  		fclose (fp);
  11  		free (file_buf);
  12  		return;
  13  	}
  14  
  15  	line_len = strlen (line_buf);
  16  
  17  	if ((begin = (*(opt->str_str))(line_buf, opt->word)))
  18  	{
  19  		total_matches++;
  20  		/* Example :{{{
  21  			line_buf = '1234567890',  word = '5', replacement = '6'
  22  
  23  			begin = address_of(5)
  24  					address_of(5) - address_of(1) - line_len = -6
  25  			
  26  			fp now => '1234567890_' , at addressof(_)
  27  			fseek (fp, -6)  => address_of (5)
  28  
  29  			begin_offset = address_of (5);
  30  			end_offset = begin_offset + strlen(repl) - 1 = address_of (5)
  31  			
  32  			so we should seek to end_offset + 1 to fread the rest.
  33  			
  34  			after reading.
  35  
  36  			we fseek to the begin_offset, then write replacement, and write back the rest.
  37  
  38  			finally we should seek to end_offset + 1 to read a new line
  39  
  40  			}}} */
  41  		if (fseek (fp, begin - line_buf - line_len, SEEK_CUR) < 0)
  42  		{
  43  			perror (file_path);
  44  			fclose (fp);
  45  			free (file_buf);
  46  			return;
  47  		}
  48  
  49  		word_begin = ftell (fp);
  50  		word_end = word_begin + (long) (strlen (opt->replacement) - 1);
  51  
  52  		fseek (fp, word_end + 1, SEEK_SET);
  53  
  54  		n = fread (file_buf, sizeof (char), file_size, fp);	// read until eof;
  55  
  56  		fseek (fp, word_begin, SEEK_SET);
  57  		
  58  		fwrite (opt->replacement, sizeof (char), strlen (opt->replacement), fp);
  59  		fwrite (file_buf, sizeof (char), n, fp);
  60  
  61  		fseek (fp, word_end + 1, SEEK_SET);
  62  	}
  63  
  64  } 

Using gsub with blocks to strip attributes from HTML tags

originally post here:
http://henrik.nyh.se/2007/03/03/using-gsub-with-blocks-to-strip-attributes-from-html-tags/
   1  
   2  html = 'Getting <a href="#" id="foo">rid</a> of <code id="bar">id
attributes, but not in text: id="not this".'

html.gsub(/<(.*?)>/) {|innards| innards.gsub(/ id=("|').*?\1/, '') }

# => Getting rid of
   1  id
attributes, but not in text: id="not this".

Strip html tags

Originally from segabor@textsnippets
The regex below removes html tags from string (untested).

   1  
   2  str = <<HTML_TEXT
   3  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   4     "http://www.w3.org/TR/html4/loose.dtd">
   5  <html>
   6  <body>
   7    <h1>Application error</h1>
   8    <p>Change this error message for exceptions thrown outside of an action (like 
   9  in Dispatcher setups or broken Ruby code) in public/500.html</p>
  10  </body>
  11  </html>
  12  HTML_TEXT
  13  
  14  puts str.gsub(/<\/?[^>]*>/, "")

arguments of methods with easy default setting

   1  
   2  def my_method(opts={})   
   3    {:arg_one => 'foo', :arg_two => 'two'}.merge!(opts)
   4  end 

active record test

ActiveRecord testing code

   1  
   2  require 'rubygems'
   3  require 'active_record'
   4  
   5  ActiveRecord::Base.establish_connection(
   6    :adapter => "mysql",
   7    :username => "root",
   8    :password => "root",
   9    :database => "blog",
  10    :host => "localhost" )
  11  
  12  class Blog < ActiveRecord::Base 
  13    set_table_name "posts"
  14    set_primary_key "id"
  15  end
  16  
  17  blog = Blog.find(:all)
  18  blog.each { |row|
  19    p row
  20  }

Set::extract - parsing an RSS feed for all post titles

// description of your code here
http://www.thinkingphp.org/2007/02/24/cake-12s-set-class-eats-arrays-for-breakfast/

The following code will produce the result:
Array
(
[0] => How-to: Use Html 4.01 in CakePHP 1.2
[1] => Looking up foreign key values using Model::displayField
[2] => Bug-fix update for SVN/FTP Deployment Task
[3] => Access your config files rapidly (Win32 only)
[4] => Making error handling for Model::save more beautiful in CakePHP
[5] => Full content RSS feed
[6] => Visual Sorting - Some Javascript fun I had last night
)

   1  
   2  uses('Xml');
   3   
   4  $feed = xmltoArray(new XML('http://feeds.feedburner.com/thinkingphp'));
   5  $postTitles = Set::extract($feed, 'rss.channel.item.{n}.title'); 
   6  

parsing an RSS feed for all post titles

// description of your code here
http://www.thinkingphp.org/2007/02/24/cake-12s-set-class-eats-arrays-for-breakfast/

   1  
   2  PHP:
   3  
   4  function xmltoArray($node)
   5  {
   6      $array = array();
   7      
   8      foreach ($node->children as $child)
   9      {
  10          if (empty($child->children))
  11          {
  12              $value = $child->value;
  13          }
  14          else
  15          {
  16              $value = xmltoArray($child);
  17          }
  18          
  19          $key = $child->name;
  20          
  21          if (!isset($array[$key]))
  22          {
  23              $array[$key] = $value;
  24          }
  25          else 
  26          {
  27              if (!is_array($array[$key]) || !isset($array[$key][0]))
  28              {
  29                  $array[$key] = array($array[$key]);
  30              }
  31              
  32              $array[$key][] = $value;
  33          }
  34      }
  35      
  36      return $array;
  37  } 

Geek InvSqrt()

http://www.beyond3d.com/articles/fastinvsqrt/

   1  
   2  float InvSqrt (float x){
   3     float xhalf = 0.5f*x;
   4     int i = *(int*)&x;
   5     i = 0x5f3759df - (i>>1);
   6     x = *(float*)&i;
   7     x = x*(1.5f - xhalf*x*x);
   8     return x;
   9  }
« Newer Snippets
Older Snippets »
Showing 1-10 of 10 total  RSS