DZone 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

Snippets

  • submit to reddit

Recent Snippets

                    require 'open-uri'
require 'tmpdir'
require 'fileutils'
require 'digest'

def wget(url)
  cachedir = File.join( Dir.tmpdir, 'httpcache' )
  filename = File.join( cachedir, Digest::MD5.hexdigest(url) )
  
  FileUtils.mkdir_p(cachedir) if !File.exists?(cachedir)
  if File.exist?(filename)
    body = File.binread(filename)
  else
    body = open(url).read
    File.open(filename, "wb") do |saved_file|
      saved_file.write(body)
    end
  end

  body
end
                
                    require 'rbconfig'

def is_root?
  if RbConfig::CONFIG['host_os'] =~ /mswin|win32|dos|mingw|cygwin/i
    require 'win32/registry'
    
    begin
      Win32::Registry::HKEY_USERS.open('S-1-5-19') {|reg| }
      true
    rescue
      false
    end
  else
    (ENV['USER'] == 'root')
  end
end
                
                    # Sample 1: 
RewriteRule ^about$ about.php [L]
 
# Sample 2: 
RewriteCond /%{REQUEST_FILENAME}.php -f
RewriteRule ^([a-zA-Z0-9_-\s]+)/$ /$1.php                
                    # Source: http://www.apphp.com/index.php?snippet=htaccess-different-directory-index-page

# Sample 1: 
DirectoryIndex index2.html
 
# Sample 2: 
DirectoryIndex index2.php                
                    # Sample 1: 
DirectoryIndex index2.html
 
# Sample 2: 
DirectoryIndex index2.php                
                    -- 1st example
UPDATE files SET filepath = REPLACE(filepath,'path/to/search','path/to/replace');
 
-- 2nd example
UPDATE customers SET address = REPLACE(address,'_CODE_',postcode);                
                    <!-- First way -->
<input type="button" value="This is
a Really
Tall
 Button">
<!-- Second way -->
<button>This is<br/>a Really<br/>Tall<br/>Button</button>                
                    <!-- This is a comment. Comments are not displayed in the browser. -->
 
<div id="header">
   <p>Some text</p>
</div> <!-- END div-header -->
 
<script type="text/javascript">
<!--
function ShowMessage()
{
  alert("Hello World!")
}
//-->
</script>

                
                    //The Sample code shows how to export VSD to VDX

//[C# Sample]
 
//Call the diagram constructor to load diagram from a VSD file
Diagram diagram = new Diagram("D:\\Drawing1.vsd");
this.Response.Clear();
this.Response.ClearHeaders();
this.Response.ContentType = "application/vnd.ms-visio";
this.Response.AppendHeader("Content-Disposition", "attachment; filename=Diagram.vdx");
this.Response.Flush();
System.IO.Stream vdxStream = this.Response.OutputStream;

//Save input VSD as VDX
diagram.Save(vdxStream, SaveFileFormat.VDX);
this.Response.End();

//[VB.NET Code Sample]

'Call the diagram constructor to load diagram from a VSD file
Dim diagram As New Diagram("D:\Drawing1.vsd")

Me.Response.Clear()
Me.Response.ClearHeaders()
Me.Response.ContentType = "application/vnd.ms-visio"
Me.Response.AppendHeader("Content-Disposition", "attachment; filename=Diagram.vdx")
Me.Response.Flush()
Dim vdxStream As System.IO.Stream = Me.Response.OutputStream

'Save inpupt VSD as VDX
diagram.Save(vdxStream, SaveFileFormat.VDX)
Me.Response.End()

//The Sample code shows how to export VSD to VSX format.

[C# Code Sample]

// Call the diagram constructor to load diagram from a VSD file
Diagram diagram = new Diagram("D:\\Drawing1.vsd");
this.Response.Clear();
this.Response.ClearHeaders();
this.Response.ContentType = "application/vnd.ms-visio";
this.Response.AppendHeader("Content-Disposition", "attachment; filename=Diagram.vsx");
this.Response.Flush();
System.IO.Stream vsxStream = this.Response.OutputStream;

//Save input VSD as VSX
diagram.Save(vsxStream, SaveFileFormat.VSX);
this.Response.End()

//[VB.NET Code Sample]

'Call the diagram constructor to load diagram from a VSD file
Dim diagram As New Diagram("D:\Drawing1.vsd")

Me.Response.Clear()
Me.Response.ClearHeaders()
Me.Response.ContentType = "application/vnd.ms-visio"
Me.Response.AppendHeader("Content-Disposition", "attachment; filename=Diagram.vsx")
Me.Response.Flush()
Dim vsxStream As System.IO.Stream = Me.Response.OutputStream

'Save input VSD as VSX
diagram.Save(vsxStream, SaveFileFormat.VSX)
Me.Response.End()
                
                    <style type="text/css">
.break {
    -ms-word-break: break-all;
    word-break: break-all;
    word-break: break-word; 
    -webkit-hyphens: auto;
    -moz-hyphens: auto;
    hyphens: auto;
}
 
.ellipsis {
    width: 250px;
    white-space: nowrap;
    overflow: hidden;
    -ms-text-overflow: ellipsis; /* Required for IE8 */
    -o-text-overflow: ellipsis; /* Required for Opera */
    text-overflow: ellipsis;
}
</style>