Skip to content

Instantly share code, notes, and snippets.

@exoad
Created March 20, 2022 16:46
Show Gist options
  • Save exoad/9435651edc02fcaaa096d9fddb87dfbf to your computer and use it in GitHub Desktop.
Save exoad/9435651edc02fcaaa096d9fddb87dfbf to your computer and use it in GitHub Desktop.
Writing to SD Card | Vex V5 C++
/*
Copyright 2022 Jack Meng
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <string>
#include <fstream>
#include <iostream>
#include <sstream>
#include <iterator>
#include <sys/stat.h>
#include <unistd.h>
#include <vector>
#include <assert.h>
#include "vex.h"
#define bprint Brain.Screen.print
#define bcursor Brain.Screen.setCursor
#define sdsave Brain.SDcard.savefile
#define sdload Brain.SDcard.loadfile
#define check() Brain.SDcard.isInserted()
#define len(arr) *(&arr + 1) - arr
#define F0R(arg) for(int i = 0; i < len(arg); i++)
brain Brain;
using namespace vex;
using namespace std;
// CONFIGURATION BEGIN
const uint8_t SYNC_TIME = 20;
const char* FILE_NAME = "wow.txt";
// CONFIGURATION END
// --------------
// I/O UTIL START
template <typename BRUH >
string tostring(BRUH t) {
ostringstream ss;
ss << t;
return ss.str();
}
template <typename GCC_VECTOR >
void vtostring(GCC_VECTOR g) {
for(auto &x : g) {
cout << x << endl;
}
}
template <typename SOME_INT >
int toint(SOME_INT t) {
stringstream ss;
ss << t;
int temp = NULL;
ss >> temp;
return temp;
}
// I/O UTIL END
// --------------
// ------------------
// GENERAL UTIL START
string randName() {
string str = "";
for(int i = 0; i < 32; i++) {
str[i] = (rand() % 26) + 'a';
}
return str;
}
// GENERAL UTIL END
// ----------------
void sprint(string content, string name = "vex_" + randName(), bool confirm = true) {
uint32_t s[content.length()];
if(!check()) {
__throw_system_error(2);
} else {
for(int i = 0; i < content.length(); i++) {
s[i] = (char) content[i];
}
sdsave(name.c_str(), (uint8_t*) s, *(&s + 1) - s);
if(confirm) {
cout << "File saved as: " + name << endl;
bprint(name.c_str());
}
}
}
bool exists(string name) {
return ( access( name.c_str(), F_OK ) != -1 );
}
void xprint(string content, string name = "vex" + randName(), const bool confirm = true) {
ofstream fout(name);
if(!check()) {
__throw_system_error(2);
} else {
fout << content;
fout.flush();
if(confirm) {
cout << "File saved as: " + name << endl;
bprint(name.c_str());
}
}
}
void aprint(string content, string name, const bool confirm = true) {
if(exists(name)) {
Brain.Screen.print("NOT ALLOWED TO APPEND TO PRE-EXISTING FILE!!!");
return;
}
ofstream fout(name, ios_base::app);
if(!check()) {
__throw_system_error(2);
} else {
fout << content;
fout.flush();
if(confirm) {
cout << "File appended to: " + name << endl;
bprint(name.c_str());
}
}
}
string xread(string file_name, bool confirm = true) {
ifstream fin(file_name);
string buffer = "";
if(!check()) {
__throw_system_error(2);
} else {
unsigned int curr = 0;
string temp_line_buffer = "";
while(getline(fin, temp_line_buffer)) {
buffer += temp_line_buffer + DELIMITER;
if(confirm) {
cout << "[ Line " << curr << " ] " << temp_line_buffer.length() << " chars" << endl;
}
temp_line_buffer = "";
curr++;
}
if(confirm) {
cout << "[ Total read ] " << buffer << endl;
}
}
return buffer == "" ? "NOTHING" : buffer;
}
vector<string> vread(string file_name, bool confirm = true) {
ifstream fin(file_name);
vector<string> vs;
if(!check()) {
__throw_system_error(2);
} else {
unsigned int curr = 0;
string temp_line_buffer = "";
while(getline(fin, temp_line_buffer)) {
vs.push_back(temp_line_buffer);
if(confirm) {
cout << "[ Line " << curr << " ] " << temp_line_buffer << " " << endl;
}
temp_line_buffer = "";
curr++;
}
if(confirm) {
cout << "[ Total read ] " << vs.size() << endl;
}
}
return vs;
}
int main() {
vexcodeInit();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment