Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "bmlib.h"
- /**** maths ****/
- int MathRandom(int max, int min) {
- srand(time(0));
- srand(rand()); //pour que l'aléatoire soit presque réélement aléatoire
- return (rand() % max) + min;
- }
- int MathRound(float a) {
- return ceil(a - 0.5);
- }
- int MathCeil(float a) {
- return ceil(a); //très utile...
- }
- double MathRoot(float a, int expo) {
- return pow(a, (double) (1 / (double) expo));
- }
- double MathFloor(float a) {
- return floor(a); //utilité ^2
- }
- /**** fichiers ****/
- //return 0 si fichier ouvert avec succès
- int FileOpen(char* fname, char* mode) {
- FILE* f = NULL;
- if((f = fopen(fname, mode)) == NULL) {
- return errno;
- }
- fclose(f);
- return 0;
- }
- int FileWrite(char* fname, const string& content) {
- FILE* f = NULL;
- if((f = fopen(fname, "a")) == NULL) {
- return errno;
- }
- fputs(content.c_str(), f);
- fclose(f);
- return errno;
- }
- int FileWriteLine(char* fname, const string& content) {
- return FileWrite(fname, "\n"+content);
- }
- string FileGetContents(char* fname) {
- FILE* f = NULL;
- if((f = fopen(fname, "r")) == NULL) {
- return "";
- }
- string out = "";
- char buffer[200];
- while (fgets(buffer, 200, f) != NULL) {
- out += buffer;
- }
- fclose(f);
- return out;
- }
- int FileWriteFirstLine(char* fname, const string& content) {
- FILE* f = NULL;
- string c = content+FileGetContents(fname);
- if((f = fopen(fname, "w+")) == NULL) {
- return errno;
- }
- int result = fputs(c.c_str(), f);
- fclose(f);
- return result;
- }
- int FileRewrite(char* fname, const string& content) {
- FILE* f;
- if((f = fopen(fname, "w+")) == NULL) {
- return errno;
- }
- fputs(content.c_str(), f);
- fclose(f);
- return errno;
- }
- bool FileIsEmpty(char* fname) {
- FILE* f;
- bool empty;
- if((f = fopen(fname, "r")) == NULL) {
- return true;
- }
- fseek(f, 0, SEEK_END);
- if(ftell(f) == 0) {
- empty = true;
- }
- fclose(f);
- return empty;
- }
- int FileCreate(char* fname) {
- fclose(fopen(fname, "w"));
- return errno;
- }
- int FileDelete(char* fname) {
- remove(fname);
- return errno;
- }
- int FileRename(char* fname, char* nwName) {
- rename(fname, nwName);
- }
- vector<string> FileGetLines(char* fname) {
- vector<string> out;
- string ligne;
- ifstream f(fname, ios::in);
- if(f) {
- while(getline(f, ligne)) {
- out.push_back(ligne);
- }
- f.close();
- }
- return out;
- }
- /**** arrays ****/
- //dit si un élément el est dans un tableau array
- template <typename T>
- bool ArrayIn(const T& el, const vector<T>& array) {
- for(int i=0; i < array.size(); i++) {
- if(el == array[i]) return true;
- }
- return false;
- }
- template <typename T>
- size_t ArraySize(const vector<T>& array) {
- return array.size();
- }
- template <typename T>
- bool ArrayEmpty(const vector<T>& array) {
- return array.empty();
- }
- template <typename T>
- string ArrayJoin(const vector<T>& array, string jointure) {
- string out = "";
- for(int i=0; i < array.size(); i++) {
- out += array[i];
- if(i+1 != array.size())
- out += jointure;
- }
- return out;
- }
- /**** strings ****/
- //pas la peine d'expliquer les deux fonctions suivantes. :)
- string StringLower(const string& str) {
- string rt = "";
- rt.reserve(str.size());
- for(size_t i = 0; i < str.size(); i++) {
- rt += tolower(str[i]);
- }
- return rt;
- }
- string StringUpper(const string& str) {
- string rt = "";
- rt.reserve(str.size());
- for(size_t i = 0; i < str.size(); i++) {
- rt += toupper(str[i]);
- }
- return rt;
- }
- //retourne la taille d'une chaine de caractère.
- //equivalent à strlen
- size_t StringLength(const string& str) {
- return str.size();
- }
- //retourne la position de motif dans str2explore.
- // -1 si motif n'est pas dans str2explore
- size_t StringFind(const string& motif, const string& str2explore) {
- return str2explore.find(motif);
- }
- string StringGetSubStr(string str, size_t pos, size_t n) {
- return str.substr(pos, n);
- }
- /**** console ****/
- template <typename T>
- void ConsolePrint(T disp) {
- cout << disp;
- }
- template <typename T>
- void ConsolePrintln(T disp) {
- cout << endl << disp;
- }
- template <typename T>
- inline void ConsoleAsk(T& ptr) {
- cin >> ptr;
- }
- inline void ConsoleAsk(string& ptr) {
- getline(cin, ptr);
- }
- inline void ConsoleClean() {
- #ifdef __linux
- printf("\033[H\033[2J");
- #endif
- #ifdef _WIN32
- system("cls");
- #endif
- }
- inline void ConsoleHide() {
- #ifdef _WIN32
- FreeConsole();
- #endif
- }
- /**** program ****/
- inline void ProgramSleep(int sec) {
- #ifdef _WIN32
- Sleep(sec * 1000);
- #endif
- #ifdef __linux
- sleep(sec);
- #endif
- }
- inline void ProgramSleepMs(int ms) {
- #ifdef _WIN32
- Sleep(ms);
- #endif
- #ifdef __linux
- usleep(ms);
- #endif
- }
- inline void ProgramExit(int code) {
- exit(code);
- }
- int main() {
- }
Advertisement
Add Comment
Please, Sign In to add comment