Debian Codegraph @

src/ffutil.c

raw
1 /*
2 * Ffindex
3 * written by Andreas Hauser <[email protected]>.
4 * Please add your name here if you distribute modified versions.
5 *
6 * Ffindex is provided under the Create Commons license "Attribution-ShareAlike
7 * 4.0", which basically captures the spirit of the Gnu Public License (GPL).
8 *
9 * See:
10 * http://creativecommons.org/licenses/by-sa/4.0/
11 *
12 * Ffindex is a very simple database for small files. The files are stored
13 * concatenated in one big data file, seperated by '\0'. A second file
14 * contains a plain text index, giving name, offset and length of of the small
15 * files.
16 */
17
18 /* In addition ffutil.h and ffutil.c is licensed under MIT
19 *
20 * Permission is hereby granted, free of charge, to any person obtaining
21 * a copy of this software and associated documentation files (the
22 * "Software"), to deal in the Software without restriction, including
23 * without limitation the rights to use, copy, modify, merge, publish,
24 * distribute, sublicense, and/or sell copies of the Software, and to
25 * permit persons to whom the Software is furnished to do so, subject to
26 * the following conditions:
27 *
28 * The above copyright notice and this permission notice shall be
29 * included in all copies or substantial portions of the Software.
30 *
31 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
34 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
35 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
36 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
37 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38 */
39
40 #include <stdio.h>
41 #include "ffutil.h"
42
43 int fferror_print(char *sourcecode_filename, int line, const char *function_name, const char *message)
44 {
45 int myerrno = errno;
46 char* errstr = strerror(myerrno);
47 fprintf(stderr, "%s:%d %s: %s: %s\n", sourcecode_filename , line, function_name, message, errstr);
48 return myerrno;
49 }
50
51
52 /* remove \n, assumes UNIX line endings! */
53 char* ffnchomp(char *s, size_t len)
54 {
55 if(len >= 1 && s[--len] == '\n')
56 s[len] = '\0';
57
58 return s;
59 }
60
61 /* replacement for strtol streamlined for speed */
62 unsigned long ffparse_ulong(const char *s, const char** end)
63 {
64 const unsigned int base = 10;
65 unsigned long l = 0UL;
66 unsigned char *c = (unsigned char*)s;
67 for(unsigned int digit = *c-'0'; *c && digit < base; digit = *++c-'0')
68 l = (l * base) + digit;
69 *end = (char *)c;
70 return l;
71 }
72
73
74 int ffstrncmp(const char* s1, const char* s2, size_t len)
75 {
76 for(size_t i = len; i > 0 && *s1 && *s2 && *s1 == *s2; i--, s1++, s2++)
77 ;
78 return *s1 - *s2;
79 }
80
81
82 size_t fffprint_ulong(FILE* file, unsigned long l)
83 {
84 char p[21];
85 char *c = p + 20;
86 while(l >= 10)
87 {
88 *c-- = '0' + (l % 10);
89 l /= 10;
90 }
91 *c = '0' + l;
92 return fwrite(c, 1, p + 21 - c, file);
93 }
94
95
96 size_t ffcount_lines(const char *filename)
97 {
98 FILE *fp = fopen(filename, "r");
99 if (fp == NULL) { return 0; }
100
101 size_t lines = 0;
102 int ch = 0;
103
104 do {
105 ch = fgetc(fp);
106 if (ch == '\n')
107 lines++;
108 } while(ch != EOF);
109
110 if(ch != '\n' && lines != 0)
111 lines++;
112
113 fclose(fp);
114 return lines;
115 }
116
117 /* vim: ts=2 sw=2 et
118 */