Code Repo    |     RSS
MD's Technical Sharing



Monday, May 31, 2010

Full RSS feed for blogger posts/comments

The official RSS feeds given by Google (http://www.google.com/support/blogger/bin/answer.py?hl=en&answer=97933) only retrieve the latest 50 posts and 20 comments, even if the settings under Site Feed has been set to Full. This default behaviour is useful as readers are usually only interested in latest posts, but will be a problem if you somehow want the RSS feed to show your entire blog.

The solution is simple, just add a HTTP GET parameter to the official RSS URL. For example, if your RSS feed is:

http://blogname.blogspot.com/feeds/posts/default?alt=rss

The feed that returns the full blog is:

http://blogname.blogspot.com/feeds/posts/default?alt=rss&max-results=N

Same for comments, add the max-results parameter to retrieve all comments:

http://blogname.blogspot.com/feeds/comments/default?alt=rss&max-results=N

where N is the maximum number of posts/comments you want to retrieve.
Read More »

Monday, May 3, 2010

Identifying used and unused resources in a Visual Studio's project Resources.resx file

If you often use project resources in a Visual Studio project, be it VB or C#, eventually you will end up with a big resource file (e.g. Resources) containing many unused items. This is because, when you remove the code that uses the resoure, very often you will forget to remove the actual resource item.

There are solutions to this problems such as using commercial code refactoring tool or making some minor modifications to Resources.Designer.vb/Resources.Designer.cs and relying on the compiler to generate warnings about unused resources. In this post, I choose to take a different approach: use a batch script.

My complete batch script will accept 4 parameters from the command line, listed from left to right:

1. Path to the VB.NET source code files, e.g. WindowsApplication1\*.vb
2. Name of project resource file, e.g. WindowsApplication1\My Project\Resources.resx
3. Name of file where all resources' usage are written, e.g. all_resources.txt
4. Name of file where all unused resources are written, e.g. unused_resources.txt

It makes use of the FOR extended syntax (FOR /F) to parse the resource file and FINDSTR to find where all the resources are referred to. It will only work properly with VB source code files, where most developers often use My.Resources.XXXX to access the resources. As FINDSTR simply performs a string search, the batch script will not care about code that are commented out, as well as resources that are accessed not by using the Vb's My namespace. If you want to use this with C# source code, you'll need to edit the call to FINDSTR to match your method of accessing the resources, e.g. Project1.Resources.Resource1 instead of My.Resources.Resource1.

The rest of this post will analyse some interesting points I encountered when writing it - hope this will be useful for those having similiar problems.

Batch script - powerful despite being crude

Many people may think that batch script is a thing of the past, since there are so many alternatives today - Windows PowerShell, Perl, or a .VBS file. Yet sometimes I still use it due to its powerful but simple nature. Take a look at the code to search for the resource:

FOR /F "tokens=*" %%a IN (%TMPFILE%) DO (
.........
        FINDSTR /S /P /N /C:"My.Resources.%%c" %SRCPATH% >> %OUT_ALLTOKENS%                  
        REM FindStr returns an errorlevel of 0 if the string was found, 1 and above if it was not found.
        IF ERRORLEVEL 1 (
.........
        )
.............      
    )
)


The call to FOR /F
and FINDSTR can easily translate into 10-20 lines of .NET code, unless you use some existing text processing libraries. Here, almost everything is done, just simply remember the syntax, available via FOR /? or FINDSTR /?.

Another example is how to generate a random file name:

:GETTEMPNAME
set TMPFILE=%TMP%\mytempfile-%RANDOM%-%TIME:~6,5%.tmp
if exist "%TMPFILE%" GOTO :GETTEMPNAME


The equivalent .NET code would be:

string filename = Path.GetTempFileName()

However, batch scripts are still very crude. For example, although Windows XP batch scripts allow the use of block statement via (........), don't expect everything to work as they do in a modern programming language. The following would cause problem:

IF (%1) == () (
    ECHO Missing first parameter: path to the VB.NET source code files (e.g. WindowsApplication1\*.vb)
    EXIT /B 1
) ELSE (
    SET SRCPATH=%1
)

Execution would terminate shortly upon reaching ECHO, without any indication why. I believe it would take many people some time to figure out what causes it and fix it. The following will work:

IF (%1) == () (
    ECHO Missing first parameter: path to the VB.NET source code files, e.g. WindowsApplication1\*.vb     
    EXIT /B 1
) ELSE (
    SET SRCPATH=%1
)

Also the above example demonstrates how to check for missing parameter. This would have made more sense in C# or VB.NET, just compare against an empty string (e.g. "").

There are many other useful code snippets in this batch script, for example, how to use delayed environment variable expansion, or how to use double quotes as delimiters in FOR /F. I will not explain it in details - the code is well commented, take some time to study it yourself.
Read More »

Saturday, May 1, 2010

1440x900 resolution on Intel 945GM Express Chipset

My laptop is using a Intel 945GM Express Chipset graphics card. Recently I purchased a 19" LCD with 1440x900 naive resolution. Believe it or not, it was very difficult to set the correct resolution for the LCD as my graphics card is quite old and does not officially support 16:10 resolution. The closest it supports is 1440x1050, causing distortion on the LCD.

I solved the problem by using PowerStrip. With some simple configuration as per this, I am able to add a new custom resolution and get it to work:


However, with this I encountered the next challenge. Media Player Classic, which is my favourite media player, is unable to play video on my LCD monitor when configured as the secondary monitor. I always got the error message

"Due to limitations of your computer's display device, this video can only play back on one of your monitors. Please move the video window completely onto that monitor" 

whenever I attempt to play videos on the LCD.

After some research, I found out that the problem has to do with hardware overlay (see this). Media Play Classic seems to attempt hardware overlay on both the primary and secondary monitors, which, unfortunately, is not within the capabilities of the graphics card.

I decided to change to VLC media player. However, VLC, by default, uses software rendering and does not play HD movies smoothly. When configure to playback using hardware overlay, VLC encounters the same problem as Media Player Classic. Even worse, there is no clear error message - the application simply crashes or plays videos with huge delay/distortion. Windows Media Player works for some simple movies (.avi, .wmv, ...) but fails to play many other formats.

Finally, some googling on the best free media player leads me to BS Player. I switch to it and everything seems good so far. Sometimes, the player would crash upon closing but that does not seem to be a big problem for me.
Read More »