| 1 | /****************************************************************************** |
| 2 | * Arachnoid Graphics Plugin for Mupen64Plus |
| 3 | * http://bitbucket.org/wahrhaft/mupen64plus-video-arachnoid/ |
| 4 | * |
| 5 | * Copyright (C) 2007 Kristofer Karlsson, Rickard Niklasson |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License |
| 9 | * as published by the Free Software Foundation; either version 2 |
| 10 | * of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 20 | *****************************************************************************/ |
| 21 | |
| 22 | /* |
| 23 | What is the difference between '\n' and '\r\n'? |
| 24 | ----------------------------------------------- |
| 25 | |
| 26 | There are a few characters which can indicate a new line. The usual ones are these two: |
| 27 | |
| 28 | * '\n' or '0x0A' (10 in decimal) -> This character is called "Line Feed" (LF). |
| 29 | * '\r' or '0x0D' (13 in decimal) -> This one is called "Carriage return" (CR). |
| 30 | |
| 31 | Different Operating Systems handle newlines in a different way. Here is a short list of the most common ones: |
| 32 | |
| 33 | * DOS and Windows |
| 34 | They expect a newline to be the combination of two characters, namely '\r\n' (or 13 followed by 10). |
| 35 | |
| 36 | * Unix (and hence Linux as well) |
| 37 | Unix uses a single '\n' to indicate a new line. |
| 38 | |
| 39 | * Mac |
| 40 | Macs use a single '\r'. |
| 41 | |
| 42 | To unify things a bit, so that writing portable C/C++ programs is possible, file streams have both a |
| 43 | "translated" and an "untranslated" mode. If you open a file in translated mode, the runtime library |
| 44 | will convert a '\n' to the appropriate newline character(s). If the following program is compiled under |
| 45 | Unix, the file will contain a single LF to indicate the newline. If it's compiled under windows, it will |
| 46 | contain a CRLF. |
| 47 | |
| 48 | #include <stdio.h> |
| 49 | #include <stdlib.h> |
| 50 | int main() { |
| 51 | FILE *fp = fopen("testfile.txt", "w"); |
| 52 | fprintf(fp, "Hello World\n"); |
| 53 | fclose(fp); |
| 54 | return 0; |
| 55 | } |
| 56 | |
| 57 | Important |
| 58 | If you want to be able to read text files written on different operating systems, you have to open the file |
| 59 | in binary (= untranslated) mode and check for the different newlines yourself. |
| 60 | |
| 61 | */ |
| 62 | |
| 63 | |
| 64 | #ifndef STRING_FUNCTIONS_H_ |
| 65 | #define STRING_FUNCTIONS_H_ |
| 66 | |
| 67 | #include <string> |
| 68 | #include <vector> |
| 69 | #include <ctype.h> |
| 70 | #include <algorithm> //std::transform |
| 71 | |
| 72 | namespace StringFunctions |
| 73 | { |
| 74 | //Split |
| 75 | std::vector<std::string> split(const std::string& str, const std::string& delims="\n\t ", size_t maxSplits=std::string::npos); |
| 76 | std::vector<std::string> split(const char* str, const std::string& delims="\n\t "); |
| 77 | |
| 78 | //Trim |
| 79 | void trim(std::string& str, bool left=true, bool right=true, const std::string delims=" \t\r\n"); |
| 80 | |
| 81 | //Trim |
| 82 | char* trim(char* str, bool left=true, bool right=true); |
| 83 | } |
| 84 | |
| 85 | #endif |