Skip to content

Instantly share code, notes, and snippets.

View amullins83's full-sized avatar

Austin Mullins amullins83

  • Milsoft Utility Solutions
  • Abilene, TX
View GitHub Profile
@amullins83
amullins83 / define-pure-virtual.cpp
Last active August 29, 2019 01:52
Define a Pure Virtual Method
#include <iostream>
using namespace std;
class Abstract
{
public:
virtual void DoStuff() = 0;
};
@amullins83
amullins83 / README.md
Last active March 29, 2024 15:04
A simple command-line progress bar in C

#Progress in C

This is in an example meant to present some ideas regarding command-line progress bars in C.

##Important parts

The main idea is to overwrite stdout with new information every time a particular step is reached. I accomplished this using the VT100 emulator hack from [this Stack Overflow answer][1]. In a nutshell, printing ^[[2K to stdout erases the current line, but it doesn't necessarily move the cursor to the start. Printing \r does that. Also, I decided I wanted to print a newline character after the progress indicator, but I need to get rid of that newline on the next print. That's what \b does: it inserts a backspace, deleting the last character printed.

Also important is the call to fflush, which will guarantee that the print operation completes and is visible before the program moves on to its next task (see [this Stack Overflow answer][2]).

@amullins83
amullins83 / count_digits.cpp
Created June 14, 2016 21:30
Counting Digits
#include <cmath>
#include <iostream>
#include <iomanip>
#include <vector>
#include <stdint.h>
using namespace std;
size_t countDigitsLog(int number)
{
@amullins83
amullins83 / generic_enum.cpp
Last active April 14, 2016 14:24
Attempt to create a reusable enumerated type wrapper
#include <iostream>
#include <string>
enum Type1 {
What,
Hey
};
enum Type2 {
Yo,
@amullins83
amullins83 / gen_ostream_stuff.cpp
Created April 12, 2016 16:07
Generic operator<< definition?
#include <iostream>
#include <string>
template <typename T>
std::ostream& streamOutEnum(std::ostream& os, T thing)
{
return os << GetString(thing);
}
enum Type1 {
@amullins83
amullins83 / string_size.cpp
Created April 9, 2016 15:49
sizeof(string) does not give you the string's length.
#include <iostream>
#include <string>
using std::cout;
using std::endl;
using std::string;
int main()
{
string sizeStr = "This is a test string that is rather long. Many bytes. Oh yes, quite a few bytes.";
@amullins83
amullins83 / transform.js
Created March 2, 2016 14:55
Simple Chrome Mangler Extension transform script
function drumpfinate(text) {
return text.replace(/\bTrump\b/g, "Drumpf");
}
(function transformTree(transform) {
var walker = document.createTreeWalker(
document.body,
NodeFilter.SHOW_TEXT,
null,
false
@amullins83
amullins83 / manifest.json
Last active March 2, 2016 14:51
Simple Page Mangler Chrome Extension
{
"manifest_version": 2,
"name": "My Drumpf",
"version": "0.0.1",
"description": "Replaces occurrences of 'Drumpf' with 'Drumpf'",
"author": "Austin Mullins",
"content_scripts": [{
"matches": ["http://*/*", "https://*/*"],
"js": ["transform.js"]
}]
@amullins83
amullins83 / README.md
Created September 15, 2015 19:05
Invoke C++ code within C

#Invoking C++ code within a C file

I don't know how useful this is in practice, but today I learned that you can invoke C++ code within a C library if needed. The basic process is to forward-declare C++ structs, then declare the function(s) to be invoked from the C library within an extern "C" block. The file containing these declarations will need to be included in the C source file and in the C++ header and source files. The C file can then be compiled normally with gcc and the C++ source with g++. Finally, the executable will need to be linked with g++ so that C++ operators and keywords will be defined.

This trivial example seems silly, but one can imagine a scenario where a C-library does useful work while taking a function pointer as an argument. The function declared in the extern "C" block here might be accepted as such an argument, and as long as the final

@amullins83
amullins83 / delete_matrix_rows.c
Created February 25, 2015 22:13
Using m_vector in a real project
#include <stdio.h>
#include <malloc.h>
#include "m_vector.h"
#ifdef __cplusplus
extern "C" {
#endif
#define TRUE 1
#define FALSE 0