Tuesday, 25 November 2014

Git - find the global gitconfig file

From here

git config --global --edit should tell you the exact location no matter what kind of setup you have--just look at what file comes up in your editor.

"Windows" and "C:\Program Files\Git" means MSysGit, now "Git for Windows".
If you look at etc/profile (in C:\Program Files\Git):
HOME="$HOMEDRIVE$HOMEPATH"
Meaning:

C:\Users\MyLogin
(on Windows7)

For writing options: write to global ~/.gitconfig file rather than the repository .git/config.
That means the file is in C:\Users\MyLogin\.gitconfig for msysgit in Windows7.

Integrating KDiff3 with git on windows

Install KDiff3, edit git config to have the following lines

[merge]
     tool = kdiff3

[mergetool "kdiff3"]
     path = C:/Program Files/KDiff3/kdiff3.exe
     keepBackup = false
     trustExitCode = false
 
From here

Tuesday, 18 November 2014

Simulating receiving an SMS from an Android Emulator

There are a couple of different ways to do it.

  1. Using DDMS: From Android Studio you can open up DDMS by clicking on the tiny android icon which says "Android Device Monitor" when you hover over it. A new window will open up which gives you access to DDMS (which is awesome - you can send SMS, make phone call, set GPS location, and a ton of other things). You can also launch DDMS from the command prompt (android-sdk-dir\tools\ddms.bat)
  2. Using telnet: (which is what DDMS uses internally). First find the console port number (number like 5554 displayed in the title bar of the android virtual device window), then telnet localhost <port-number>. After that there is a ton of stuff you can do including sending sms like so: send sms <senderNumber> <messageText>  See here for other capabilities.

Wednesday, 5 November 2014

Reading data from Excel in C#

You can use ADO.Net to read data from Excel - almost using it like a database with each sheet as a table. But there are some pre-requisites and quirks.

If you have 32 bit office installed, you'll need to install 32 bit providers to connect to it and if you have 64 bit office, you'll need the 64 bit drivers. You can get the drivers from here:

The connection string that you use to connect to the excel file depends on what kind of excel file it is:

Also from the same link above :
File Type (extension)                                                             Extended Properties
--------------------------------------------------------------------------------------------------
Excel 97-2003 Workbook (.xls)                                             "Excel 8.0"
Excel 2007-2010 Workbook (.xlsx)                                       "Excel 12.0 Xml"
Excel 2007-2010 Macro-enabled workbook (.xlsm)             "Excel 12.0 Macro"
Excel 2007-2010 Non-XML binary workbook (.xlsb)            "Excel 12.0"

This goes at the end of the connection string. The whole connection string would look something like this: "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\full\path\to\filename.xlsx; Extended Properties=Excel 12.0 Xml;"

The connection string that you use to connect to the excel file depends on what kind of excel file it is:

The code to read from a sheet looks like this (from here):

var fileName = string.Format("{0}\\fileNameHere", Directory.GetCurrentDirectory());
var connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\\full\\path\\to\\filename.xlsx; Extended Properties=Excel 12.0 Xml;";

var adapter = new OleDbDataAdapter("SELECT * FROM [workSheetNameHere$]", connectionString);
var ds = new DataSet();

adapter.Fill(ds, "anyNameHere");

DataTable data = ds.Tables["anyNameHere"];

var data = ds.Tables["anyNameHere"].AsEnumerable();
var query = data.Where(x => x.Field<string>("phoneNumber") != string.Empty).Select(x =>
    new MyContact
    {
    firstName= x.Field<string>("First Name"),
    lastName = x.Field<string>("Last Name"),
    phoneNumber =x.Field<string>("Phone Number"),
    });

Reading data like this has some horrible quirks and is not reliable. I wouldn't recommend this approach. From here the worst part is: It tries to guess at the type of a column based on the values in the first few rows. This is by default the first 8 rows (but can be anywhere between 1 and 16 or you can make it use all of 16384 rows) - all of this is based on registry entries and not the connection string! There suggestion from that link (which made me laugh out loud when I read it) "use a decent API like the Jakarta POI"

Tuesday, 28 October 2014

Creating a right-click menu to open with Sublime Text

http://superuser.com/questions/412312/can-i-right-click-a-folder-in-windows-7-and-choose-open-with-sublime-text

The one I used involved downloading OpenWithSublimeTextAsAdmin.bat from here saving it to the sublime text folder and running it. 

Other option seems to be registry editing manually. 

Sunday, 12 October 2014

Git: find only the names of files which are different across two commits/branches

git diff branch1..branch2 --name-only

The --name-only also works with git show

Git: diff between two branches for a file

git diff branch1..branch2 -- file_path_from_base_of_repo

In the diff output "a" will be the version of the file in branch1 and "b" will the file in branch2

Wednesday, 8 October 2014

Create a git branch and push it upstream

If you want to create a local branch and then push it upstream so that changes are tracked between the remote (upstream) branch and the local branch, then do this: (git 1.7.0+)

$ git checkout -b feature_branch_name
... edit files, add and commit ...
$ git push -u origin feature_branch_name
Reference

Monday, 6 October 2014

Location of outlook PST file

When moving from one computer to another, to carry old emails along, you'll need to locate the PST file in the old computer, move it across to the new computer and then tell outlook on the new computer about the PST file.
To do all this, go here: File -> Info -> Account Settings -> Data Files

See the following screenshots:


Sunday, 28 September 2014

Reading a terminfo file

The terminfo/termcap configure various terminals in linux

The infocmp can be used to dump the compiled current terminal configurations into the terminfo format.

Sample output of infocmp

