Skip to content

Instantly share code, notes, and snippets.

@CDAGaming
Last active September 10, 2020 16:46
Show Gist options
  • Save CDAGaming/54efa1e37984295503a96156bedff6a0 to your computer and use it in GitHub Desktop.
Save CDAGaming/54efa1e37984295503a96156bedff6a0 to your computer and use it in GitHub Desktop.
VSCode - Compatibility Data (See Original Comment for Details)
// CompatibilityData.cpp
// Modified by Christopher Stack, 2020
//
#include <iostream>
#include <time.h> //ctime
#include <assert.h> //assert
#include <sys/timeb.h> //_timeb _ftime_s
using namespace std;
#pragma region Compatibility_Wrappers
// If not on Windows, Redirect fopen_s -> fopen
#if !defined(WIN32) && !defined(_WIN32) && !defined(__WIN32)
#define fopen_s(fp, fmt, mode) ({\
*(fp)=fopen( (fmt), (mode));\
(*(fp) ) ? 0:errno;\
})
#endif
// AwaitInput: Outputs a "Press any Key to Continue" Input to await input
void AwaitInput() {
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
system("pause");
#else
system("read");
#endif
}
// get_ctime: Compatibility Function for retrieving TimeDate information
const char *get_ctime(char *buffer, size_t bufsize, const time_t *cur_time)
{
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
errno_t e = ctime_s(buffer, bufsize, cur_time);
assert(e == 0 && "Huh? ctime_s returned an error");
return buffer;
#else
const char *res = ctime_r(buffer, cur_time);
assert(res != NULL && "ctime_r failed...");
return res;
#endif
}
#pragma endregion Utilities imported for Compatibility between Other OS Systems
#pragma region Utility_Methods
// ToLowerCase: Port of Java function to force all string characters into their lowercase form
string ToLowerCase(string input) {
string output;
for (string::size_type i = 0; i < input.length(); ++i)
output += tolower(input[i]);
return output;
}
// ToUpperCase: Port of Java function to force all string characters into their uppercase form
string ToUpperCase(string input) {
string output;
for (string::size_type i = 0; i < input.length(); ++i)
output += toupper(input[i]);
return output;
}
// PrintTime: Displays an Output of the Current Time and Date
int PrintTime()
{
char timeline[26];
long milliSecond;
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
struct _timeb timebuffer;
_ftime_s(&timebuffer);
get_ctime(timeline, sizeof(timeline), &(timebuffer.time));
milliSecond = timebuffer.millitm;
#else
struct timespec timeSpec;
int resultCode;
resultCode = clock_gettime(CLOCK_REALTIME, &timeSpec);
get_ctime(timeline, sizeof(timeline), &timeSpec.tv_sec);
milliSecond = (timeSpec.tv_nsec / CLOCKS_PER_SEC);
#endif
printf("Current Time => %.19s.%hu %s", timeline, milliSecond, &timeline[20]);
// Add an Additional Newline to prettify output a bit more
cout << endl;
return 0;
}
#pragma endregion Utilities used in Methods
{
"version": "0.2.0",
"configurations": [
{
"name": "g++ build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "g++ build active file",
"miDebuggerPath": "/usr/bin/gdb"
},
{
"name": "cl.exe build and debug active file",
"type": "cppvsdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"preLaunchTask": "cl.exe build active file"
}
]
}
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "g++ build active file",
"command": "/usr/bin/g++",
"args": ["-g", "${file}", "-o", "${fileDirname}/${fileBasenameNoExtension}"],
"options": {
"cwd": "/usr/bin"
},
"problemMatcher": ["$gcc"],
"group": {
"kind": "build",
"isDefault": true
}
},
{
"type": "shell",
"label": "cl.exe build active file",
"command": "cl.exe",
"args": [
"/Zi",
"/EHsc",
"/Fe:",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"${file}"
],
"problemMatcher": ["$msCompile"],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
@CDAGaming
Copy link
Author

CDAGaming commented Sep 10, 2020

Disclaimer: I am merely a programmer learning among others, and this was merely a solution to a problem. This description and guide assumes you know what you are doing, though I take no responsibility if something messes up here, unless it was due to a coding error I made. Caution is advised, though this was tested with both Linux and Windows with success.

Description:

In a CS course where I'm studying for my degree, I among others had received code for reading Binary Files and getting the current timestamp with the Date and Time including milliseconds.

As Visual Studio Code has become more prominent over time as well as being cross-compatible, one jump in my workflow included jumping from Visual Studio to VSCode as well as documenting my code over time.

As the original codebase given was not compatible with anything besides Windows and VSCode lacked a sure-fire way to both debug and feel more familiar to Visual Studio, this gist resolves both of those issues, in the ways outlined below:

  1. Compatibility Wrappers for get_ctime, The "Press any Key to Continue" action, and formatting for the PrintTime() function have now been tweaked to the point of successfully working on both Unix and Windows under the same format. MacOS has not been tested under the new functions, but should work fine. Please report any Issues here though if there are issues from my adjustments.

  2. Added launch.json and tasks.json with two different tasks attached to them. These two files go into the {$PROJECT_ROOT}/.vscode directory (Created when you first open a project in VSCode). These two files are responsible for controlling the debugging and build behavior for projects, and in this case, designating a task to build both via g++/gdb and cl.exe/msbuild. For Unix, BOTH g++ and gdb will need to be installed for successful compilation, while on windows you'll need to install MINGW and trigger an install for the g++ compiler OR if using msbuild/cl.exe, ensure c++ dependencies are installed from the Visual Studio Installer and that your VSCode project is launched from the Developer command prompt via code ${PROJECT_ROOT}. Note that the Developer Command Prompt and VSCode do not like whitespace or symbols in project directories.

Build Shortcut: CTRL+SHIFT+B to manually select a build task to execute; Play button in top right to play/build auto if you have the Code Runner extension
Use the Run tab on the Left side of VSCode by default in order to select a debugging task then press the Green play button beside the task name.

Additional Info:

Additionally as an added bonus, I've added helper methods I've used in the past (2018 - present) which also are used in my Java Applications. These are the ToLowerCase() and ToUpperCase() methods and should likely serve as a useful asset.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment