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

http://www.hrum.org http://debedb.blogspot.com/

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

Add a concat() aggregate function to SQLite

I often need string concatenation to behave just like an aggregate function.
Once again I find a need to do that in SQLite, and to do that without recompiling
SQLite for every platform we distribute for...

#include <stdlib.h>
#include "sqlite3ext.h"
SQLITE_EXTENSION_INIT1


typedef struct SCtx SCtx;
struct SCtx {
  int rowCnt;
  int charCnt;
  char *result;
};

static void concat_step(sqlite3_context* ctx, int argc, sqlite3_value**argv) {

  SCtx *p = (SCtx *) sqlite3_aggregate_context(ctx, sizeof(*p));

  char *sep = sqlite3_value_text(argv[1]);

  char *txt = sqlite3_value_text(argv[0]);

  if (p->rowCnt) {
    char *txt2 = malloc(strlen(txt) + strlen(sep) + 1);
    strcpy(txt2,sep);
    strcat(txt2,txt);
    txt = txt2;
  }

  //  printf("%d. Txt: [%s] len %d\n", p->rowCnt, txt, strlen(txt));

  int len = strlen(txt);

  if (!p->result) {
    p->result = malloc(len + 1);
    strcpy(p->result, txt);
  } else {
    p->result = realloc(p->result, strlen(p->result) + len + 1);
    strcat(p->result,txt);
  }
  //  printf ("intermediate [%s]\n", p->result);
  p->rowCnt++;
}

static void concat_final(sqlite3_context* ctx,
                         int argc,
                         sqlite3_value** argv) {

  SCtx *p = (SCtx *) sqlite3_aggregate_context(ctx, sizeof(*p));
  //  printf("Finally: %s\n", p->result);
  sqlite3_result_text(ctx,  p->result, strlen(p->result), NULL);
}

int sqlite3_extension_init(
  sqlite3 *db,
  char **pzErrMsg,
  const sqlite3_api_routines *pApi
){
  SQLITE_EXTENSION_INIT2(pApi)
  sqlite3_create_function(db, "concat", 2, SQLITE_ANY, 0, NULL, concat_step, concat_final);
  return 0;
}


I compiled this with the following (here, ./src is the SQLite code - I used
http://www.sqlite.org/sqlite-source-3_3_13.zip).

gcc -fpic -c agg.c -I./src
gcc -shared -Wl,-soname,libagg.so -o libagg.so agg.o


And here's how it works:

sqlite> CREATE TABLE test (animals VARCHAR, interjection VARCHAR);

sqlite> insert into test (animals, interjection) values ('lions', 'oh my');

sqlite> insert into test (animals, interjection) values ('tigers', 'oh my'); 

sqlite> insert into test (animals, interjection) values ('bears', 'oh my');

sqlite> select load_extension('./libagg.so');

sqlite> select concat(animals, ' and '), interjection from test group by interjection;

sqlite> lions and tigers and bears|oh my




See also:
1. http://www.sqlite.org/cvstrac/wiki?p=LoadableExtensions
2. http://souptonuts.sourceforge.net/readme_sqlite_tutorial.html
3. http://www.sqlite.org/capi3ref.html

Flex/ActionScript: Making trace() display the method it was called in

While trace() is useful, its indiscriminate use by me often crowds
the console of my fellow developers. We agreed on making every trace() show
the method it came from, but I am too lazy to do this, this makes it automatic:

public static function TRACE(s:Object):void {
  try {
    throw new Error();
  } catch (e:Error) {
    var stack:String = e.getStackTrace();
    var frames:Array = stack.split("\n");
    var myFrame:String = String(frames[2]);
    myFrame = myFrame.replace("\t", "");
  
    // "at " can be followed by some part of the package
    // you don't want to see. E.g., if your code is all in
    // com.foo.bar, you can put "at com.foo.bar." so as not
    // to crowd the display
    myFrame = myFrame.substr("at ".length);
    myFrame = myFrame.substring(0, myFrame.indexOf("["));
    trace(myFrame + ": " + s);
  }
}

Converting file list into a class list

Converting file list into a class list - this is particularly useful for include-classes attribute of compc task in Flex, but may be useful for Java. The <chainmapper> is most probably pleonastic but it works for me this way, and I don't feel like trying it without it...

   <path id="xmlrpc_class_list_1">
      <fileset dir="${user.dir}/xmlrpc">
         <include name="com/mattism/**"/>
         <exclude name="**/Test*"/>
      </fileset>
   </path>
   <pathconvert 
      property="xmlrpc_class_list_2" 
      pathsep=" " 
      dirsep="." 
      refid="xmlrpc_class_list_1">
      <map from="${user.dir}/xmlrpc/" to=""/>
      <mapper>
         <chainedmapper>
            <globmapper from="*.as" to="*"/>
         </chainedmapper>
      </mapper>
</pathconvert>

Getting build number for Ant build in Continuum

Getting build number for Ant build in Continuum

   
<timestampselector property="mostrecentlog">
<path>
<fileset dir="${user.dir}/../../cont-output/1">
<include name="*.log.txt" />
</fileset>
</path>
</timestampselector>			

<basename property="reportsDirName" file="${mostrecentlog}" suffix=".log.txt">
</basename>

Print call sequence of a C program

Perl script to run gdb and collect information about method call
sequences. See also the blog entry.

#!/usr/bin/perl 

use FileHandle;
use IPC::Open2;

if (! (-e "tags")) {
  print "Cannot find tags, will run\n";
  $tagCmd = "ctags *.h *.c";
  print "\t$tagCmd\n";
  system($tagCmd) && die ("Make sure ctags is installed\n");
}

%breakId2Func = {};
%func2Args = {};

#if ($ARGV[0] eq "prepare") {
#  exec('ctags *.h *.c');
  $exec = $ARGV[0];
  splice(@ARGV, 0, 1);
  
  $pid = open2(*Reader, *Writer, "gdb -annotate 3 --interpreter=mi $exec");

  while (<Reader>) {
    chop;
    last if ($_ eq "(gdb) ") 
  }    
  print Writer  "set print pretty on\n";
  while (<Reader>) {
    chop;
    last if ($_ eq "(gdb) ") 
  }    

  print Writer  "set print array on\n";
  while (<Reader>) {
    chop;
    last if ($_ eq "(gdb) ") 
  }    
  print Writer  "set print union on\n"; 
  while (<Reader>) {
    chop;
    last if ($_ eq "(gdb) ") 
  }    
  
  print "...\n";
  open(GREP, 'grep "f$" ./tags|') || die "Do you have ctags?\n";
  while (<GREP>) {
    ($func, $file, $regexp, $f) = split(/\t+/);
    $from = index($regexp, "(") + 1;
    $to = rindex($regexp, ")");
    $args = substr($regexp, $from, $to - $from);

    print Writer  "break $func\n";
    while (<Reader>) {
      # Store the number of the breakpoint, we will
       # need it later to determine when it's hit
      chop;
       if ($_ =~ /Breakpoint/) {
	 $breakId = substr($_, length('~"Breakpoint '));
         $breakId = substr($breakId, 0, index($breakId, " "));
         $breakId2Func{$breakId} = $func;
       } 
       last if $_ eq "(gdb) ";
     }

    ##############################################################
    # I wrote this when I didn't realize I can call "info args"
    # But maybe saving this is still useful for more information
    # like figuring out the type of variable and printing more
    # info about it, in case it's some pointer to struct to whatever
    #
    # Save argument names for evaluation when breakpoint is hit    
    # @args = split(/,/, $args );
    #
    # @$func = ();
    # foreach $arg (@args) {
    #  @typeAndVar = split(/\s+/,$arg);
    #
    #  $$func[++$#$func] = $typeAndVar[$#typeAndVar];
    # }
    #    $func2Args{$func} = \@$func;
    #    *x = $func2Args{$func};
    #    print "$func > @x\n";
    # }


print "Calling run @ARGV\n";
print Writer "run @ARGV\n";

$inBreak = 0;
while (<Reader>) {
  chop;
  if ($_ =~ /Breakpoint/) {
    #      print "$_\n";
    $breakId = substr($_, length('~"Breakpoint '));
    $breakId = substr($breakId, 0, index($breakId, ","));
    
    $func = $breakId2Func{$breakId};
    $inBreak = $breakId;
    
    # Which one is better?
    print Writer "info args\n";
    # print Writer "-stack-list-arguments 1 0 0\n";

    print Writer "cont\n";
  }
  
  if ($inBreak) {
    if ($_ =~ /~\"Continuing./) {
      $inBreak = 0;
      print "***************************\n";
    } else {
      if ($_ ne "(gdb) " &&
	  $_ ne "^done" &&
	  $_ ne '~"\n"' &&
          $_ ne '&"cont\n"' &&
	 $_ !~ /stopped/) {

	$_ =~ s/\\[nt]//g;
	$_ =~ s/\\032//g;
	$_ =~ s/^~\"//g;
	print "$_\n";
	
      }
    }
  }
}

Beginnings of a fixed-width file import wizard in wxPython

A draft, saved for future reference, of an analog of Python-DSV, but for fixed-length files...

from wxPython.grid import wxGrid
from wxPython.wx import wxPySimpleApp
from wxPython.wx import wxFrame
import wx
import string
import types

class FixLenDiscovery(wxFrame):
    colNum = 2
    
    rowNum = 6
    
    defLen = "5"

    def addField(self,event):
        self.fieldGrid.InsertCols(pos = self.colNum)
        self.grid.InsertCols(pos = self.colNum)
        self.fieldGrid.SetCellValue(0, self.colNum, self.defLen)
        self.colNum = self.colNum + 1
        self.Layout()
        
    def removeField(self,event):
        if self.colNum > 1:
            self.fieldGrid.DeleteCols(self.colNum  - 1)
            self.grid.DeleteCols(self.colNum  - 1)
            self.colNum = self.colNum - 1
        self.Layout()

        
    def loadFile(self,event):
         path = self.filePicker.GetPath()
         file = open(path, 'r')
         
         self.grid.ClearGrid()
         for row in range(0, self.rowNum -1):
             for col in range(0, self.colNum):
                 width = self.fieldGrid.GetCellValue(0, col)
                 width = string.atoi(width)
                 value = file.read(width)
                 self.grid.SetCellValue(row, col, value)
             
         self.grid.AutoSize()
   
    def __init__(self):
        wxFrame.__init__(self, None, -1, '', size=(1000, 500))
        
        self.filePicker = wx.FilePickerCtrl(self, 1)
        
        self.loadButton = wx.Button(self, wx.FIXED, "Preview")
        self.loadButton.Bind(wx.EVT_BUTTON, self.loadFile)
        
        self.grid=wxGrid(self, -1)
        self.grid.CreateGrid(self.rowNum, self.colNum)
        self.grid.DisableCellEditControl()
       
        topBox = wx.BoxSizer(wx.HORIZONTAL)
#        topBox.Add(wx.StaticText(self, wx.FIXED, "File: "), 1)
        topBox.Add(self.filePicker, 3)
        topBox.Add(wx.StaticText(self, wx.FIXED, ""), 1)
        topBox.Add(self.loadButton, 1)
        
        fieldsBox = wx.BoxSizer(wx.VERTICAL)
        
        fieldsButtonBox = wx.BoxSizer(wx.HORIZONTAL)
        
        self.fieldGrid = wxGrid(self, -1)
        self.fieldGrid.CreateGrid(1, self.colNum)
        for i in range(0, self.colNum):
            self.fieldGrid.SetCellValue(0, i, self.defLen)
        
        
        addFieldButton = wx.Button(self, wx.FIXED, "Add field")
        addFieldButton.Bind(wx.EVT_BUTTON, self.addField)
        removeFieldButton = wx.Button(self, wx.FIXED, "Remove field")
        removeFieldButton.Bind(wx.EVT_BUTTON, self.removeField)
        
        fieldsButtonBox.Add(addFieldButton, 1)
        fieldsButtonBox.Add(removeFieldButton, 1)
        
        fieldsBox.Add(self.fieldGrid, 1)
        fieldsBox.Add(fieldsButtonBox, 1)
        
        mySizer = wx.BoxSizer(wx.VERTICAL)
        mySizer.Add(topBox, 1)
        mySizer.Add(fieldsBox, 3)

        mySizer.Add(self.grid, 4)
        
        self.SetAutoLayout(True)
        self.SetSizer(mySizer)
        self.Layout()
       
if __name__ == "__main__":
    app = wxPySimpleApp()
    frame = FixLenDiscovery()
    frame.Show(True)
    app.MainLoop()

One-click connect from cygwin to full-screen Linux (X stuff required)

Optional: execute the following once (see http://hacks.oreilly.com/pub/h/66 for
details), to avoid typing in the password.

set LINUX_HOST=mylinuxhost
ssh-keygen -t rsa
ssh %USERNAME%@%LINUX_HOST% "mkdir .ssh; chmod 0700 .ssh"
bash -c 'scp ~/.ssh/id_rsa.pub %LINUX_HOST%:.ssh/authorized_keys2'


Now, if you save the following as a BATCH file, you can just click it
to connect to fullscreen Linux:

set LINUX_HOST=mylinuxhost
start /min xinit 
xhost +%LINUX_HOST%
ssh %LINUX_HOST% "declare -x DISPLAY=%COMPUTERNAME%:0; echo $DISPLAY;gnome-session"'


NOTES:
1.
Sometimes xhost + does not execute in time. O well. Just close
all windows that got opened and try again.

2.
If you didn't do the step one (ssh keys), you will be prompted
to enter the linux password; this increases the chances of the above
error.

3.
The single terminal window appearing in the Linux desktop is in fact
a window from your PC.

JUnit: Convenient use of TestSuite based on parameterized TestCases

Here's a frequently (at least for me) occurring problem: let's say
you have a TestCase where each testXXX() method really does mostly
the same thing, parameterized by some instance variables. For instance,
I have a file consisting of good data for each parameter, such as:

goodAAA=foo
goodBBB=bar
goodCCC=baz
...


You don't want to create tens or hundreds of methods (testAAA(),
testBBB(), etc.) by copying and pasting. So you create a TestCase that
has:

- An field called, say, key

- A single testKey() method that, based on the
key, gets the good data, gets the test data and compares them.

- static suite() method that, in a loop, creates instances of your
TestCase and sets their keys appropriately.

This is all well and good. But when you run it, a failure will always
show up in testKey() method in JUnit's UI. (Sure, you can add a message
that shows the key for which it failed, but it does not give
you an at-a-glance feedback.)