#       Reconstructed via infocmp from file: /usr/share/terminfo/c/cygwin
cygwin|ansi emulation for Cygwin,
        am, hs, mir, msgr, xon,
        colors#8, it#8, pairs#64,
        bel=^G, bold=\E[1m, clear=\E[H\E[J, cr=^M, cub=\E[%p1%dD,

Here's how to read the output

# - comment
the first line of | separated fields are the aliases of the terminal (ends with comma)
3 kinds of settings - all comma separated.
simple words  - boolean variables (eg. am = auto margin)
word#number - sets variable to numbers - eg. colors#8
word=string - sets variable to string

Reference:
http://docstore.mik.ua/orelly/unix/upt/ch41_11.htm

Monday, 22 September 2014

Where does Visual Studio pick up framework assemblies from

From this link : C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework

Visual Studio doesn't use the dlls it finds in the installed framework directories (i.e. %windir%\Microsoft.Net\Framework\v3.0 v4.0 etc. directories). 



Tuesday, 16 September 2014

Android LinearLayout: Adding a button beside an EditText

To layout an EditText and Button side-by-side like so:

Use the following:
LinearLayout
     orientation:horizontal
EditText
    layout_width="0dp"
    layout_weight="1"
Button
    layout_width="wrap_content"


With this the button has enough width to display its contents and then ensure that the edit box fills the rest of the width of the parent.

What does layout_weight do? 

In a LinearLayout, it specifies how to assign the remaining space across different views. Whatever space each view needs (based on wrap_content, explicit width, etc.) would be used first, then any extra space that is available will be assigned to all the views in proportion to their weights.

See here for more details.



Andriod: Creating a Toast with data from the parent Activity of an onClickListener

We'll often register an inner class as an onClickListener. Inside this if there's a need to access the parent Activity use this:
getBaseContext()

Making a Toast message:
Toast.makeText(Context, text, Toast.LENGTH_LONG).show();


Code sample:

        addTask.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String text = ((EditText)findViewById(R.id.task_text)).getText().toString();
                Toast.makeText(getBaseContext(),text, Toast.LENGTH_LONG).show();
            }
        });

Saturday, 13 September 2014

Android AVD Creation: Missing Snapshot and Host GPU options

From:   http://stackoverflow.com/questions/23883415/android-emulator-shnapshot-option-is-missing

The Creation window had to be manually expanded to reveal the hidden options

Just increase the size of this window and it becomes this:

Android Studio: Location of the SDK


If you install Android Studio (and you don't already have the Android SDK?), Android Studio will install the Android SDK inside the Android Studio install directory like so:
<android_studio_install_location>\sdk

Once you download the Intel Hardware accelerated execution manager from the SDK manager, you need to install it - it will be here: 
<android_studio_install_location>\sdk\extras\intel\Hardware_Accelerated_Execution_Manager

Friday, 12 September 2014

.Net Build Error: The "exists" function only accepts a scalar value, but its argument "$(PackageSourceManifest)" evaluates to "[same path];[same path]" which is not a scalar value.

From: http://stackoverflow.com/questions/18006428/vs2012-exists-only-accepts-scalar-values

The solution which worked for me:
Open your .csproj file and look for any duplicated import tag.
In my case the .csproj had two lines like this:
<Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio
\v10.0\WebApplications\Microsoft.WebApplication.targets" />
Remove one of them fixed the problem.

Thursday, 11 September 2014

Finding out which version of .Net Framework you have installed

From: http://msdn.microsoft.com/en-us/library/hh925568(v=vs.110).aspx

To find .NET Framework versions by viewing the registry (.NET Framework 1-4)

  1. On the Start menu, choose Run.
  2. In the Open box, enter regedit.exe.
    You must have administrative credentials to run regedit.exe.
  3. In the Registry Editor, open the following subkey:
    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP
    The installed versions are listed under the NDP subkey. The version number is stored in the Version entry. For the .NET Framework 4 the Version entry is under the Client or Full subkey (under NDP), or under both subkeys.

To find .NET Framework versions by viewing the registry (.NET Framework 4.5 and later)

  1. On the Start menu, choose Run.
  2. In the Open box, enter regedit.exe.
    You must have administrative credentials to run regedit.exe.
  3. In the Registry Editor, open the following subkey:
    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full
Note Note
If the Full subkey is not present, then you do not have a the .NET Framework 4.5 or later installed.
Check for a DWORD value named Release. The existence of the Release DWORD indicates that the .NET Framework 4.5 or newer has been installed on that computer.

The value of the Release DWORD indicates which version of the .NET Framework is installed.
Value of the Release DWORD
Version
378389
.NET Framework 4.5
378675
.NET Framework 4.5.1 installed with Windows 8.1
378758
.NET Framework 4.5.1 installed on Windows 8, Windows 7 SP1, or Windows Vista SP2
379893
.NET Framework 4.5.2

Copied Android Studio project still refers to old layout files


I recently copied a HelloWorld Android Studio app from one location to another. Changes I made to the layout in the new project were just not being picked up. A Ctrl+B on the resource which was being used to refer to the layout opened up the layout file from the old HelloWorld project.

It turns out that there was a variable RES_FOLDERS_RELATIVE_PATH in the app.iml file which was pointing to file://$MODULE_DIR$/../../HelloWorld/app/src/main/res  (the old res folder) - after closing Android Studio and manually changing this to refer to file://$MODULE_DIR$/src/main/res, and removing the .idea\workspace.xml file and things started working as expected.

Wednesday, 10 September 2014

Git: Getting the contents of a file as of a particular SHA


http://stackoverflow.com/questions/7685433/how-to-get-files-and-content-by-sha-of-commit

git show f414f31:Documentation/help.txt

.. where the path Documentation/help.txt is relative to the top level of the working tree, regardless of whether you're in a subdirectory or not. 

Git: Fixing merge conflicts when you want to resolve by taking theirs


git checkout --theirs foo/bar.java
git add foo/bar.java
see: http://stackoverflow.com/questions/10697463/resolve-git-merge-conflict-accepting-their-changes

Git: cherry-pick the last commit on another branch

git cherry-pick `git log -1 --format='%H' <branchname_to_cherry_pick_from>`

# This assumes you are running this in git bash and that you have checked out the branch that you want to cherry-pick into.

Tuesday, 9 September 2014

Unicode - what programmers need to know

From: http://www.joelonsoftware.com/articles/Unicode.html

Unicode represents a letter as a Unicode code point (represented as U+nnnn) - this doesn't say how the letter is stored in memory - only what its conceptual representation is. This can then be stored using some encoding (UTF-8, UTF-16, etc).

UTF-8 uses 1 byte to represent all letters below ASCII 128
UCS-2  alias UTF-16 uses 2 bytes - these can then be big-endian or little-endian and uses an initial FE FF (unicode byte order mark) to indicate which endian it is.

The Unicode code points can be encoded in any encoding scheme you want - could be ASCII, Hebew ANSI, OEM Greek, etc. But if the unicode code point has no visual representation in the encoding scheme of the reader, then you get a little ? or a ? in a rhombus.

A string without an encoding specified doesn't make sense. 

Wednesday, 3 September 2014

Android Notes: Lessons from HelloWorld

Basic Setup

  • Install Android Studio
  • Get the required SDK - I got the latest SDK for 4.4
  • Gradle build tool is bundled with Android Studio? (Maybe, I don't know)
  • Create the Android Virtual Devices (emulator) for testing - use the AVD manager (the AVD manager can also be launched from inside Android Studio, as can the SDK Manager)
    • You may need to install the correct CPU/ABI for the emulator from the SDK Manager.
  • Launch the AVD - you will probably need to set the RAM on the emulator to a low value (200MB) for the device emulator to launch in a decent time. 

Basic development process


  • Write code - target a particular version of the Android SDK
  • Launch emulator
  • Run code. this will:  
    • compile and create an APK
    • install the APK in the emulator
    • launch the app

Structure of an Android HelloWorld App


public class Greetings extends Activity 
  • An Activity represents a screen in the UI
  • Contains a bunch of on* methods (onCreate, onCreateOptionsMenu, ...) which will be invoked when specific events happen.
AndroidManifest.xml
  • Declares various components (including Activities) which are to be a part of the application.
  • Various important pieces:
    • uses-sdk  - which sdk the application can work with (max, min,...)
    • application - defines the application, contains the various components like activities, services, etc.
    • activity (inside application) - defines a screen
    • intent-filter (inside activity, can it be in other places?) - defines what events the activity will listen for?
The res/ Directory
  • Contains various resources needed by the application
  • drawable-* : different images to be drawn, based on screen resolution
  • layout - contains layouts for various activities - how to layout the screen
  • menu?
  • values/strings.xml - different strings to be used in the application
R  Generated class
  • Generated during build - do not hand edit. 
  • Contains various strings/resources/etc. - can be used in the application.

Wednesday, 27 August 2014

Android Emulator startup is slow

http://stackoverflow.com/questions/20051073/emulator-for-android-4-4-kitkat-is-not-starting

Unfortunately the emulator does take a very long time to load. A regular complaint amongst Android developers.
It sometimes helps if you go into the AVD configuration and modify your KitKat emulator and tick the checkbox that says Use Host GPU. This makes it slightly faster.
Also I've found that if you use the default RAM space that it decides, usually over 1000, it takes a stupidly long time, if it loads up at all. I usually set mine to 512MB, 700+MB at a push but reducing the amount of RAM that the emulator can have helps.
There is also a different emulator you can use, although it currently doesn't yet support KitKat, although they say its coming soon, I've heard quite a few positive comments.
You can find the alternative emulator at http://www.genymotion.com/

Creating Custom resolutions for Intel Graphics card

Run C:\Windows\System32\CustomModeApp.exe

Tuesday, 19 August 2014

When emacs displays garbage on screen


From: http://www.nongnu.org/emacsdoc-fr/manuel/lossage.html

32.9.3. Garbage on the Screen

If the data on the screen looks wrong, the first thing to do is see whether the text is really wrong. Type C-l to redisplay the entire screen. If the screen appears correct after this, the problem was entirely in the previous screen update.
Display updating problems often result from an incorrect termcap entry for the terminal you are using. The file etc/TERMS in the Emacs distribution gives the fixes for known problems of this sort. INSTALL contains general advice for these problems in one of its sections. Very likely there is simply insufficient padding for certain display operations. To investigate the possibility that you have this sort of problem, try Emacs on another terminal made by a different manufacturer. If problems happen frequently on one kind of terminal but not another kind, it is likely to be a bad termcap entry, though it could also be due to a bug in Emacs that appears for terminals that have or that lack specific features.

What worked for me: the terminal was cygwin, setting it to something else (xterm) improved matters. I still haven't found the "best" terminal for my usecase


Wednesday, 23 July 2014

Figuring out whether a .Net dll is 64bit or 32bit

 Open the Visual Studio Command Prompt and type "corflags [your assembly]". You'll get something like this:

c:\Program Files (x86)\Microsoft Visual Studio 9.0\VC>corflags "C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Data.dll"

Microsoft (R) .NET Framework CorFlags Conversion Tool. Version 3.5.21022.8 Copyright (c) Microsoft Corporation. All rights reserved.

Version : v2.0.50727
CLR Header: 2.5
PE : PE32
CorFlags : 24
ILONLY : 0
32BIT : 0
Signed : 1

You're looking at PE and 32BIT specifically.

Any CPU:

PE: PE32
32BIT: 0

x86:

PE: PE32
32BIT: 1

x64:

PE: PE32+
32BIT: 0