00001 /* 00002 * This file is part of testrunner-lite 00003 * 00004 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). 00005 * 00006 * Contact: Raimo Gratseff <ext-raimo.gratseff@nokia.com> 00007 * 00008 * This program is free software; you can redistribute it and/or 00009 * modify it under the terms of the GNU Lesser General Public License 00010 * version 2.1 as published by the Free Software Foundation. 00011 * 00012 * This program is distributed in the hope that it will be useful, but 00013 * WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with this program; if not, write to the Free Software 00019 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 00020 * 02110-1301 USA 00021 * 00022 */ 00023 00024 /* ------------------------------------------------------------------------- */ 00025 /* INCLUDE FILES */ 00026 #include <stdio.h> 00027 #include <stdlib.h> 00028 #include <errno.h> 00029 #include <string.h> 00030 00031 /* ------------------------------------------------------------------------- */ 00032 /* EXTERNAL DATA STRUCTURES */ 00033 /* None */ 00034 00035 /* ------------------------------------------------------------------------- */ 00036 /* EXTERNAL GLOBAL VARIABLES */ 00037 /* None */ 00038 00039 /* ------------------------------------------------------------------------- */ 00040 /* EXTERNAL FUNCTION PROTOTYPES */ 00041 /* None */ 00042 00043 /* ------------------------------------------------------------------------- */ 00044 /* GLOBAL VARIABLES */ 00045 /* None */ 00046 00047 /* ------------------------------------------------------------------------- */ 00048 /* CONSTANTS */ 00049 /* None */ 00050 00051 /* ------------------------------------------------------------------------- */ 00052 /* MACROS */ 00053 /* None */ 00054 00055 /* ------------------------------------------------------------------------- */ 00056 /* LOCAL GLOBAL VARIABLES */ 00057 /* None */ 00058 00059 /* ------------------------------------------------------------------------- */ 00060 /* LOCAL CONSTANTS AND MACROS */ 00061 00062 /* Match these to log.h log_message_types */ 00063 /* None */ 00064 00065 /* ------------------------------------------------------------------------- */ 00066 /* MODULE DATA STRUCTURES */ 00067 /* None */ 00068 00069 00070 /* ------------------------------------------------------------------------- */ 00071 /* LOCAL FUNCTION PROTOTYPES */ 00072 /* ------------------------------------------------------------------------- */ 00073 /* None */ 00074 00075 /* FORWARD DECLARATIONS */ 00076 /* None */ 00077 00078 /* ------------------------------------------------------------------------- */ 00079 /* ==================== LOCAL FUNCTIONS ==================================== */ 00080 /* ------------------------------------------------------------------------- */ 00081 /* None */ 00082 00083 /* ------------------------------------------------------------------------- */ 00084 /* ======================== FUNCTIONS ====================================== */ 00085 /* ------------------------------------------------------------------------- */ 00095 unsigned int trim_string (char *ins, char *outs) 00096 { 00097 unsigned int ins_i = 0; 00098 unsigned int ins_end = 0; 00099 unsigned int outs_i = 0; 00100 00101 /* make sure input and output strings exist */ 00102 if (ins == 0 || outs == 0) { 00103 return 0; 00104 } 00105 00106 ins_end = strlen(ins); 00107 00108 /* test if the string is empty */ 00109 if (ins_end == 0) { 00110 return 0; 00111 } 00112 00113 /* find the first non-whitespace character */ 00114 while (1) { 00115 if (ins_i >= ins_end) 00116 break; 00117 if (isspace(ins[ins_i])) 00118 ins_i += 1; 00119 else 00120 break; 00121 } 00122 00123 /* find the last non-whitespace character */ 00124 while (1) { 00125 if (ins_end <= ins_i) 00126 break; 00127 if (isspace(ins[ins_end - 1])) 00128 ins_end -= 1; 00129 else 00130 break; 00131 } 00132 00133 /* Copy trimmed string to output */ 00134 while (ins_i < ins_end) { 00135 /* check and skip control characters */ 00136 if (!iscntrl(ins[ins_i])) { 00137 outs[outs_i] = ins[ins_i]; 00138 outs_i += 1; 00139 } 00140 ins_i += 1; 00141 } 00142 /* add null termination */ 00143 outs[outs_i] = 0; 00144 00145 return outs_i; 00146 } 00147 00148 00149 /* ================= OTHER EXPORTED FUNCTIONS ============================== */ 00150 /* None */ 00151 00152 /* ------------------------------------------------------------------------- */ 00153 /* End of file */ 00154