"Fossies" - the Fresh Open Source Software Archive

Member "fdupes-2.1.2/ncurses-print.c" (12 Aug 2020, 3948 Bytes) of package /linux/privat/fdupes-2.1.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "ncurses-print.c" see the Fossies "Dox" file reference documentation.

    1 /* FDUPES Copyright (c) 2018 Adrian Lopez
    2 
    3    Permission is hereby granted, free of charge, to any person
    4    obtaining a copy of this software and associated documentation files
    5    (the "Software"), to deal in the Software without restriction,
    6    including without limitation the rights to use, copy, modify, merge,
    7    publish, distribute, sublicense, and/or sell copies of the Software,
    8    and to permit persons to whom the Software is furnished to do so,
    9    subject to the following conditions:
   10 
   11    The above copyright notice and this permission notice shall be
   12    included in all copies or substantial portions of the Software.
   13 
   14    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 
   15    OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
   16    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
   17    IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 
   18    CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 
   19    TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
   20    SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
   21 
   22 #include "config.h"
   23 #include <stdlib.h>
   24 #include <wchar.h>
   25 #include "ncurses-print.h"
   26 #include "errormsg.h"
   27 #include "mbstowcs_escape_invalid.h"
   28 #include "positive_wcwidth.h"
   29 
   30 void putline(WINDOW *window, const char *str, const int line, const int columns, const int compensate_indent)
   31 {
   32   wchar_t *dest = 0;
   33   int inputlength;
   34   int linestart;
   35   int linelength;
   36   int linewidth;
   37   int first_line_columns;
   38   int l;
   39 
   40   inputlength = mbstowcs_escape_invalid(0, str, 0);
   41   if (inputlength == 0)
   42     return;
   43 
   44   dest = (wchar_t *) malloc((inputlength + 1) * sizeof(wchar_t));
   45   if (dest == NULL)
   46   {
   47     endwin();
   48     errormsg("out of memory\n");
   49     exit(1);
   50   }
   51 
   52   mbstowcs_escape_invalid(dest, str, inputlength);
   53   dest[inputlength] = L'\0';
   54 
   55   first_line_columns = columns - compensate_indent;
   56 
   57   linestart = 0;
   58 
   59   if (line > 0)
   60   {
   61     linewidth = positive_wcwidth(dest[linestart]);
   62 
   63     while (linestart + 1 < inputlength && linewidth + positive_wcwidth(dest[linestart + 1]) <= first_line_columns)
   64       linewidth += positive_wcwidth(dest[++linestart]);
   65 
   66     if (++linestart == inputlength)
   67       return;
   68 
   69     for (l = 1; l < line; ++l)
   70     {
   71       linewidth = positive_wcwidth(dest[linestart]);
   72 
   73       while (linestart + 1 < inputlength && linewidth + positive_wcwidth(dest[linestart + 1]) <= columns)
   74         linewidth += positive_wcwidth(dest[++linestart]);
   75 
   76       if (++linestart == inputlength)
   77         return;
   78     }
   79   }
   80 
   81   linewidth = positive_wcwidth(dest[linestart]);
   82   linelength = 1;
   83 
   84   if (line == 0)
   85   {
   86     while (linestart + linelength < inputlength && linewidth + positive_wcwidth(dest[linestart + linelength]) <= first_line_columns)
   87     {
   88       linewidth += positive_wcwidth(dest[linestart + linelength]);
   89       ++linelength;
   90     }
   91   }
   92   else
   93   {
   94     while (linestart + linelength < inputlength && linewidth + positive_wcwidth(dest[linestart + linelength]) <= columns)
   95     {
   96       linewidth += positive_wcwidth(dest[linestart + linelength]);
   97       ++linelength;
   98     }    
   99   }
  100 
  101   waddnwstr(window, dest + linestart, linelength);
  102 
  103   free(dest);
  104 }
  105 
  106 void print_spaces(WINDOW *window, int spaces)
  107 {
  108   int x;
  109 
  110   for (x = 0; x < spaces; ++x)
  111     waddch(window, L' ');
  112 }
  113 
  114 void print_right_justified_int(WINDOW *window, int number, int width)
  115 {
  116   int length;
  117 
  118   length = get_num_digits(number);
  119   if (number < 0)
  120     ++length;
  121 
  122   if (length < width)
  123     print_spaces(window, width - length);
  124 
  125   wprintw(window, "%d", number);
  126 }
  127 
  128 int vwprintflength(const wchar_t *format, va_list args)
  129 {
  130   FILE *fp;
  131   int size;
  132 
  133   fp = fopen("/dev/null", "w");
  134   if (fp == 0)
  135     return 0;
  136 
  137   size = vfwprintf(fp, format, args);
  138 
  139   fclose(fp);
  140 
  141   return size;
  142 }
  143 
  144 int get_num_digits(int value)
  145 {
  146   int digits = 0;
  147 
  148   if (value < 0)
  149     value = -value;
  150 
  151   do {
  152     value /= 10;
  153     ++digits;
  154   } while (value > 0);
  155 
  156   return digits;
  157 }