Main Page   Modules   Data Structures   File List   Data Fields   Globals  

tools.cpp

Go to the documentation of this file.
00001 /*
00002  * tools.cpp
00003  * Created on 25 Feb 2005
00004  * Authors:
00005  *    Wesley Leggette <wleggette@kaylix.net>
00006  * 
00007  * 
00008  * 
00009  * libdarc
00010  * 
00011  * 
00012  * This program is free software; you can redistribute it and/or modify
00013  * it under the terms of the GNU General Public License as published by
00014  * the Free Software Foundation; either version 2 of the License, or
00015  * (at your option) any later version.
00016  *
00017  * This program is distributed in the hope that it will be useful,
00018  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00019  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020  * GNU General Public License for more details.
00021  *
00022  * You should have received a copy of the GNU General Public License
00023  * along with this program; if not, write to the Free Software
00024  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00025  * 
00026  * $Id: tools_8cpp-source.html 708 2005-03-21 12:30:37Z leggwes $
00027  */
00028 
00035 #include "tools.h"
00036 #if LIBDARC_HAVE_LIBDAR_HPP
00037 #include <dar/libdar.hpp>
00038 #endif
00039 
00040 #include <string>
00041 
00042 #if HAVE_MALLOC_H
00043 #include <malloc.h>
00044 #endif
00045 
00046 NAMESPACE_LIBDAR_START
00047 
00048 
00049 
00050 extern "C" dar_infinint *dar_tools_get_extended_size(char* s, U_I base)
00051 {
00052     std::string size = s;
00053     infinint* tmp = new infinint();
00054     *tmp = tools_get_extended_size( size, base );
00055     return reinterpret_cast<dar_infinint*>(tmp);
00056 }
00057 
00058 //extern "C" dar_infinint *dar_tools_get_filesize(char* path)
00059 //{
00060 //  libdar::path p(path);
00061 //      printf("path = %s\n", p.display().c_str());
00062 //  infinint* ret;
00063 //  try
00064 //  {
00065 //      printf("check 1\n");
00066 //      ret = new infinint( tools_get_filesize( p ) );
00067 //      printf("check 2\n");
00068 //  } catch (...) {
00069 //      printf("caught error!\n");
00070 //      return NULL;
00071 //  }
00072 //  return (dar_infinint*)ret;
00073 //}
00074 
00075 
00076 
00077 extern "C" char* dar_tools_display_date( dar_infinint* date )
00078 {
00079     infinint* d = reinterpret_cast<infinint*>(date);
00080     std::string str = tools_display_date( *d );
00081     char* ret = (char*)malloc( str.size() + 1 );
00082     if (ret == NULL) { return NULL; }
00083     strcpy( ret, str.c_str() );
00084     return ret;
00085 }
00086 
00087 // size = size of string 's'
00088 char* dar_internal_string_return_overflow(char *s, U_I size)
00089 {
00090 //      printf("dar_internal_string_return_overflow called\n");
00091     char* ret = (char*)malloc(size + 1);
00092     if (ret == NULL) { return NULL; }
00093     strcpy( ret, s );
00094 //      printf("    s = '%s'\n", s);
00095 //      printf("    ret = '%s'\n", ret);
00096     return ret;
00097 }
00098 
00099 // size = size of string 's', fixed_size = target field size
00100 char* dar_internal_string_return_truncate(char *s, U_I size, U_I fixed_size)
00101 {
00102     char* ret = (char*)malloc(fixed_size + 1);
00103     if (ret == NULL) { return NULL; }
00104     strncpy( ret, s, fixed_size );
00105     ret[fixed_size] = '\0';
00106     return ret;
00107 }
00108 
00109 extern "C" char* dar_tools_string_justify_right(char* s, U_I min_size)
00110 {
00111     char* ret;
00112     U_I l = strlen(s);
00113     if ( l > min_size )
00114         return dar_internal_string_return_overflow(s, l);
00115     ret = (char*)malloc( min_size + 1 );
00116     if (ret == NULL) { return NULL; }
00117     unsigned int i;
00118     for ( i = 0; i < min_size - l; i++ )
00119         ret[i] = ' ';
00120     strcpy( ret + i, s );
00121     return ret;
00122 }
00123 
00124 extern "C" char* dar_tools_string_justify_center(char* s, U_I min_size)
00125 {
00126     char* ret;
00127     U_I l = strlen(s);
00128     if ( l > min_size )
00129         return dar_internal_string_return_overflow(s, l);
00130     ret = (char*)malloc( min_size + 1 );
00131     if (ret == NULL) { return NULL; }
00132     unsigned int i;
00133     for ( i = 0; i < (min_size - l)/2; i++ )
00134         ret[i] = ' ';
00135     strcpy( ret + i, s );
00136     for ( i += l; i < min_size; i++ )
00137         ret[i] = ' ';
00138     ret[min_size] = '\0';
00139     return ret;
00140 }
00141 
00142 extern "C" char* dar_tools_string_justify_left(char* s, U_I min_size)
00143 {
00144     char* ret;
00145     U_I l = strlen(s);
00146     if ( l > min_size )
00147         return dar_internal_string_return_overflow(s, l);
00148     ret = (char*)malloc( min_size + 1 );
00149     if (ret == NULL) { return NULL; }
00150     strcpy( ret, s );
00151     unsigned int i;
00152     for ( i = l; i < min_size; i++ )
00153         ret[i] = ' ';
00154     ret[min_size] = '\0';
00155     return ret;
00156 }
00157 
00158 
00159 extern "C" char* dar_tools_string_justify_right_truncate(char* s, U_I fixed_size)
00160 {
00161     char* ret;
00162     U_I l = strlen(s);
00163     if ( l > fixed_size )
00164         return dar_internal_string_return_truncate(s, l, fixed_size);
00165     ret = (char*)malloc( fixed_size + 1 );
00166     if (ret == NULL) { return NULL; }
00167     unsigned int i;
00168     for ( i = 0; i < fixed_size - l; i++ )
00169         ret[i] = ' ';
00170     strcpy( ret + i, s );
00171     return ret;
00172 }
00173 
00174 extern "C" char* dar_tools_string_justify_center_truncate(char* s, U_I fixed_size)
00175 {
00176     char* ret;
00177     U_I l = strlen(s);
00178     if ( l > fixed_size )
00179         return dar_internal_string_return_truncate(s, l, fixed_size);
00180     ret = (char*)malloc( fixed_size + 1 );
00181     if (ret == NULL) { return NULL; }
00182     unsigned int i;
00183     for ( i = 0; i < (fixed_size - l)/2; i++ )
00184         ret[i] = ' ';
00185     strcpy( ret + i, s );
00186     for ( i += l; i < fixed_size; i++ )
00187         ret[i] = ' ';
00188     ret[fixed_size] = '\0';
00189     return ret;
00190 }
00191 
00192 extern "C" char* dar_tools_string_justify_left_truncate(char* s, U_I fixed_size)
00193 {
00194     char* ret;
00195     U_I l = strlen(s);
00196     if ( l > fixed_size )
00197         return dar_internal_string_return_truncate(s, l, fixed_size);
00198     ret = (char*)malloc( fixed_size + 1 );
00199     if (ret == NULL) { return NULL; }
00200     strcpy( ret, s );
00201     unsigned int i;
00202     for ( i = l; i < fixed_size; i++ )
00203         ret[i] = ' ';
00204     ret[fixed_size] = '\0';
00205     return ret;
00206 }
00207 
00208 
00209 extern "C" char* dar_tools_name_of_uid( U_16 uid )
00210 {
00211     printf("name_of_uid function\n");
00212     // TODO: This function is not yet implemented.
00213 //  printf("uid check 1\n");
00214 //  std::string tmp = tools_name_of_uid(uid);
00215 //  printf("uid check 2\n");
00216 //  char* ret = (char*)malloc(tmp.length() + 1);
00217 //  printf("uid check 3\n");
00218 //  if (ret == NULL) { return NULL; }
00219 //  printf("uid check 4\n");
00220 //  strcpy( ret, tmp.c_str() );
00221 //  printf("uid check 5\n");
00222 //  return ret;
00223 
00224 }
00225 
00226 extern "C" char* dar_tools_name_of_gid( U_16 gid )
00227 {
00228     // TODO: This function is not yet implemented.
00229 //  printf("gid check 1\n");
00230 //  std::string tmp = tools_name_of_gid(gid);
00231 //  char* ret = (char*)malloc(tmp.length() + 1);
00232 //  if (ret == NULL) { return NULL; }
00233 //  strcpy( ret, tmp.c_str() );
00234 //  return ret;
00235 }
00236 
00237 extern "C" char* dar_tools_get_cwd( )
00238 {
00239     // TODO: This function is not yet implemented.
00240 //  printf("cwd check 1\n");
00241 //  std::string tmp = tools_getcwd();
00242 //  char* ret = (char*)malloc(tmp.length() + 1);
00243 //  if (ret == NULL) { return NULL; }
00244 //  strcpy( ret, tmp.c_str() );
00245 //  return ret;
00246 }
00247 
00248 
00249 
00250 
00251 
00252 
00253 NAMESPACE_LIBDAR_END
00254