Smipple is shutting down Nov 30, 2017. Some features are currently disabled.
Profile image for svein kristian nykaas skn
The Macros.cpp source for the macro-class of my current project.
Language
C++
Tags
c++ class

Macros class with functions

1 #include <string> 2 #include <fstream> 3 #include <iostream> 4 #include <cstdlib> 5 #include "Macros.hpp" 6 #include "ColorChange.hpp" 7 8 Macro::Macro(int i, const std::string& n, const std::string& m){ 9 name = n; 10 mtu = m; 11 id = i; 12 } 13 14 int Macro::getID(){ 15 return id; 16 } 17 18 std::string Macro::getMTU(){ 19 return mtu; 20 } 21 22 std::string Macro::getName(){ 23 return name; 24 } 25 26 namespace macFun{ 27 // Creates a new macro 28 void addMacro(std::string& name, std::string& mtu){ 29 std::fstream macFile("macros.dat", std::ios::in); 30 int id; 31 32 // Get the latest ID from the macros.dat file, if it dosen't exist default id to 0 33 if(macFile.is_open()){ 34 while(macFile.good()){ 35 std::string line = ""; 36 std::getline(macFile,line); 37 if(!line.empty()){ 38 std::string tmpID = "id=\""; 39 int idL = tmpID.length(); 40 41 int found, found2; 42 // get the ID 43 found = line.find(tmpID); 44 std::string tmp = line.substr(found + idL); 45 id = atoi(tmp.c_str()); 46 } 47 } 48 macFile.close(); 49 }else{ 50 id = 0; 51 } 52 // Create the macro object 53 Macro m(++id, name, mtu); 54 55 saveMacro(m); 56 } // end of addMacro 57 58 59 // Save current Macro into the macros.dat file 60 void saveMacro(Macro m){ 61 // Load up file to check if it the file exists, if it does check if it contains more then 1 non-empty line. 62 // if it does changes exists to true, if it exist but the lines are empty(file is empty) deletes the file. 63 std::fstream macFile("macros.dat", std::fstream::in); 64 bool exist = false, emptyFile = false; 65 66 if(macFile.is_open()){ 67 int lines = 0; 68 while(macFile.good()){ 69 std::string t; 70 std::getline(macFile,t); 71 if(!t.empty()) 72 lines++; 73 } 74 if(lines > 0) 75 exist = true; 76 else{ 77 emptyFile = true; 78 } 79 }macFile.close(); 80 81 if(emptyFile) 82 remove("macros.dat"); 83 84 macFile.open("macros.dat", std::fstream::app | std::fstream::out); 85 if(macFile.is_open()){ 86 87 // get the ID, Name and MTU from the macro class 88 std::string strID,name,mtu; 89 int id = m.getID(); 90 91 std::ostringstream convert; 92 convert << id; 93 strID = convert.str(); 94 95 name = m.getName(); 96 mtu = m.getMTU(); 97 98 if(exist) 99 macFile << std::endl << "id=\"" << strID << "\"" << " name=\"" << name << "\" mtu=\"" << mtu << "\""; 100 else 101 macFile << "id=\"" << strID << "\"" << " name=\"" << name << "\" mtu=\"" << mtu << "\""; 102 macFile.close(); 103 } 104 } // end of saveMacro 105 106 // load up data from the macros.dat folder 107 std::vector<Macro> loadMacros(){ 108 std::vector<Macro> macros; 109 110 std::fstream macFile("macros.dat", std::fstream::in); 111 112 if(macFile.is_open()){ 113 while(macFile.good()){ 114 int id; 115 std::string line = "", name, mtu; 116 std::getline(macFile,line); 117 if(!line.empty()){ 118 119 // strings contain the data-splitter\formaters. integers contain the lenght of them. 120 std::string tmpID = "id=\"", tmpName = "name=\"", tmpMTU = "mtu=\""; 121 int idL = tmpID.length(), nameL = tmpName.length(), mtuL = tmpMTU.length(); 122 123 int found, found2; 124 125 // get the ID 126 found = line.find(tmpID); 127 std::string tmp = line.substr(found + idL); 128 id = atoi(tmp.c_str()); 129 130 // get the name 131 found = line.find(tmpName); 132 found2 = line.find("\"",found + nameL); 133 134 name = line.substr(found + nameL, (found2 - (found + nameL))); 135 136 // get the MTU value 137 found = line.find(tmpMTU); 138 found2 = line.find("\"",found + mtuL); 139 140 mtu = line.substr(found + mtuL, (found2 - (found + mtuL))); 141 142 } 143 Macro m(id, name, mtu); 144 macros.push_back(m); 145 } 146 macFile.close(); 147 return macros; 148 } 149 } // end of loadMacros 150 151 /* // Functions to be added later 152 void delMacro(int id); 153 void editMacro(int id,string& name, string& mtu); 154 155 */ 156 }

Comments