1 /******************************************************************************
2 * Arachnoid Graphics Plugin for Mupen64Plus
3 * http://bitbucket.org/wahrhaft/mupen64plus-video-arachnoid/
5 * Copyright (C) 2007 Kristofer Karlsson, Rickard Niklasson
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.
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.
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 *****************************************************************************/
23 What is the difference between '\n' and '\r\n'?
24 -----------------------------------------------
26 There are a few characters which can indicate a new line. The usual ones are these two:
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).
31 Different Operating Systems handle newlines in a different way. Here is a short list of the most common ones:
34 They expect a newline to be the combination of two characters, namely '\r\n' (or 13 followed by 10).
36 * Unix (and hence Linux as well)
37 Unix uses a single '\n' to indicate a new line.
40 Macs use a single '\r'.
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
51 FILE *fp = fopen("testfile.txt", "w");
52 fprintf(fp, "Hello World\n");
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.
64 #ifndef STRING_FUNCTIONS_H_
65 #define STRING_FUNCTIONS_H_
70 #include <algorithm> //std::transform
72 namespace StringFunctions
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 ");
79 void trim(std::string& str, bool left=true, bool right=true, const std::string delims=" \t\r\n");
82 char* trim(char* str, bool left=true, bool right=true);