1 /*
2  *  utils.c - this file is part of "codenavigation", which is
3  *  part of the "geany-plugins" project.
4  *
5  *  Copyright 2009 Lionel Fuentes <funto66(at)gmail(dot)com>
6  *  Copyright 2014 Federico Reghenzani <federico(dot)dev(at)reghe(dot)net>
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #include "utils.h"
23 
24 #include <geanyplugin.h>
25 
26  /**
27  * @brief	Function which returns the extension of the file path which
28  * 			is given, or NULL if it did not found any extension.
29  * @param 	gchar*	the file path
30  * @return	gchar*	newly-allocated string containing the extension
31  *
32  */
33 gchar*
get_extension(gchar * path)34 get_extension(gchar* path)
35 {
36 	gchar* extension = NULL;
37 	gchar* pc = path;
38 	while(*pc != '\0')
39 	{
40 		if(*pc == '.')
41 			extension = pc+1;
42 		pc++;
43 	}
44 
45 	if(extension == NULL || (*extension) == '\0')
46 		return NULL;
47 	else
48 		return g_strdup(extension);
49 }
50 
51 /**
52  * @brief	Copy a path and remove the extension
53  * @param 	gchar*	the file path
54  * @return	gchar*	newly-allocated string containing the filename without ext
55  *
56  */
57 gchar*
copy_and_remove_extension(gchar * path)58 copy_and_remove_extension(gchar* path)
59 {
60 	gchar* str = NULL;
61 	gchar* pc = NULL;
62 	gchar* dot_pos = NULL;
63 
64 	if(path == NULL || path[0] == '\0')
65 		return NULL;
66 
67 	str = g_strdup(path);
68 	pc = str;
69 	while(*pc != '\0')
70 	{
71 		if(*pc == '.')
72 		{
73 			dot_pos = pc;
74 			break;
75 		}
76 		pc++;
77 	}
78 
79 	if(dot_pos != NULL)
80 		*dot_pos = '\0';
81 
82 	return str;
83 }
84 
85 /**
86  * @brief	Comparison of strings, for use with g_slist_find_custom
87  * @param 	const gchar*, const gchar*
88  * @return	gint
89  *
90  */
91 gint
compare_strings(const gchar * a,const gchar * b)92 compare_strings(const gchar* a, const gchar* b)
93 {
94 	return (gint)(!utils_str_equal(a, b));
95 }
96 
97 /**
98  * @brief	A PHP-like reverse strpos implementation
99  * @param 	const gchar*	haystack
100  * @param   const gchar*	needle
101  * @return	gint	position or -1 if not found
102  *
103  */
104 gint
strrpos(const gchar * haystack,const gchar * needle)105 strrpos(const gchar *haystack, const gchar *needle)
106 {
107    char *p = g_strrstr_len(haystack, -1, needle);
108    if (p)
109 	  return p - haystack;
110    return -1;   // not found
111 }
112 
113