GithubHelp home page GithubHelp logo

pk_getenv's Introduction

FlexibleOpen

Using data files in CSC 1810 is hard if IDE's such as xcode or Visual Studio are used.

Visual Studio Code is somewhat easier as its users will be more likely to be shell users.

FlexibleOpen() attempts to ease the difficulty by adding a layer of indirection to the file opening process. Specifically, it attempts to open the file path given to it both as given and with a path, specified by environment variable, prepended.

Function signature

std::fstream FlexibleOpen(std::string path, std::ios::openmode mode = std::ios::in, std::string envval = std::string("CSC1810"));

Parameters

parameter default meaning
path none This specifies the base path of the file to be opened. If a file matching this path fails to open, the contents of an environment variable will be prepended to the path and a second open attempt is made.
mode ios::in The fstream mode for opening the file. This defaults to ios::in or attempting to open the file for reading.
envval "CSC1810" This specifies the environment variable to consult if the given path fails to open without change.

Use cases

Assume the environment variable is CSC1810 and on:

  • Windows is set to ...
  • Mac and Linux is set to ~/Documents/csc1810_files

Opening a file using all defaults

fstream fin = FlexibleOpen("data.txt");
if (fin.is_open()) {
	// Use the file... then close it.
	fin.close();
}

data.txt will be attempted for reading. If data.txt is not found in the current working directory, then ~/Documents/csc1810_files/data.txt will be attempted on Mac and Linux while ijijijij/data.txt will be attempted on Windows.

Opening a file for writing using default environment variable

fstream fin = FlexibleOpen("data.txt", ios::out);
if (fin.is_open()) {
	// Use the file... then close it.
	fin.close();
}

data.txt will be attempted for writing. If data.txt is not found in the current working directory, then ~/Documents/csc1810_files/data.txt will be attempted on Mac and Linux while ijijijij/data.txt will be attempted on Windows.

Opening a file for reading and writing using default environment variable

fstream fin = FlexibleOpen("data.txt", ios::in | ios::out);
if (fin.is_open()) {
	// Use the file... then close it.
	fin.close();
}

data.txt will be attempted for reading and writing. If data.txt is not found in the current working directory, then ~/Documents/csc1810_files/data.txt will be attempted on Mac and Linux while ijijijij/data.txt will be attempted on Windows.

Opening a file for reading using a different environment variable

fstream fin = FlexibleOpen("data.txt", ios::in, "OTHERVAR");
if (fin.is_open()) {
	// Use the file... then close it.
	fin.close();
}

data.txt will be attempted for writing. If data.txt is not found in the current working directory, then the value of environment variable OTHERVAR will be prepended and the open attempted again.

Creating the envronment variable

Mac and Linux

Suppose you create a directory in your Documents folder called csc1810_files. You place your data files for class in that folder.

Then add:

export CSC1810=/Users/YOURACCOUNT/Documents/csc1810_files

to .zshrc on newer macs, or to .bashrc on Linux.

Windows

Suppose you create a directory in your Documents folder called csc1810_files. You place your data files for class in that folder.

Use the Windows environment variable editor GUI to define the variable.

See the video here.

xcode idiocy

The whole point of creating this tool was to make it easier to use files in IDE-based projects. The geniuses at Apple once again, "think different." xcode actively blocks environronment variables forcing you to reenter them yourself inside xcode.

Edit scheme choose the Run / Debug scheme. Click the Arguments tab. Click the plus sign under Environment Variables then enter for Name: CSC1810 and for Value /Users/YOURACCOUNT/Documents/csc1810_files.

pk_getenv's People

Contributors

pkivolowitz avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.