Removing line numbers from a source code

At the moment I’m learning developping eclipse plugins for my work. Therefore I had to copy some code 1 from this tutorial. Unfortunately I had to remove the stupid line numbers. It’s best to let remove them by a script. Because I spent a lot of time in WordPress plugin development few weeks ago I chose PHP to do the work.

<?php
$code = '
1.   package com.myplugin.rmp.views;
2.   import java.util.ArrayList;
3.   import org.eclipse.core.resources.IFile;
4.   import org.eclipse.core.resources.IFolder;
5.   import org.eclipse.core.resources.IProject;
6.   import org.eclipse.core.resources.IResource;
7.   import org.eclipse.core.resources.IWorkspace;
8.   import org.eclipse.core.resources.ResourcesPlugin;
9.   import org.eclipse.core.runtime.IAdaptable;
10.  import org.eclipse.jface.action.Action;
11.  import org.eclipse.jface.action.MenuManager;
12.  import org.eclipse.jface.dialogs.MessageDialog;

...

199.         public void setFocus() {
200.                  viewer.getControl().setFocus();
201.         }
202. }
';

echo "<pre>". preg_replace("/\d+\./", "", $code) ."</pre>";
?>

The script is self descriptive but be sure that you adopt the regular expression to your needs. The regex I used destroys code containing constructs like anyObject12.doAnything();

  1. Copy & paste is bad but 200 lines are too many to rewrite ;-)
DeliciousDiggTechnorati FavoritesRedditLinkedInFacebookSpurlTwitterWebnewsYiGGMySpaceYahoo BookmarksFriendFeedGoogle BookmarksLiveJournalShare

Changing the menu text with jQuery

If you need to toggle the menu text client-side when you click it some JavaScript is required. This is best done with a framework like jQuery. All you need is an event handler bound to the menu item. It uses a counter variable to store the state which will be checked for oddness. If it’s odd use the one otherwise the other text.

Show only defect entries

<script type="text/javascript">
  jQuery(document).ready( function(){
    var flip = 0;
    jQuery('#owad_show_defects').bind( "click", function(e){
      flip++;

      if ( flip % 2 == 0 )
        jQuery('#owad_show_defects').text('Show all entries');
      else
        jQuery('#owad_show_defects').text('Show only defect entries');

    });
  });
</script>
DeliciousDiggTechnorati FavoritesRedditLinkedInFacebookSpurlTwitterWebnewsYiGGMySpaceYahoo BookmarksFriendFeedGoogle BookmarksLiveJournalShare

Using sub-patterns in regular expressions

Using sub-patterns allows you to just filter a part of a specific content. These are patterns in patterns. In the following example I show you how to get the text This text is what I want to filter.

<html>
<body>
This is a <u>sample text</u> to show you how to filter
some text surrounded by quotes. "Here's a dummy text to
make our example more complicated". Now here's the text we
are interested in: "This text is what I want to filter."
</body>
</html>

There are several php commands that handles regular expressions but only preg_match_all is the most interesting for us now.

Let us consider the the sub-pattern first. We’re looking for any characters surrounded by quotes that are different to the quote itself.

"([^"]+)"
() represents a group
[^"] any character that is not the quote
+ tells the regex engine to look for repeated characters one or more times

We’re not interested in the quotes itself so we put them to the left and right of the opening and closing braces. Thus the quotes don’t appear in the final result.

Now we have to specify which match of the sub-pattern we’d like to filter. This is quite easy. Just take some text in front of it, for example we are interested in:

we are interested in: "([^"]+)"

In this example there’s only one space between the colon and the first quote. If the number of spaces is unknown we have to use the meta-character \s followed by an asterisk that represents a space appearing zero or more times. So the final expression looks like

we are interested in:\s*"([^"]+)"

And now some php code to get the final result ;-)

$pattern = '/we are interested in:\s*"([^"]+)"/';
preg_match_all( $pattern, $input, $matches );
$final_result = $matches[1][0];

