SuperMonkey

BMstdlib - bibliothèque standard du BootMonkey.

Aug 19th, 2012
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.13 KB | None | 0 0
  1. #include "bmlib.h"
  2.  
  3. /**** maths ****/
  4. int MathRandom(int max, int min) {
  5.     srand(time(0));
  6.     srand(rand()); //pour que l'aléatoire soit presque réélement aléatoire
  7.     return (rand() % max) + min;
  8. }
  9.  
  10. int MathRound(float a) {
  11.     return ceil(a - 0.5);
  12. }
  13.  
  14. int MathCeil(float a) {
  15.     return ceil(a); //très utile...
  16. }
  17.  
  18. double MathRoot(float a, int expo) {
  19.     return pow(a, (double) (1 / (double) expo));
  20. }
  21.  
  22. double MathFloor(float a) {
  23.     return floor(a); //utilité ^2
  24. }
  25.  
  26. /**** fichiers ****/
  27. //return 0 si fichier ouvert avec succès
  28. int FileOpen(char* fname, char* mode) {
  29.     FILE* f = NULL;
  30.     if((f = fopen(fname, mode)) == NULL) {
  31.         return errno;
  32.     }
  33.     fclose(f);
  34.     return 0;
  35. }
  36.  
  37. int FileWrite(char* fname, const string& content) {
  38.     FILE* f = NULL;
  39.     if((f = fopen(fname, "a")) == NULL) {
  40.         return errno;
  41.     }
  42.     fputs(content.c_str(), f);
  43.     fclose(f);
  44.     return errno;
  45. }
  46.  
  47. int FileWriteLine(char* fname, const string& content) {
  48.     return FileWrite(fname, "\n"+content);
  49. }
  50.  
  51. string FileGetContents(char* fname) {
  52.     FILE* f = NULL;
  53.     if((f = fopen(fname, "r")) == NULL) {
  54.         return "";
  55.     }
  56.     string out = "";
  57.     char buffer[200];
  58.     while (fgets(buffer, 200, f) != NULL) {
  59.         out += buffer;
  60.     }
  61.  
  62.     fclose(f);
  63.     return out;
  64. }
  65.  
  66. int FileWriteFirstLine(char* fname, const string& content) {
  67.     FILE* f = NULL;
  68.     string c = content+FileGetContents(fname);
  69.  
  70.     if((f = fopen(fname, "w+")) == NULL) {
  71.         return errno;
  72.     }
  73.     int result = fputs(c.c_str(), f);
  74.  
  75.     fclose(f);
  76.     return result;
  77. }
  78.  
  79. int FileRewrite(char* fname, const string& content) {
  80.     FILE* f;
  81.  
  82.     if((f = fopen(fname, "w+")) == NULL) {
  83.         return errno;
  84.     }
  85.  
  86.     fputs(content.c_str(), f);
  87.  
  88.     fclose(f);
  89.     return errno;
  90. }
  91.  
  92. bool FileIsEmpty(char* fname) {
  93.     FILE* f;
  94.     bool empty;
  95.     if((f = fopen(fname, "r")) == NULL) {
  96.         return true;
  97.     }
  98.  
  99.     fseek(f, 0, SEEK_END);
  100.     if(ftell(f) == 0) {
  101.         empty = true;
  102.     }
  103.  
  104.     fclose(f);
  105.     return empty;
  106. }
  107.  
  108. int FileCreate(char* fname) {
  109.     fclose(fopen(fname, "w"));
  110.     return errno;
  111. }
  112.  
  113. int FileDelete(char* fname) {
  114.     remove(fname);
  115.     return errno;
  116. }
  117.  
  118. int FileRename(char* fname, char* nwName) {
  119.     rename(fname, nwName);
  120. }
  121.  
  122. vector<string> FileGetLines(char* fname) {
  123.     vector<string> out;
  124.     string ligne;
  125.     ifstream f(fname, ios::in);
  126.  
  127.     if(f) {
  128.         while(getline(f, ligne)) {
  129.             out.push_back(ligne);
  130.         }
  131.  
  132.         f.close();
  133.     }
  134.  
  135.     return out;
  136. }
  137.  
  138. /**** arrays ****/
  139. //dit si un élément el est dans un tableau array
  140. template <typename T>
  141. bool ArrayIn(const T& el, const vector<T>& array) {
  142.     for(int i=0; i < array.size(); i++) {
  143.         if(el == array[i]) return true;
  144.     }
  145.     return false;
  146. }
  147.  
  148. template <typename T>
  149. size_t ArraySize(const vector<T>& array) {
  150.     return array.size();
  151. }
  152.  
  153. template <typename T>
  154. bool ArrayEmpty(const vector<T>& array) {
  155.     return array.empty();
  156. }
  157.  
  158. template <typename T>
  159. string ArrayJoin(const vector<T>& array, string jointure) {
  160.     string out = "";
  161.     for(int i=0; i < array.size(); i++) {
  162.         out += array[i];
  163.         if(i+1 != array.size())
  164.             out += jointure;
  165.     }
  166.  
  167.     return out;
  168. }
  169.  
  170. /**** strings ****/
  171. //pas la peine d'expliquer les deux fonctions suivantes. :)
  172. string StringLower(const string& str) {
  173.     string rt = "";
  174.     rt.reserve(str.size());
  175.     for(size_t i = 0; i < str.size(); i++) {
  176.         rt += tolower(str[i]);
  177.     }
  178.  
  179.     return rt;
  180. }
  181.  
  182. string StringUpper(const string& str) {
  183.     string rt = "";
  184.     rt.reserve(str.size());
  185.     for(size_t i = 0; i < str.size(); i++) {
  186.         rt += toupper(str[i]);
  187.     }
  188.  
  189.     return rt;
  190. }
  191.  
  192. //retourne la taille d'une chaine de caractère.
  193. //equivalent à strlen
  194. size_t StringLength(const string& str) {
  195.     return str.size();
  196. }
  197.  
  198. //retourne la position de motif dans str2explore.
  199. // -1 si motif n'est pas dans str2explore
  200. size_t StringFind(const string& motif, const string& str2explore) {
  201.     return str2explore.find(motif);
  202. }
  203.  
  204. string StringGetSubStr(string str, size_t pos, size_t n) {
  205.     return str.substr(pos, n);
  206. }
  207.  
  208. /**** console ****/
  209.  
  210. template <typename T>
  211. void ConsolePrint(T disp) {
  212.     cout << disp;
  213. }
  214.  
  215. template <typename T>
  216. void ConsolePrintln(T disp) {
  217.     cout << endl << disp;
  218. }
  219.  
  220. template <typename T>
  221. inline void ConsoleAsk(T& ptr) {
  222.     cin >> ptr;
  223. }
  224.  
  225. inline void ConsoleAsk(string& ptr) {
  226.     getline(cin, ptr);
  227. }
  228.  
  229. inline void ConsoleClean() {
  230.     #ifdef __linux
  231.     printf("\033[H\033[2J");
  232.     #endif
  233.     #ifdef _WIN32
  234.     system("cls");
  235.     #endif
  236. }
  237.  
  238. inline void ConsoleHide() {
  239.     #ifdef _WIN32
  240.     FreeConsole();
  241.     #endif
  242. }
  243.  
  244. /**** program ****/
  245.  
  246. inline void ProgramSleep(int sec) {
  247.     #ifdef _WIN32
  248.     Sleep(sec * 1000);
  249.     #endif
  250.     #ifdef __linux
  251.     sleep(sec);
  252.     #endif
  253. }
  254.  
  255. inline void ProgramSleepMs(int ms) {
  256.     #ifdef _WIN32
  257.     Sleep(ms);
  258.     #endif
  259.     #ifdef __linux
  260.     usleep(ms);
  261.     #endif
  262. }
  263.  
  264. inline void ProgramExit(int code) {
  265.     exit(code);
  266. }
  267.  
  268. int main() {
  269.  
  270. }
Advertisement
Add Comment
Please, Sign In to add comment