utils.c 4.1 KB
Newer Older
Chris Allegretta's avatar
Chris Allegretta committed
1
/* $Id$ */
Chris Allegretta's avatar
Chris Allegretta committed
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**************************************************************************
 *   utils.c                                                              *
 *                                                                        *
 *   Copyright (C) 1999 Chris Allegretta                                  *
 *   This program is free software; you can redistribute it and/or modify *
 *   it under the terms of the GNU General Public License as published by *
 *   the Free Software Foundation; either version 1, or (at your option)  *
 *   any later version.                                                   *
 *                                                                        *
 *   This program is distributed in the hope that it will be useful,      *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of       *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
 *   GNU General Public License for more details.                         *
 *                                                                        *
 *   You should have received a copy of the GNU General Public License    *
 *   along with this program; if not, write to the Free Software          *
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.            *
 *                                                                        *
 **************************************************************************/

22
#include <unistd.h>
Chris Allegretta's avatar
Chris Allegretta committed
23
24
25
26
27
28
29
30
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#include "config.h"
#include "nano.h"
#include "proto.h"

Chris Allegretta's avatar
Chris Allegretta committed
31
#ifndef NANO_SMALL
Chris Allegretta's avatar
Chris Allegretta committed
32
33
34
35
36
37
#include <libintl.h>
#define _(string) gettext(string)
#else
#define _(string) (string)
#endif

Chris Allegretta's avatar
Chris Allegretta committed
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/* Lower case a string - must be null terminated */
void lowercase(char *src)
{
    long i = 0;

    while (src[i] != 0) {
	src[i] = (char) tolower(src[i]);
	i++;
    }
}


/* I can't believe I have to write this function */
char *strcasestr(char *haystack, char *needle)
{
    char *localneedle, *localhaystack, *found, *tmp, *tmp2;

    /* Make a copy of the search string and searcgh space */
    localneedle = nmalloc(strlen(needle) + 2);
    localhaystack = nmalloc(strlen(haystack) + 2);

    strcpy(localneedle, needle);
    strcpy(localhaystack, haystack);

    /* Make them lowercase */
    lowercase(localneedle);
    lowercase(localhaystack);

    /* Look for the lowercased substring in the lowercased search space -
       return NULL if we didn't find anything */
    if ((found = strstr(localhaystack, localneedle)) == NULL) {
	free(localneedle);
	free(localhaystack);
	return NULL;
    }
    /* Else return the pointer to the same place in the real search space */
    tmp2 = haystack;
    for (tmp = localhaystack; tmp != found; tmp++)
	tmp2++;

    free(localneedle);
    free(localhaystack);
    return tmp2;
}

char *strstrwrapper(char *haystack, char *needle)
{
85
#ifdef HAVE_REGEX_H
86
    if (ISSET(USE_REGEXP)) {
87
88
89
90
91
	int result = regexec(&search_regexp, haystack, 10, regmatches, 0);
	if (!result)
	    return haystack + regmatches[0].rm_so;
	return 0;
    }
92
#endif
Chris Allegretta's avatar
Chris Allegretta committed
93
94
95
96
97
    if (ISSET(CASE_SENSITIVE))
	return strstr(haystack, needle);
    else
	return strcasestr(haystack, needle);
}
Chris Allegretta's avatar
Chris Allegretta committed
98
99
100

/* Thanks BG, many ppl have been asking for this... */
void *nmalloc(size_t howmuch)
Chris Allegretta's avatar
Chris Allegretta committed
101
{
Chris Allegretta's avatar
Chris Allegretta committed
102
    void *r;
Chris Allegretta's avatar
Chris Allegretta committed
103

Chris Allegretta's avatar
Chris Allegretta committed
104
105
106
107
108
109
110
    /* Panic save? */

    if (!(r = malloc(howmuch)))
	die(_("nano: malloc: out of memory!"));

    return r;
}
Chris Allegretta's avatar
Chris Allegretta committed
111

Chris Allegretta's avatar
Chris Allegretta committed
112
void *nrealloc(void *ptr, size_t howmuch)
Chris Allegretta's avatar
Chris Allegretta committed
113
{
Chris Allegretta's avatar
Chris Allegretta committed
114
    void *r;
Chris Allegretta's avatar
Chris Allegretta committed
115

Chris Allegretta's avatar
Chris Allegretta committed
116
    if (!(r = realloc(ptr, howmuch)))
Jordi Mallach's avatar
   
Jordi Mallach committed
117
	die(_("nano: realloc: out of memory!"));
Chris Allegretta's avatar
Chris Allegretta committed
118
119
120

    return r;
}
Robert Siemborski's avatar
Robert Siemborski committed
121

122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/* Copy one malloced string to another pointer.

   Should be used as dest = mallocstrcpy(dest, src);
*/
void *mallocstrcpy(void *dest, void *src)
{

    if (dest != NULL)
	free(dest);

    dest = nmalloc(strlen(src) + 1);
    strcpy(dest, src);

    return dest;
}


Robert Siemborski's avatar
Robert Siemborski committed
139
/* Append a new magic-line to filebot */
140
141
void new_magicline(void)
{
Robert Siemborski's avatar
Robert Siemborski committed
142
143
144
145
146
147
148
149
150
    filebot->next = nmalloc(sizeof(filestruct));
    filebot->next->data = nmalloc(1);
    filebot->next->data[0] = '\0';
    filebot->next->prev = filebot;
    filebot->next->next = NULL;
    filebot->next->lineno = filebot->lineno + 1;
    filebot = filebot->next;
    totlines++;
}