A library for talking to Multi-Tech's Cellular SocketModem Devices.

Dependents:   M2X_dev axeda_wrapper_dev MTS_M2x_Example1 MTS_Cellular_Connect_Example ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MTSText.cpp Source File

MTSText.cpp

00001 /* Universal Socket Modem Interface Library
00002 * Copyright (c) 2013 Multi-Tech Systems
00003 *
00004 * Licensed under the Apache License, Version 2.0 (the "License");
00005 * you may not use this file except in compliance with the License.
00006 * You may obtain a copy of the License at
00007 *
00008 *     http://www.apache.org/licenses/LICENSE-2.0
00009 *
00010 * Unless required by applicable law or agreed to in writing, software
00011 * distributed under the License is distributed on an "AS IS" BASIS,
00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013 * See the License for the specific language governing permissions and
00014 * limitations under the License.
00015 */
00016 
00017 #include "MTSText.h"
00018 
00019 using namespace mts;
00020 
00021 std::string Text::getLine(const std::string& source, const size_t& start, size_t& cursor) {
00022     char delimiters[2];
00023     delimiters[0] = '\n';
00024     delimiters[1] = '\r';
00025     size_t end = source.find_first_of(delimiters, start, 2);
00026     std::string line(source.substr(start, end - start));
00027     if (end < source.size()) {
00028         if (end < source.size() - 1)
00029             if ((source[end] == '\n' && source[end + 1] == '\r') || (source[end] == '\r' && source[end + 1] == '\n')) {
00030                 //Advance an additional character in scenarios where lines end in \r\n or \n\r
00031                 end++;
00032             }
00033         end++;
00034     }
00035     cursor = end;
00036     return line;
00037 }
00038 
00039 std::vector<std::string> Text::split(const std::string& str, char delimiter, int limit) {
00040     return split(str, std::string(1, delimiter), limit);
00041 }
00042 
00043 std::vector<std::string> Text::split(const std::string& str, const std::string& delimiter, int limit) {
00044     std::vector<std::string> result;
00045     if(str.size() == 0) {
00046         return result;
00047     }
00048     size_t start = 0;
00049     size_t end = str.find(delimiter, start);
00050     for (int i = 1; i < limit || (limit <= 0 && (end != std::string::npos)); ++i) {
00051         result.push_back(str.substr(start, end - start));
00052         start = end + delimiter.length();
00053         end = str.find(delimiter, start);
00054     }
00055     result.push_back(str.substr(start));
00056     return result;
00057 }
00058