Mercurial > repos > dawe > srf2fastq
comparison srf2fastq/io_lib-1.12.2/io_lib/strings.c @ 0:d901c9f41a6a default tip
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
author | dawe |
---|---|
date | Tue, 07 Jun 2011 17:48:05 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:d901c9f41a6a |
---|---|
1 /* | |
2 * Copyright (c) Medical Research Council 1994. All rights reserved. | |
3 * | |
4 * Permission to use, copy, modify and distribute this software and its | |
5 * documentation for any purpose is hereby granted without fee, provided that | |
6 * this copyright and notice appears in all copies. | |
7 * | |
8 * This file was written by James Bonfield, Simon Dear, Rodger Staden, | |
9 * as part of the Staden Package at the MRC Laboratory of Molecular | |
10 * Biology, Hills Road, Cambridge, CB2 2QH, United Kingdom. | |
11 * | |
12 * MRC disclaims all warranties with regard to this software. | |
13 */ | |
14 | |
15 #include "io_lib/misc.h" | |
16 #include <stdlib.h> | |
17 #include <ctype.h> | |
18 #include <string.h> | |
19 #include <sys/types.h> | |
20 | |
21 int fstrlen(char *f, int max_f) | |
22 { | |
23 for (; max_f > 0 && (isspace(f[max_f-1]) || f[max_f-1]=='\0'); max_f--); | |
24 return max_f; | |
25 } | |
26 | |
27 | |
28 | |
29 | |
30 | |
31 void f2cstr(char *f, int max_f, char *c, int max_c) | |
32 { | |
33 int i; | |
34 | |
35 i = min(fstrlen(f,max_f),max_c); | |
36 strncpy(c,f,i); | |
37 c[i]='\0'; | |
38 } | |
39 | |
40 | |
41 void c2fstr(char *c, int max_c, char *f, int max_f) | |
42 { | |
43 int i; | |
44 i = min((int)strlen(c),max_f); | |
45 strncpy(f,c,i); | |
46 for( ; i<max_f; i++) f[i]=' '; | |
47 | |
48 } | |
49 | |
50 | |
51 | |
52 | |
53 char *mystrtok(char *s, char *ct) | |
54 /* | |
55 ** When strtok isn't good enough | |
56 */ | |
57 { | |
58 char *this; | |
59 static char *look; | |
60 static int last; | |
61 | |
62 if (s == NULL) { | |
63 if (last) return NULL; | |
64 } else { | |
65 look = s; | |
66 last = 0; | |
67 } | |
68 this = look; | |
69 | |
70 for ( ; *look && strchr(ct,*look)==NULL; look++ ) ; | |
71 last = (! *look); | |
72 *look++ = '\0'; | |
73 | |
74 return this; | |
75 } | |
76 | |
77 | |
78 void str_tolower (char *s) | |
79 /* | |
80 ** Convert string to lower case | |
81 */ | |
82 { | |
83 if (!s) return; | |
84 for ( ; *s ; s++ ) | |
85 if (isupper(*s)) | |
86 *s = tolower(*s); | |
87 } | |
88 | |
89 void str_toupper (char *s) | |
90 /* | |
91 ** Convert string to upper case | |
92 */ | |
93 { | |
94 if (!s) return; | |
95 for ( ; *s ; s++ ) | |
96 if (islower(*s)) | |
97 *s = toupper(*s); | |
98 } | |
99 | |
100 #ifdef NOSTRSTR | |
101 /* | |
102 ** My routines for nice sun ones. | |
103 */ | |
104 char *strstr(char *cs, char *ct) | |
105 /* | |
106 ** ANSI C has the function strstr(). | |
107 ** | |
108 ** strstr() returns a pointer to the first occurrence of the | |
109 ** pattern string s2 in s1. For example, if s1 is "string | |
110 ** thing" and s2 is "ing", strstr() returns "ing thing". If s2 | |
111 ** does not occur in s1, strstr() returns NULL. | |
112 ** | |
113 ** It's not always implemented. Here's my cludge: | |
114 */ | |
115 { | |
116 int i; | |
117 int len_ct; | |
118 int end; | |
119 len_ct = strlen(ct); | |
120 end = strlen(cs) - len_ct; | |
121 for (i=0;i<=end;i++) | |
122 if (strncmp(&cs[i],ct,len_ct)==0) | |
123 return &cs[i]; | |
124 | |
125 return NULL; | |
126 } | |
127 #endif | |
128 | |
129 #ifdef NOSTRDUP | |
130 char *strdup(const char *str) | |
131 /* | |
132 ** SunOS has a nice strdup() function. | |
133 ** | |
134 ** strdup() returns a pointer to a new string which is a dupli- | |
135 ** cate of the string pointed to by s1. The space for the new | |
136 ** string is obtained using malloc(3V). If the new string can- | |
137 ** not be created, a NULL pointer is returned. | |
138 ** | |
139 ** Other ANSI C libraries don't have this. Here is my kludge: | |
140 */ | |
141 { | |
142 char *newstr; | |
143 int i = strlen(str); | |
144 | |
145 if ((newstr = (char *)malloc((unsigned int)(i+1))) == NULL) | |
146 return NULL; | |
147 | |
148 for (; i>=0; i--) | |
149 newstr[i] = str[i]; | |
150 | |
151 return newstr; | |
152 } | |
153 #endif | |
154 | |
155 #ifdef NOSTRCASECMP | |
156 int strcasecmp(const char *s1, const char *s2) { | |
157 while (tolower(*s1) == tolower(*s2)) { | |
158 /* If at the end of the string, then they're equal */ | |
159 if (0 == *s1) | |
160 return 0; | |
161 s1++; | |
162 s2++; | |
163 } | |
164 | |
165 /* One ended before the other, so return 1 or -1 */ | |
166 return (*(unsigned char *)s1) < (*(unsigned char *)s2) ? -1 : 1; | |
167 } | |
168 #endif |