So for convenience, you override getName() to return key. Now
it's very easy to tell, by merely glancing at that JUnit bar, what
failed.

But if you run it within IDE, say, Eclipse, you want to click on the
failed test so that Failure Trace appears, where you can click on any
Stack Trace Element and get to the exact line of failure in your code.
But because the failed test's name is based on the key field, it does
not correspond to a real testXXX() method in your TestCase. So your
IDE can't take you to the code that failed.

Here's how to override getName() to get the best of both worlds:

public String getName() {
    StackTraceElement[] st = new Throwable().getStackTrace();
    if (st.length < 4 || !st[3].getMethodName().equals("endTest")) {
        return key; 	  // Meaningful name to appear in the bar
    } else {
        return "testKey"; // Real method name to appear in Failure Trace
    }
}

Lowercasing a string in BAT files

Using this silliness, here's an example of BAT file that will
lower-case a string given as an argument.

echo>%1
dir /b/l %1>lower.tmp
set /p result=<lower.tmp
echo %result%


Saving this as lower.bat, I run

lower "HELLO WORLD"


and get

hello world

as a result

Using Unix shell's backquote functionality in DOS/Windows batch files

In BAT files, an equivalent of a Unix shell's
set foo `bar`


can be accomplished as

bar>bar.tmp
set /p foo=<bar.tmp


If you're spoiled by Unix's notion of
true pipes, note that the following:

bar | set /p foo=


does not work.
« Newer Snippets
Older Snippets »
Showing 1-10 of 10 total  RSS