The pattern has to be surrounded by delimiters which have to be non-alphanumeric. With preg_match_all we’re looking for any matches stored in the array $matches. $matches[0] contains the matches by the full expression and $matches[1] those of the sub-pattern. They can be accessed by a further index variable. In this example $matches[1][0] contains This text is what I want to filter.

DeliciousDiggTechnorati FavoritesRedditLinkedInFacebookSpurlTwitterWebnewsYiGGMySpaceYahoo BookmarksFriendFeedGoogle BookmarksLiveJournalShare

Matlab for C++ programmers

Today I went to Frankfurt to a seminar provided by MathWorks. It was about “Matlab for C++ programmers”. I learnt more about features that I didn’t know before like the Matlab engine. There’s more stuff that I’m going to tell you about later on but first some words about my trip.

When I came out of the train I rememberd my trip to a congress in Napoli two years ago. The train station in Frankfurt bear a resemblance to that one of Milano. Outside the first car I saw was an Italian. “Am I in Italy or in Frankfurt?” I asked myself. While I walked to the venue I took these pictures.

DSC00595.JPG DSC00594.JPG

Back to topic. At the seminar we were shown how easy data can be plotted with Matlab probably generated by a C/C++ application. With some sample codes the Matlab language was introduced. The following code plots a unit circle.

t = linspace(0, 2*pi, 200);
x = sin(t);
y = cos(t);
plot(x,y)

There is more stuff in the Matlab tool chain that was new to me:

  • The Matlab Engine is used to call Matlab software from C/C++ and Fortran.
  • Embedded Matlab is a subset of the Matlab language. Efficient code can be generated for embedded implementation with Embedded Matlab.
  • PolySpace products verify C, C++, and Ada code for embedded applications by detecting run-time errors before code is compiled and executed. This advanced verification technology uses formal methods not only to detect errors, but to prove mathematically that certain classes of run-time errors do not exist.
  • With the Matlab Compiler you are able to generate a standalone application from your Matlab code. This application can be ported to other computers without Matlab installed on it. You just need the Matlab Compiler Runtime (MCR) to execute the Matlab instructions. If MCR comes with the application the end user doesn’t need anything else of the Matlab tool chain. There’s no obstacle anymore to share your Matlab algorithms.
DeliciousDiggTechnorati FavoritesRedditLinkedInFacebookSpurlTwitterWebnewsYiGGMySpaceYahoo BookmarksFriendFeedGoogle BookmarksLiveJournalShare

‘staticMetaObject’ is not a member

I tried to compile a code looking like this:

#ifndef CONTROLLER_H_
#define CONTROLLER_H_

#include<QObject>

class AbstractClass
{
public:
    virtual ~AbstractClass() {}
    virtual void update() = 0;
};

class Controller :
      public AbstractClass, public QObject
{
      Q_OBJECT

public:
      Controller();
      virtual ~Controller();
      virtual void update();

public slots:
      void m();
};

#endif /*CONTROLLER_H_*/

If you’re trying to compile it you get an error:

tmp/moc/moc_Controller.cpp:45: error: 'staticMetaObject' is not a member of 'AbstractClass'
tmp/moc/moc_Controller.cpp: In member function 'virtual void* Controller::qt_metacast(const char*)':
tmp/moc/moc_Controller.cpp:61: error: 'qt_metacast' is not a member of 'AbstractClass'
tmp/moc/moc_Controller.cpp: In member function 'virtual int Controller::qt_metacall(QMetaObject::Call, int, void**)':
tmp/moc/moc_Controller.cpp:66: error: 'qt_metacall' is not a member of 'AbstractClass'
make: *** [tmp/objects/moc_Controller.o] Error 1

The error is caused by a bad order of inheritance.

Instead of this

class Controller :
	public AbstractClass, public QObject
{
   ...

you should use this

class Controller :
	public QObject, public AbstractClass
{
   ...

 

DeliciousDiggTechnorati FavoritesRedditLinkedInFacebookSpurlTwitterWebnewsYiGGMySpaceYahoo BookmarksFriendFeedGoogle BookmarksLiveJournalShare