Coding

All things computer programming related...

Diet tracker program

- Posted in Coding by

I've been pretty pleased with recent progress I've made on diet.exe, a console program I'm developing to help make tracking my diet easier. As of today's date, the software can synchronize diet files between Dropbox and Zim. As an added benefit, when it does this, it also synchronizes blood pressure readings between those two directories. Sweet!

I've downloaded a couple of solo gaming systems I want to read and perhaps take on a test-drive: World of Dungeons is one, Notequest the other. I have a feeling that my myth-weavers game may be in it's death throes. One player bowed out, albeit gracefully. I must admit, my own interest has begun to flag. We're at a natural point for an ending, as the party has saved Slaytonthorpe from a group of bandits who appear to be working for a much more sinister (and currently unknown) force.


I've read through the pdf for Notequest and I'm intrigued. I may look into getting the Expanded World pdf. I'm going to continue reading The Solo Adventurer's Toolkit by Paul Bimler. I may wind up using it with Black Streams, which would allow me to use the wealth of OSR and 5e adventure modules I have.

Programmatically open a command prompt or Explorer window

- Posted in Coding by

To open a command prompt:

start cmd.exe /k cd C:\Users\kyrat\Dropboxcs_dev

To instead open an Explorer window there:

START C:\Users\kyrat\Dropboxcs_dev

Compressing to and decompressing from zip archive

- Posted in Coding by

This is non-trivial, but at some point in the past I looked into compressing and decompressing programmatically using C#:

I've hosted the source code on GitHub demonstrating using .NET to compress a folder and its contents into a single .zip archive. It also demonstrates decompressing such an archive into its component directory, subdirectories, and files.

And here are a couple libraries you'll need to use: /r:System.IO.Compression.dll /r:System.IO.Compression.FileSystem.dll

Zip from the Windows command line

- Posted in Coding by

If a valid subdirectory is passed in as an argument on the command-line, will zip it to .zip file that is located in same directory as executing code:

download sourcecode

Get full path of executing assembly

- Posted in Coding by

C# code to get the full path of the executing assembly:

<script src="https://gist.github.com/kyrathasoft/825c2c277bdaed4d300880415f72ab5b.js"></script>

then there's also this:

<script src="https://gist.github.com/kyrathasoft/4f4f1d6639c6faff0b2e4c77e60ede87.js"></script>

using System;
using System.Reflection; //required for Assembly namespace
using System.IO; //required for Path namespace

namespace HelloWorld {
class Hello {

    static void Main(string[] args){
        Console.WriteLine("n Now executing: {0}", ExecutingAssemblyPath());
        Console.Write("n Press any key to exit... ");
        Console.ReadKey();
        Console.WriteLine();
    }

    public static string ExecutingAssemblyPath(){
    string path = AppDomain.CurrentDomain.BaseDirectory;
    string fname = Assembly.GetEntryAssembly().GetName().Name;
    path += fname;
    if(File.Exists(fname + ".exe")){ path += ".exe"; } else {path += ".dll"; }
        return path;
    }    

  }
}

I'm very interested in Twine

- Posted in Coding by

I've been interested in Twine 2.0 since it emerged onto the IF scene in 2009. But I only recently (August 2019) revisited it and began to experiment with it in earnest. As I nail down concepts, achieve insights, and come up with snippets of script that work, I'm collecting them here.

Some credit goes to Chapel and the code and examples he's made available on the Twine lab under this license.

I keep a copy of the Windows version 2.3.3 of Twine here. It's an 84.8 Mb download.

If you want a zero-install Twine engine, with some additional story formats and an extractible Web-to-Exe that can turn your Twine stories into stand-alone applications, get this. It's an 85.8 Mb download. You can put it wherever you want on your hard drive (excepting the Program Files directory) and pin a shortcut to the Twine executable to your Start Menu or Desktop.

If you experiment with Twine and decide you want to spend some extended time using it, you may want to peruse Chapel's custom macros. You can put together a custom download of the macros you like here.

Get top level subdirectories from a given directory

- Posted in Coding by

Here is a program demonstrating how to get the names of the top-level subdirectories in a given directory:

String manipulation in C#

- Posted in Coding by

Here is a method that, if passed a proper noun, such as a person's name or a street address, will return a properly formatted version of that string:

And here is an example of using method ProperlyCapitalizeName in a program:

Bytes to characters and vice versa

- Posted in Coding by

It just so happens that any character we might use in a string has a numerical encoding. The alphabet ranges from 65-90 for capital letters A through Z, and from 97-122 for lowercase letters a-z. When we want to work with text encoding, we need to add the following namespace to the top of our source code file:

using System.Text;

The numerical representation of our English alphabet takes the form of unique numbers for each upper and lowercase letter, and these are stored in a variable type called byte. A byte is an 8-bit integer that can hold values from 0 to 255.

If we have a string variable named sChar, we can obtain its numerical encoding as follows, if we have referenced the System.Text namespace:

byte[] bt = Encoding.Default.GetBytes(sChar);

The reverse is encoding bytes to characters. If we have an array of bytes, we can convert each element’s value to its corresponding character:

byte[] bytes = {40, 80, 20};
char result;
foreach(byte number in bytes)
{
  result = Convert.ToChar(number);
  Console.WriteLine(“{0} converts to {1}.”, number, result);
}

Full source code listing:

download sourcecode

Recursive file counting

- Posted in Coding by
using System;
using System.Reflection;
using System.IO;

namespace GetFilesRecursively {
class Program {

    static void Main(){
        int cnt = 0;
        long bites = 0;
        string p = string.Empty;
        foreach (string file in System.IO.Directory.GetFiles(
            GetExecutingDirectoryName(), "*",SearchOption.AllDirectories))
        {
            cnt++;
            FileInfo fi = new FileInfo(file);
            bites += fi.Length;
            //do something with file
            p = String.Format("{0:n0}", (int)bites);
        }   
        Console.Write(" Total files: {0}, ", cnt.ToString());
        Console.WriteLine("totaling {0} bytes.", p);
    }

    /* found following method here: 
    https://www.red-gate.com/simple-talk/blogs/c-getting-
    the-directory-of-a-running-executable/ */
    public static string GetExecutingDirectoryName()
    {
        var location = new Uri(Assembly.GetEntryAssembly().GetName().CodeBase);
        return new FileInfo(location.AbsolutePath).Directory.FullName;
    }       
  }
}
Page 8 of 11