inlib  1.2.0
/Users/barrand/private/dev/softinex/old/inexlib-1.2/inlib/inlib/sjust
Go to the documentation of this file.
00001 // Copyright (C) 2010, Guy Barrand. All rights reserved.
00002 // See the file inlib.license for terms.
00003 
00004 #ifndef inlib_sjust
00005 #define inlib_sjust
00006 
00007 #include "strip"
00008 #include "sto"
00009 
00010 namespace inlib {
00011 
00012 enum side { left, right, middle };
00013 
00014 inline bool justify(std::string& a_string,
00015                     unsigned int a_size,
00016                     side a_side = left){
00017   // a_size is the final string length.
00018   strip(a_string);
00019   if(a_size<=a_string.size()) {
00020     a_string.resize(a_size);
00021     return false;
00022   } else {
00023     if(a_side==left) {
00024       a_string = a_string + std::string(a_size-a_string.size(),' ');
00025     } else if(a_side==right) {
00026       a_string = std::string(a_size-a_string.size(),' ') + a_string;
00027     } else if(a_side==middle) {
00028       int l = a_size - a_string.size();
00029       int h = l/2;
00030       if(h*2==l) { //even number of spaces :
00031         a_string = std::string(h,' ') + a_string + std::string(h,' ');
00032       } else { // odd number of spaces :
00033         a_string = std::string(h,' ') + a_string + std::string(h+1,' ');
00034       }
00035     }
00036     return false;
00037   }
00038 }
00039 
00040 inline std::string sjust(const std::string& a_string,
00041                          unsigned int a_size,
00042                          side a_side = left){
00043   std::string s(a_string); 
00044   justify(s,a_size,a_side);
00045   return s;
00046 }
00047 
00048 template <class T>
00049 inline std::string sjust(T a_value,unsigned int a_size,side a_side = left) {
00050   std::string s = to<T>(a_value);
00051   justify(s,a_size,a_side);
00052   return s;
00053 }
00054 
00055 }
00056 
00057 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines