<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: Tvrtko's Code Snippets</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 08 Aug 2008 21:03:42 GMT</pubDate>
    <description>DZone Snippets: Tvrtko's Code Snippets</description>
    <item>
      <title>Listing the files and subdirectories in C - Linux</title>
      <link>http://snippets.dzone.com/posts/show/5734</link>
      <description>// program lists the files and subdirectories within a given directory in full path&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#include &lt;stdio.h&gt;&lt;br /&gt;#include &lt;stdlib.h&gt;&lt;br /&gt;#include &lt;string.h&gt;&lt;br /&gt;#include &lt;dirent.h&gt;&lt;br /&gt;&lt;br /&gt;char *path_cat (const char *str1, char *str2);&lt;br /&gt;&lt;br /&gt;int main () {&lt;br /&gt;	struct dirent *dp;&lt;br /&gt;&lt;br /&gt;        // enter existing path to directory below&lt;br /&gt;	const char *dir_path="/path/to/directory/to/list";&lt;br /&gt;	DIR *dir = opendir(dir_path);&lt;br /&gt;	while ((dp=readdir(dir)) != NULL) {&lt;br /&gt;		char *tmp;&lt;br /&gt;		tmp = path_cat(dir_path, dp-&gt;d_name);&lt;br /&gt;		printf("%s\n", tmp);&lt;br /&gt;		free(tmp);&lt;br /&gt;		tmp=NULL;&lt;br /&gt;	}&lt;br /&gt;	closedir(dir);&lt;br /&gt;	return 0;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;char *path_cat (const char *str1, char *str2) {&lt;br /&gt;	size_t str1_len = strlen(str1);&lt;br /&gt;	size_t str2_len = strlen(str2);&lt;br /&gt;	char *result;&lt;br /&gt;	result = malloc((str1_len+str2_len+1)*sizeof *result);&lt;br /&gt;	strcpy (result,str1);&lt;br /&gt;	int i,j;&lt;br /&gt;	for(i=str1_len, j=0; ((i&lt;(str1_len+str2_len)) &amp;&amp; (j&lt;str2_len));i++, j++) {&lt;br /&gt;		result[i]=str2[j];&lt;br /&gt;	}&lt;br /&gt;	result[str1_len+str2_len]='\0';&lt;br /&gt;	return result;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 08 Jul 2008 01:13:50 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5734</guid>
      <author>Tvrtko (Tvrtko)</author>
    </item>
    <item>
      <title>C functions for getting disk space information in Linux OS</title>
      <link>http://snippets.dzone.com/posts/show/5601</link>
      <description>These are 2 C functions for getting disk capacity and disk free space in megabytes into char array&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#include &lt;sys/statvfs.h&gt;&lt;br /&gt;#include &lt;glib.h&gt;&lt;br /&gt;&lt;br /&gt;gchar *&lt;br /&gt;g_get_capacity ( gchar * dev_path)&lt;br /&gt;{&lt;br /&gt;	unsigned long long result = 0;&lt;br /&gt;	int n;&lt;br /&gt;	gchar s_cap[50];&lt;br /&gt;	gchar * ss_cap = "N/A";&lt;br /&gt;	struct statvfs sfs;&lt;br /&gt;	if ( statvfs ( dev_path, &amp;sfs) != -1 )&lt;br /&gt;	{&lt;br /&gt;		result = (unsigned long long)sfs.f_bsize * sfs.f_blocks;&lt;br /&gt;	}&lt;br /&gt;	if (result &gt; 0)&lt;br /&gt;	{&lt;br /&gt;		double f_cap = (double)result/(1024*1024);&lt;br /&gt;		n = sprintf(s_cap, "%.2f Mb", f_cap);&lt;br /&gt;		ss_cap = g_strdup(s_cap);&lt;br /&gt;	}&lt;br /&gt;	return ss_cap;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;gchar * &lt;br /&gt;g_get_free_space ( gchar * dev_path)&lt;br /&gt;{&lt;br /&gt;	unsigned long long result = 0;&lt;br /&gt;	int n;&lt;br /&gt;	gchar s_cap[50];&lt;br /&gt;	gchar * ss_cap = "N/A";&lt;br /&gt;	struct statvfs sfs;&lt;br /&gt;	if ( statvfs ( dev_path, &amp;sfs) != -1 )&lt;br /&gt;	{&lt;br /&gt;		result = (unsigned long long)sfs.f_bsize * sfs.f_bfree;&lt;br /&gt;	}&lt;br /&gt;	if (result &gt; 0)&lt;br /&gt;	{&lt;br /&gt;		double f_cap = (double)result/(1024*1024);&lt;br /&gt;		n = sprintf(s_cap, "%.2f Mb", f_cap);&lt;br /&gt;		ss_cap = g_strdup(s_cap);&lt;br /&gt;	}&lt;br /&gt;	return ss_cap;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 06 Jun 2008 16:41:22 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5601</guid>
      <author>Tvrtko (Tvrtko)</author>
    </item>
    <item>
      <title>C++ code to HTML</title>
      <link>http://snippets.dzone.com/posts/show/5473</link>
      <description>generate html colored file from a source file&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#include &lt;iostream&gt;&lt;br /&gt;#include &lt;string&gt;&lt;br /&gt;#include &lt;fstream&gt;&lt;br /&gt;#include &lt;sstream&gt;&lt;br /&gt;#include &lt;iterator&gt;&lt;br /&gt;using namespace std;&lt;br /&gt;&lt;br /&gt;#include &lt;boost/regex.hpp&gt;&lt;br /&gt;&lt;br /&gt;const char * expressions =&lt;br /&gt;	// comments&lt;br /&gt;	"(//[^\\n]*|/\\*.*?\\*/)|"&lt;br /&gt;	// precompilers&lt;br /&gt;	"(^[[:blank:]]*#[^\\n]*)|"&lt;br /&gt;	// string literals&lt;br /&gt;	"(\"(?:[^\\\\\"]|\\\\.)*\"|'(?:[^\\\\']|\\\\.)*')|"&lt;br /&gt;	// floating point literal&lt;br /&gt;	"(\\&lt;[[:digit:]]+\\.[[:digit:]]+)|"&lt;br /&gt;	//  integer literal&lt;br /&gt;	"(\\&lt;[[:digit:]]+\\&gt;)|"&lt;br /&gt;	// boolean literal&lt;br /&gt;	"((?:\\&lt;true\\&gt;)|(?:\\&lt;false\\&gt;))|"&lt;br /&gt;	// keywords&lt;br /&gt;	"\\&lt;(__asm|__cdecl|__declspec|__export|__far16|__fastcall|__fortran|__import"&lt;br /&gt;	"|__pascal|__rtti|__stdcall|_asm|_cdecl|__except|_export|_far16|_fastcall"&lt;br /&gt;	"|__finally|_fortran|_import|_pascal|_stdcall|__thread|__try|asm|auto|bool"&lt;br /&gt;	"|break|case|catch|cdecl|char|class|const|const_cast|continue|default|delete"&lt;br /&gt;	"|do|double|dynamic_cast|else|enum|explicit|extern|false|float|for|friend|goto"&lt;br /&gt;	"|if|inline|int|long|mutable|namespace|new|operator|pascal|private|protected"&lt;br /&gt;	"|public|register|reinterpret_cast|return|short|signed|sizeof|static|static_cast"&lt;br /&gt;	"|struct|switch|template|this|throw|true|try|typedef|typeid|typename|union|unsigned"&lt;br /&gt;	"|using|virtual|void|volatile|wchar_t|while)\\&gt;";&lt;br /&gt;&lt;br /&gt;const char * format = &lt;br /&gt;	"(?1&lt;font color = \"#999999\"&gt;&lt;i&gt;$&amp;&lt;/i&gt;&lt;/font&gt;)"&lt;br /&gt;	"(?2&lt;font color = \"#000099\"&gt;$&amp;&lt;/font&gt;)"&lt;br /&gt;	"(?3&lt;font color = \"#009900\"&gt;$&amp;&lt;/font&gt;)"&lt;br /&gt;	"(?4&lt;font color = \"#996600\"&gt;$&amp;&lt;/font&gt;)"&lt;br /&gt;	"(?5&lt;font color = \"#999900\"&gt;$&amp;&lt;/font&gt;)"&lt;br /&gt;	"(?6&lt;font color = \"#660000\"&gt;&lt;b&gt;$&amp;&lt;/b&gt;&lt;/font&gt;)"&lt;br /&gt;	"(?7&lt;font color = \"#336699\"&gt;&lt;b&gt;$&amp;&lt;/b&gt;&lt;/font&gt;)";&lt;br /&gt;&lt;br /&gt;string help = &lt;br /&gt;	"Usage:"&lt;br /&gt;	"\n\nccode2html &lt;input file&gt; &lt;output file&gt;"&lt;br /&gt;	"\n\nIf output filename is omitted its name it will be &lt;input file&gt;.html";&lt;br /&gt;string code_tag_start = "&lt;PRE&gt;";&lt;br /&gt;string code_tag_end = "&lt;/PRE&gt;";&lt;br /&gt;&lt;br /&gt;void ccode2html (string input, string output);&lt;br /&gt;string test();&lt;br /&gt;&lt;br /&gt;extern const char* pre_expression = "(&lt;)|(&gt;)|(&amp;)|\\r";&lt;br /&gt;extern const char* pre_format = "(?1\\&amp;lt;)(?2\\&amp;gt;)(?3&amp;amp;)";&lt;br /&gt;&lt;br /&gt;int main (int argc, char *argv[])&lt;br /&gt;{&lt;br /&gt;	string input_file;&lt;br /&gt;	string output_file;&lt;br /&gt;	if (argc &gt; 3 || argc==1) {&lt;br /&gt;		cout &lt;&lt; help;&lt;br /&gt;		exit(0);&lt;br /&gt;	}&lt;br /&gt;	else if (argc==2) {&lt;br /&gt;		input_file=argv[1];&lt;br /&gt;		output_file=input_file+".html";&lt;br /&gt;		ccode2html (input_file, output_file);&lt;br /&gt;	} else {&lt;br /&gt;		input_file=argv[1];&lt;br /&gt;		output_file=argv[2];&lt;br /&gt;		ccode2html (input_file, output_file);&lt;br /&gt;	}&lt;br /&gt;	return 0;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;// ccode2html method&lt;br /&gt;&lt;br /&gt;void ccode2html (string input, string output)&lt;br /&gt;{&lt;br /&gt;	try {&lt;br /&gt;		boost::regex e1, e2;&lt;br /&gt;		e2.assign(pre_expression);&lt;br /&gt;		e1.assign(expressions);&lt;br /&gt;		fstream in (input.c_str(), fstream::in);&lt;br /&gt;		fstream out (output.c_str(), fstream::out);&lt;br /&gt;		out &lt;&lt; "&lt;pre&gt;";&lt;br /&gt;		string ins;&lt;br /&gt;		char c;&lt;br /&gt;		while (in.get (c)) {&lt;br /&gt;			ins.append (1, c);&lt;br /&gt;		}&lt;br /&gt;		ostringstream t(std::ios::out | std::ios::binary);&lt;br /&gt;		ostream_iterator&lt;char, char&gt; io(t);&lt;br /&gt;		boost::regex_replace(io, ins.begin(), ins.end(), e2, pre_format, &lt;br /&gt;				boost::match_default | boost::format_all);&lt;br /&gt;		string s(t.str());&lt;br /&gt;		std::ostream_iterator&lt;char, char&gt; o(out);&lt;br /&gt;		boost::regex_replace(o, s.begin(), s.end(), e1, format,&lt;br /&gt;				boost::match_default | boost::format_all);&lt;br /&gt;		out &lt;&lt; "&lt;/pre&gt;";&lt;br /&gt;		cout &lt;&lt; s;&lt;br /&gt;		cout &lt;&lt; input &lt;&lt; endl;&lt;br /&gt;		cout &lt;&lt; output &lt;&lt; endl;&lt;br /&gt;		test();&lt;br /&gt;	} catch (char * exc) {&lt;br /&gt;		cout &lt;&lt; "errors:" &lt;&lt; endl;&lt;br /&gt;		cout &lt;&lt; exc &lt;&lt; endl;&lt;br /&gt;	}&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 07 May 2008 15:59:25 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5473</guid>
      <author>Tvrtko (Tvrtko)</author>
    </item>
    <item>
      <title>Put mounted drives list into Glist</title>
      <link>http://snippets.dzone.com/posts/show/5350</link>
      <description>Function that gets list of mounted drives from '/etc/fstab' and '/etc/mtab' files and puts data in the GList object (from GLib library) that can be further used in GTK.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#include &lt;glib.h&gt;&lt;br /&gt;#include &lt;mntent.h&gt;&lt;br /&gt;#include &lt;string.h&gt;&lt;br /&gt;&lt;br /&gt;GList * g_get_drives_list(GList * g) {&lt;br /&gt;	FILE *fstab = NULL;&lt;br /&gt;	struct mntent *part = NULL;&lt;br /&gt;	gchar *mntp = NULL;&lt;br /&gt;	&lt;br /&gt;	if ((fstab = setmntent( "/etc/fstab", "r" )) != NULL) {&lt;br /&gt;		while ((part = getmntent(fstab))  != NULL) {&lt;br /&gt;			if((strcmp(part-&gt;mnt_type, "proc")) != 0 &amp;&amp; (strcmp(part-&gt;mnt_type, "devpts")) != 0 &lt;br /&gt;																					&amp;&amp; (strcmp(part-&gt;mnt_type, "swap")) != 0) {&lt;br /&gt;				mntp = g_strdup(part-&gt;mnt_dir);&lt;br /&gt;				g=g_list_append(g, mntp);&lt;br /&gt;			}&lt;br /&gt;		}&lt;br /&gt;		endmntent(fstab);&lt;br /&gt;	}&lt;br /&gt;	&lt;br /&gt;	if ((fstab = setmntent( "/etc/mtab", "r")) != NULL) {&lt;br /&gt;		while ((part = getmntent(fstab)) != NULL) {&lt;br /&gt;			if (part-&gt;mnt_type != NULL) {&lt;br /&gt;				if((strcmp(part-&gt;mnt_type, "proc")) != 0 &amp;&amp; (strcmp(part-&gt;mnt_type, "devpts")) != 0&lt;br /&gt;						&amp;&amp; (strcmp(part-&gt;mnt_type, "swap")) != 0 &amp;&amp; (strcmp(part-&gt;mnt_type, "sysfs")) != 0&lt;br /&gt;						&amp;&amp; (strcmp(part-&gt;mnt_type, "tmpfs")) != 0 &amp;&amp; (strcmp(part-&gt;mnt_type, "fuseblk")) != 0&lt;br /&gt;						&amp;&amp; (strcmp(part-&gt;mnt_type, "securityfs")) != 0) {&lt;br /&gt;					if((g_list_find_custom(g, part-&gt;mnt_dir, (GCompareFunc)strcmp)) == 0) {&lt;br /&gt;						mntp=g_strdup(part-&gt;mnt_dir);&lt;br /&gt;						g=g_list_append(g, mntp);&lt;br /&gt;					}&lt;br /&gt;				}&lt;br /&gt;			}&lt;br /&gt;		}&lt;br /&gt;		endmntent(fstab);&lt;br /&gt;	}&lt;br /&gt;	return g;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sat, 12 Apr 2008 13:22:02 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5350</guid>
      <author>Tvrtko (Tvrtko)</author>
    </item>
    <item>
      <title>Code 2 HTML</title>
      <link>http://snippets.dzone.com/posts/show/5330</link>
      <description>This is a simple python program that reads a file (code), and replaces line breaks and spaces with appropriate tag and entity.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;import sys&lt;br /&gt;&lt;br /&gt;line_break='&lt;br&gt;'&lt;br /&gt;nb_space='&amp;#160;'&lt;br /&gt;&lt;br /&gt;def toHtml(f,out):&lt;br /&gt;    try:&lt;br /&gt;        fo=open(out,'w')&lt;br /&gt;    except IOError:&lt;br /&gt;        print 'output file error!'&lt;br /&gt;    else:&lt;br /&gt;        for line in f:&lt;br /&gt;            line=line.replace('\t',nb_space*8)&lt;br /&gt;            line=line.replace(' ',nb_space)&lt;br /&gt;            line=line.replace('\n',line_break)&lt;br /&gt;            fo.write(line)&lt;br /&gt;        fo.close()&lt;br /&gt;        &lt;br /&gt;&lt;br /&gt;if len(sys.argv) == 1:&lt;br /&gt;    print("at least one argument / input filename / required")&lt;br /&gt;    sys.exit()&lt;br /&gt;if len(sys.argv) &gt; 3:&lt;br /&gt;    print"too many arguments"&lt;br /&gt;    sys.exit()       &lt;br /&gt;try:&lt;br /&gt;    f=open(sys.argv[1],'r')&lt;br /&gt;except IOError:&lt;br /&gt;    print 'cannot open file ', sys.argv[1]&lt;br /&gt;else:&lt;br /&gt;    if len(sys.argv) == 2:&lt;br /&gt;        out=f.name + '.html'&lt;br /&gt;    else:&lt;br /&gt;        out=sys.argv[2]&lt;br /&gt;    toHtml(f, out)&lt;br /&gt;    f.close()&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 07 Apr 2008 07:59:48 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5330</guid>
      <author>Tvrtko (Tvrtko)</author>
    </item>
    <item>
      <title>decompressing various archive types</title>
      <link>http://snippets.dzone.com/posts/show/5328</link>
      <description>// decompressing various archive types with this python script&lt;br /&gt;// usage unpack &lt;archive filename&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#!/usr/bin/env python&lt;br /&gt;#&lt;br /&gt;# simple python script for extracting mostly used types of archives&lt;br /&gt;# this script extracts .tar, .tar.gz, .tar.bz2, .gz and .zip archives &lt;br /&gt;#&lt;br /&gt;&lt;br /&gt;import sys	# required for fetching command line arguments &lt;br /&gt;import os	# required for calling commands for archive extracting&lt;br /&gt;&lt;br /&gt;def unpack(s):									# this is definition of depack&lt;br /&gt;	if (s.find('.tar.gz') != -1):				#	function. It takes string&lt;br /&gt;		os.system("tar -xvvzf " + filename)		#   filename as argument.&lt;br /&gt;	elif (s.find('.tar.bz2') != -1):			#	functon than calls &lt;br /&gt;		os.system("tar -xvvjf " + filename)		#   appropriate command according&lt;br /&gt;	elif (s.find('.tar') != -1):				# 	to file extension&lt;br /&gt;		os.system("tar -xvvf " + filename)&lt;br /&gt;	elif (s.find('.gz') != -1):&lt;br /&gt;		os.system("gunzip" + filename)			&lt;br /&gt;	elif (s.find('.zip') != -1):		&lt;br /&gt;		os.system("unzip " + filename)&lt;br /&gt;	else: print "Wrong archive or filename"		# other types not supported&lt;br /&gt;&lt;br /&gt;try:											# this is main program&lt;br /&gt;	filename = sys.argv[1]						# first argument right after&lt;br /&gt;	unpack(filename)							#	'unpack' command goes in the&lt;br /&gt;except IndexError:								#	filename string&lt;br /&gt;	print "Filename is invalid!"				#	than the depack function is called&lt;br /&gt;&lt;br /&gt;# try-except block is used for handling IndexError exception if no argument is passed&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 06 Apr 2008 22:31:57 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5328</guid>
      <author>Tvrtko (Tvrtko)</author>
    </item>
  </channel>
</rss>
