Branch data Line data Source code
1 : : /* 2 : : * gnome-keyring 3 : : * 4 : : * Copyright (C) 2011 Collabora Ltd 5 : : * 6 : : * This program is free software; you can redistribute it and/or modify 7 : : * it under the terms of the GNU Lesser General Public License as 8 : : * published by the Free Software Foundation; either version 2.1 of 9 : : * the License, or (at your option) any later version. 10 : : * 11 : : * This program is distributed in the hope that it will be useful, but 12 : : * WITHOUT ANY WARRANTY; without even the implied warranty of 13 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 : : * Lesser General Public License for more details. 15 : : * 16 : : * You should have received a copy of the GNU Lesser General Public 17 : : * License along with this program; if not, see <http://www.gnu.org/licenses/>. 18 : : * 19 : : * Author: Stef Walter <stefw@collabora.co.uk> 20 : : */ 21 : : 22 : : #include "config.h" 23 : : 24 : : #include "gcr-util.h" 25 : : 26 : : #include <string.h> 27 : : 28 : : /** 29 : : * _gcr_util_parse_lines: 30 : : * @string: The string to parse lines from, will be modified 31 : : * @last_line: Whether or not we should run for last line or not 32 : : * @callback: Call for each line 33 : : * @user_data: Data for callback 34 : : * 35 : : * Calls callback for each line. If last_line, also sends the remainder 36 : : * data that comes after the last line break. \n and \r\n are line separators. 37 : : * Neither are included in data passed to callback. 38 : : */ 39 : : void 40 : 9 : _gcr_util_parse_lines (GString *string, gboolean last_line, 41 : : GcrLineCallback callback, gpointer user_data) 42 : : { 43 : : gchar *ptr; 44 : : gchar *prev; 45 : : 46 [ - + ]: 9 : g_return_if_fail (string); 47 [ - + ]: 9 : g_return_if_fail (callback); 48 : : 49 : : /* Print all stderr lines as messages */ 50 [ + + ]: 24 : while ((ptr = strchr (string->str, '\n')) != NULL) { 51 : 15 : *ptr = '\0'; 52 [ + + ]: 15 : if (ptr != string->str) { 53 : 13 : prev = ptr - 1; 54 [ + + ]: 13 : if (*prev == '\r') 55 : 3 : *prev = '\0'; 56 : : } 57 : : 58 : 15 : (callback) (string->str, user_data); 59 : 15 : g_string_erase (string, 0, ptr - string->str + 1); 60 : : } 61 : : 62 [ + + + - ]: 9 : if (last_line && string->len) { 63 : 2 : (callback) (string->str, user_data); 64 : 2 : g_string_erase (string, 0, string->len); 65 : : } 66 : : }