util.c

00001 /*
00002  * Copyright (c) 2010 Lenka Trochtova
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  *
00009  * - Redistributions of source code must retain the above copyright
00010  *   notice, this list of conditions and the following disclaimer.
00011  * - Redistributions in binary form must reproduce the above copyright
00012  *   notice, this list of conditions and the following disclaimer in the
00013  *   documentation and/or other materials provided with the distribution.
00014  * - The name of the author may not be used to endorse or promote products
00015  *   derived from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00018  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00019  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00020  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00021  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00022  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00023  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00024  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00026  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027  */
00028 
00033 #include <stdlib.h>
00034 #include <str.h>
00035 
00036 #include "util.h"
00037 
00038 char *get_abs_path(const char *base_path, const char *name, const char *ext)
00039 {
00040         char *res;
00041         int base_len = str_size(base_path);
00042         int size = base_len + 2 * str_size(name) + str_size(ext) + 3;
00043         
00044         res = malloc(size);
00045         
00046         if (res) {
00047                 str_cpy(res, size, base_path);
00048                 if (base_path[base_len - 1] != '/')
00049                         str_append(res, size, "/");
00050                 str_append(res, size, name);
00051                 str_append(res, size, "/");
00052                 str_append(res, size, name);
00053                 if (ext[0] != '.')
00054                         str_append(res, size, ".");
00055                 str_append(res, size, ext);
00056         }
00057         
00058         return res;
00059 }
00060 
00061 char *get_path_elem_end(char *path)
00062 {
00063         while (*path != '\0' && *path != '/')
00064                 path++;
00065         return path;
00066 }
00067 
00068 bool skip_spaces(char **buf)
00069 {
00070         while (isspace(**buf))
00071                 (*buf)++;
00072         return *buf != 0;
00073 }
00074 
00075 size_t get_nonspace_len(const char *str)
00076 {
00077         size_t len = 0;
00078         
00079         while(*str != '\0' && !isspace(*str)) {
00080                 len++;
00081                 str++;
00082         }
00083 
00084         return len;
00085 }
00086 
00087 void free_not_null(const void *ptr)
00088 {
00089         if (ptr != NULL)
00090                 free(ptr);
00091 }
00092 
00093 char *clone_string(const char *s)
00094 {
00095         size_t size = str_size(s) + 1;
00096         char *str;
00097         
00098         str = (char *) malloc(size);
00099         if (str != NULL)
00100                 str_cpy(str, size, s);
00101         return str;
00102 }
00103 
00104 void replace_char(char *str, char orig, char repl)
00105 {
00106         while (*str) {
00107                 if (*str == orig)
00108                         *str = repl;
00109                 str++;
00110         }
00111 }
00112 
00113 ssize_t safe_read(int fd, void *buffer, size_t size)
00114 {
00115         if (size == 0) {
00116                 return 0;
00117         }
00118 
00119         uint8_t *buf_ptr = (uint8_t *) buffer;
00120 
00121         size_t total_read = 0;
00122         while (total_read < size) {
00123                 ssize_t bytes_read = read(fd, buf_ptr, size - total_read);
00124                 if (bytes_read < 0) {
00125                         /* Error. */
00126                         return bytes_read;
00127                 } else if (bytes_read == 0) {
00128                         /* Possibly end of file. */
00129                         break;
00130                 } else {
00131                         /* Read at least something. */
00132                         buf_ptr += bytes_read;
00133                         total_read += bytes_read;
00134                 }
00135         }
00136 
00137         return (ssize_t) total_read;
00138 }
00139 

Generated on Thu Jun 2 07:45:41 2011 for HelenOS/USB by  doxygen 1.4.7