Пример #1
0
int cmd_mudstatus() {
    int utime=uptime(), mem=memory_info(), i=0, ct=0;
    object *o;

    if(!archp(this_player())) return 0;
    write("%^BLUE%^+=+=+=+( %^BOLD%^%^WHITE%^Mud Status%^RESET%^%^BLUE%^ )+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=%^RESET%^");

    write("    The mud has been online for "+query_time(uptime())+"");
    write("    The mud will reboot in "+query_time((EVENTS_D->query_next_reboot())-time())+"");
    write("    Number of Objects Loaded:  "+i=sizeof(o=objects()));
    write("    "+add_commas(mem)+" bytes of memory is loaded.");
    while(i--) if(query_heart_beat(o[i])) ct++;
    write("    Number of objects with heartbeats: "+ct+".");
    write("    Number of callouts: "+sizeof(call_out_info())+".");
    write("    Processing "+query_load_average()+".");
    if(find_object("/d/nopk/standard/freezer"))
        write("    Number of link-dead players: "+
              sizeof( all_inventory( find_object( "/d/nopk/standard/freezer" ) ) ) );
    write("    Number of players online: "+(string)sizeof(users()));
    write("%^BLUE%^+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+%^RESET%^\n");
    return 1;
}
Пример #2
0
Файл: sql.c Проект: debfx/bareos
/*
 * If full_list is set, we list vertically, otherwise, we
 *  list on one line horizontally.
 * Return number of rows
 */
int list_result(JCR *jcr, B_DB *mdb, OUTPUT_FORMATTER *send, e_list_type type)
{
   SQL_FIELD *field;
   SQL_ROW row;
   int i, col_len, max_len = 0;
   int num_fields;
   char ewc[30];
   POOL_MEM key;
   POOL_MEM value;

   Dmsg0(800, "list_result starts\n");
   if (sql_num_rows(mdb) == 0) {
      send->decoration(_("No results to list.\n"));
      send->object_end("table");
      return sql_num_rows(mdb);
   }

   num_fields = sql_num_fields(mdb);
   switch (type) {
   case NF_LIST:
   case RAW_LIST:
      /*
       * No need to calculate things like column widths for
       * unformated or raw output.
       */
      break;
   case HORZ_LIST:
   case VERT_LIST:
      Dmsg1(800, "list_result starts looking at %d fields\n", num_fields);
      /*
       * Determine column display widths
       */
      sql_field_seek(mdb, 0);
      for (i = 0; i < num_fields; i++) {
         Dmsg1(800, "list_result processing field %d\n", i);
         field = sql_fetch_field(mdb);
         if (!field) {
            break;
         }
         col_len = cstrlen(field->name);
         if (type == VERT_LIST) {
            if (col_len > max_len) {
               max_len = col_len;
            }
         } else {
            if (sql_field_is_numeric(mdb, field->type) && (int)field->max_length > 0) { /* fixup for commas */
               field->max_length += (field->max_length - 1) / 3;
            }
            if (col_len < (int)field->max_length) {
               col_len = field->max_length;
            }
            if (col_len < 4 && !sql_field_is_not_null(mdb, field->flags)) {
               col_len = 4;                 /* 4 = length of the word "NULL" */
            }
            field->max_length = col_len;    /* reset column info */
         }
      }
      break;
   }

   Dmsg0(800, "list_result finished first loop\n");

   switch (type) {
   case NF_LIST:
   case RAW_LIST:
      Dmsg1(800, "list_result starts second loop looking at %d fields\n", num_fields);
      while ((row = sql_fetch_row(mdb)) != NULL) {
         send->object_start(row[0]);
         sql_field_seek(mdb, 0);
         for (i = 0; i < num_fields; i++) {
            field = sql_fetch_field(mdb);
            if (!field) {
               break;
            }
            if (row[i] == NULL) {
               value.bsprintf(" %s", "NULL");
            } else {
               value.bsprintf(" %s", row[i]);
            }
            send->object_key_value(field->name, value.c_str(), "%s");
         }
         if (type != RAW_LIST) {
            send->decoration("\n");
         }
         send->object_end(row[0]);
      }
      break;
   case HORZ_LIST:
      Dmsg1(800, "list_result starts second loop looking at %d fields\n", num_fields);
      list_dashes(mdb, send);
      send->decoration("|");
      sql_field_seek(mdb, 0);
      for (i = 0; i < num_fields; i++) {
         Dmsg1(800, "list_result looking at field %d\n", i);
         field = sql_fetch_field(mdb);
         if (!field) {
            break;
         }
         max_len = max_length(field->max_length);
         send->decoration(" %-*s |", max_len, field->name);
      }
      send->decoration("\n");
      list_dashes(mdb, send);

      Dmsg1(800, "list_result starts third loop looking at %d fields\n", num_fields);
      while ((row = sql_fetch_row(mdb)) != NULL) {
         send->object_start(row[0]);
         sql_field_seek(mdb, 0);
         send->decoration("|");
         for (i = 0; i < num_fields; i++) {
            field = sql_fetch_field(mdb);
            if (!field) {
               break;
            }
            max_len = max_length(field->max_length);
            if (row[i] == NULL) {
               value.bsprintf(" %-*s |", max_len, "NULL");
            } else if (sql_field_is_numeric(mdb, field->type) && !jcr->gui && is_an_integer(row[i])) {
               value.bsprintf(" %*s |", max_len, add_commas(row[i], ewc));
            } else {
               value.bsprintf(" %-*s |", max_len, row[i]);
            }
            if (i == num_fields-1) {
               value.strcat("\n");
            }
            /* use value format string to send preformated value */
            send->object_key_value(field->name, row[i], value.c_str());
         }
         send->object_end(row[0]);
      }
      list_dashes(mdb, send);
      break;
   case VERT_LIST:
      Dmsg1(800, "list_result starts vertical list at %d fields\n", num_fields);
      while ((row = sql_fetch_row(mdb)) != NULL) {
         send->object_start(row[0]);
         sql_field_seek(mdb, 0);
         for (i = 0; i < num_fields; i++) {
            field = sql_fetch_field(mdb);
            if (!field) {
               break;
            }
            if (row[i] == NULL) {
               key.bsprintf(" %*s: ", max_len, field->name);
               value.bsprintf("%s\n", "NULL");
            } else if (sql_field_is_numeric(mdb, field->type) && !jcr->gui && is_an_integer(row[i])) {
               key.bsprintf(" %*s: ", max_len, field->name);
               value.bsprintf("%s\n", add_commas(row[i], ewc));
            } else {
               key.bsprintf(" %*s: ", max_len, field->name);
               value.bsprintf("%s\n", row[i]);
            }
            /* use value format string to send preformated value */
            send->object_key_value(field->name, key.c_str(), row[i], value.c_str());
         }
         send->decoration("\n");
         send->object_end(row[0]);
      }
      break;
   }
   return sql_num_rows(mdb);
}
Пример #3
0
Файл: sql.c Проект: debfx/bareos
int list_result(void *vctx, int nb_col, char **row)
{
   SQL_FIELD *field;
   int i, col_len, max_len = 0;
   int num_fields;
   char ewc[30];
   POOL_MEM key;
   POOL_MEM value;

   LIST_CTX *pctx = (LIST_CTX *)vctx;
   OUTPUT_FORMATTER *send = pctx->send;
   e_list_type type = pctx->type;
   B_DB *mdb = pctx->mdb;
   JCR *jcr = pctx->jcr;

   send->object_start("row");

   num_fields = sql_num_fields(mdb);
   switch (type) {
   case NF_LIST:
   case RAW_LIST:
      /*
       * No need to calculate things like maximum field lenght for
       * unformated or raw output.
       */
      break;
   case HORZ_LIST:
   case VERT_LIST:
      if (!pctx->once) {
         pctx->once = true;

         Dmsg1(800, "list_result starts looking at %d fields\n", num_fields);
         /*
          * Determine column display widths
          */
         sql_field_seek(mdb, 0);
         for (i = 0; i < num_fields; i++) {
            Dmsg1(800, "list_result processing field %d\n", i);
            field = sql_fetch_field(mdb);
            if (!field) {
               break;
            }
            col_len = cstrlen(field->name);
            if (type == VERT_LIST) {
               if (col_len > max_len) {
                  max_len = col_len;
               }
            } else {
               if (sql_field_is_numeric(mdb, field->type) && (int)field->max_length > 0) { /* fixup for commas */
                  field->max_length += (field->max_length - 1) / 3;
               }
               if (col_len < (int)field->max_length) {
                  col_len = field->max_length;
               }
               if (col_len < 4 && !sql_field_is_not_null(mdb, field->flags)) {
                  col_len = 4;                 /* 4 = length of the word "NULL" */
               }
               field->max_length = col_len;    /* reset column info */
            }
         }

         pctx->num_rows++;

         Dmsg0(800, "list_result finished first loop\n");
         if (type == VERT_LIST) {
            break;
         }

         Dmsg1(800, "list_result starts second loop looking at %d fields\n", num_fields);

         /*
          * Keep the result to display the same line at the end of the table
          */
         list_dashes(mdb, send);

         send->decoration("|");
         sql_field_seek(mdb, 0);
         for (i = 0; i < num_fields; i++) {
            Dmsg1(800, "list_result looking at field %d\n", i);
            field = sql_fetch_field(mdb);
            if (!field) {
               break;
            }
            max_len = max_length(field->max_length);
            send->decoration(" %-*s |", max_len, field->name);
         }
         send->decoration("\n");
         list_dashes(mdb, send);
      }
      break;
   default:
      break;
   }

   switch (type) {
   case NF_LIST:
   case RAW_LIST:
      Dmsg1(800, "list_result starts third loop looking at %d fields\n", num_fields);
      sql_field_seek(mdb, 0);
      for (i = 0; i < num_fields; i++) {
         field = sql_fetch_field(mdb);
         if (!field) {
            break;
         }
         if (row[i] == NULL) {
            value.bsprintf(" %s", "NULL");
         } else {
            value.bsprintf(" %s", row[i]);
         }
         send->object_key_value(field->name, value.c_str(), "%s");
      }
      if (type != RAW_LIST) {
         send->decoration("\n");
      }
      break;
   case HORZ_LIST:
      Dmsg1(800, "list_result starts third loop looking at %d fields\n", num_fields);
      sql_field_seek(mdb, 0);
      send->decoration("|");
      for (i = 0; i < num_fields; i++) {
         field = sql_fetch_field(mdb);
         if (!field) {
            break;
         }

         max_len = max_length(field->max_length);
         if (row[i] == NULL) {
            value.bsprintf(" %-*s |", max_len, "NULL");
         } else if (sql_field_is_numeric(mdb, field->type) && !jcr->gui && is_an_integer(row[i])) {
            value.bsprintf(" %*s |", max_len, add_commas(row[i], ewc));
         } else {
            value.bsprintf(" %-*s |", max_len, row[i]);
         }

         /*
          * Use value format string to send preformated value.
          */
         send->object_key_value(field->name, row[i], value.c_str());
      }
      send->decoration("\n");
      break;
   case VERT_LIST:
      Dmsg1(800, "list_result starts vertical list at %d fields\n", num_fields);
      sql_field_seek(mdb, 0);
      for (i = 0; i < num_fields; i++) {
         field = sql_fetch_field(mdb);
         if (!field) {
            break;
         }
         if (row[i] == NULL) {
            key.bsprintf(" %*s: ", max_len, field->name);
            value.bsprintf("%s\n", "NULL");
         } else if (sql_field_is_numeric(mdb, field->type) && !jcr->gui && is_an_integer(row[i])) {
            key.bsprintf(" %*s: ", max_len, field->name);
            value.bsprintf("%s\n", add_commas(row[i], ewc));
         } else {
            key.bsprintf(" %*s: ", max_len, field->name);
            value.bsprintf("%s\n", row[i]);
         }

         /*
          * Use value format string to send preformated value.
          */
         send->object_key_value(field->name, key.c_str(), row[i], value.c_str());
      }
      send->decoration("\n");
      break;
   default:
      break;
   }
   send->object_end("row");
   return 0;
}
Пример #4
0
Файл: edit.c Проект: AlD/bareos
/*
 * Edit an integer number with commas, the supplied buffer
 * must be at least 27 bytes long.  The incoming number
 * is always widened to 64 bits.
 */
char *edit_uint64_with_commas(uint64_t val, char *buf)
{
   edit_uint64(val, buf);
   return add_commas(buf, buf);
}
Пример #5
0
/*
 * If full_list is set, we list vertically, otherwise, we
 *  list on one line horizontally.
 * Return number of rows
 */
int list_result(JCR *jcr, B_DB *mdb, DB_LIST_HANDLER *send, void *ctx, e_list_type type)
{
   SQL_FIELD *field;
   SQL_ROW row;
   int i, col_len, max_len = 0;
   int num_fields;
   char buf[2000], ewc[30];

   Dmsg0(800, "list_result starts\n");
   if (sql_num_rows(mdb) == 0) {
      send(ctx, _("No results to list.\n"));
      return sql_num_rows(mdb);
   }

   num_fields = sql_num_fields(mdb);
   switch (type) {
   case NF_LIST:
   case RAW_LIST:
      /*
       * No need to calculate things like column widths for
       * unformated or raw output.
       */
      break;
   case HORZ_LIST:
   case VERT_LIST:
      Dmsg1(800, "list_result starts looking at %d fields\n", num_fields);
      /*
       * Determine column display widths
       */
      sql_field_seek(mdb, 0);
      for (i = 0; i < num_fields; i++) {
         Dmsg1(800, "list_result processing field %d\n", i);
         field = sql_fetch_field(mdb);
         if (!field) {
            break;
         }
         col_len = cstrlen(field->name);
         if (type == VERT_LIST) {
            if (col_len > max_len) {
               max_len = col_len;
            }
         } else {
            if (sql_field_is_numeric(mdb, field->type) && (int)field->max_length > 0) { /* fixup for commas */
               field->max_length += (field->max_length - 1) / 3;
            }
            if (col_len < (int)field->max_length) {
               col_len = field->max_length;
            }
            if (col_len < 4 && !sql_field_is_not_null(mdb, field->flags)) {
               col_len = 4;                 /* 4 = length of the word "NULL" */
            }
            field->max_length = col_len;    /* reset column info */
         }
      }
      break;
   }

   Dmsg0(800, "list_result finished first loop\n");

   switch (type) {
   case NF_LIST:
   case RAW_LIST:
      Dmsg1(800, "list_result starts second loop looking at %d fields\n", num_fields);
      while ((row = sql_fetch_row(mdb)) != NULL) {
         sql_field_seek(mdb, 0);
         for (i = 0; i < num_fields; i++) {
            field = sql_fetch_field(mdb);
            if (!field) {
               break;
            }
            if (row[i] == NULL) {
               bsnprintf(buf, sizeof(buf), " %s", "NULL");
            } else {
               bsnprintf(buf, sizeof(buf), " %s", row[i]);
            }
            send(ctx, buf);
         }
         if (type != RAW_LIST) {
            send(ctx, "\n");
         }
      }
      break;
   case HORZ_LIST:
      Dmsg1(800, "list_result starts second loop looking at %d fields\n", num_fields);
      list_dashes(mdb, send, ctx);
      send(ctx, "|");
      sql_field_seek(mdb, 0);
      for (i = 0; i < num_fields; i++) {
         Dmsg1(800, "list_result looking at field %d\n", i);
         field = sql_fetch_field(mdb);
         if (!field) {
            break;
         }
         max_len = max_length(field->max_length);
         bsnprintf(buf, sizeof(buf), " %-*s |", max_len, field->name);
         send(ctx, buf);
      }
      send(ctx, "\n");
      list_dashes(mdb, send, ctx);

      Dmsg1(800, "list_result starts third loop looking at %d fields\n", num_fields);
      while ((row = sql_fetch_row(mdb)) != NULL) {
         sql_field_seek(mdb, 0);
         send(ctx, "|");
         for (i = 0; i < num_fields; i++) {
            field = sql_fetch_field(mdb);
            if (!field) {
               break;
            }
            max_len = max_length(field->max_length);
            if (row[i] == NULL) {
               bsnprintf(buf, sizeof(buf), " %-*s |", max_len, "NULL");
            } else if (sql_field_is_numeric(mdb, field->type) && !jcr->gui && is_an_integer(row[i])) {
               bsnprintf(buf, sizeof(buf), " %*s |", max_len,
                         add_commas(row[i], ewc));
            } else {
               bsnprintf(buf, sizeof(buf), " %-*s |", max_len, row[i]);
            }
            send(ctx, buf);
         }
         send(ctx, "\n");
      }
      list_dashes(mdb, send, ctx);
      break;
   case VERT_LIST:
      Dmsg1(800, "list_result starts vertical list at %d fields\n", num_fields);
      while ((row = sql_fetch_row(mdb)) != NULL) {
         sql_field_seek(mdb, 0);
         for (i = 0; i < num_fields; i++) {
            field = sql_fetch_field(mdb);
            if (!field) {
               break;
            }
            if (row[i] == NULL) {
               bsnprintf(buf, sizeof(buf), " %*s: %s\n", max_len, field->name, "NULL");
            } else if (sql_field_is_numeric(mdb, field->type) && !jcr->gui && is_an_integer(row[i])) {
               bsnprintf(buf, sizeof(buf), " %*s: %s\n", max_len, field->name,
                   add_commas(row[i], ewc));
            } else {
               bsnprintf(buf, sizeof(buf), " %*s: %s\n", max_len, field->name, row[i]);
            }
            send(ctx, buf);
         }
         send(ctx, "\n");
      }
      break;
   }
   return sql_num_rows(mdb);
}
Пример #6
0
int list_result(void *vctx, int nb_col, char **row)
{
   SQL_FIELD *field;
   int i, col_len, max_len = 0;
   int num_fields;
   char buf[2000], ewc[30];

   LIST_CTX *pctx = (LIST_CTX *)vctx;
   DB_LIST_HANDLER *send = pctx->send;
   e_list_type type = pctx->type;
   B_DB *mdb = pctx->mdb;
   void *ctx = pctx->ctx;
   JCR *jcr = pctx->jcr;

   num_fields = sql_num_fields(mdb);
   switch (type) {
   case NF_LIST:
   case RAW_LIST:
      /*
       * No need to calculate things like maximum field lenght for
       * unformated or raw output.
       */
      break;
   case HORZ_LIST:
   case VERT_LIST:
      if (!pctx->once) {
         pctx->once = true;

         Dmsg1(800, "list_result starts looking at %d fields\n", num_fields);
         /*
          * Determine column display widths
          */
         sql_field_seek(mdb, 0);
         for (i = 0; i < num_fields; i++) {
            Dmsg1(800, "list_result processing field %d\n", i);
            field = sql_fetch_field(mdb);
            if (!field) {
               break;
            }
            col_len = cstrlen(field->name);
            if (type == VERT_LIST) {
               if (col_len > max_len) {
                  max_len = col_len;
               }
            } else {
               if (sql_field_is_numeric(mdb, field->type) && (int)field->max_length > 0) { /* fixup for commas */
                  field->max_length += (field->max_length - 1) / 3;
               }
               if (col_len < (int)field->max_length) {
                  col_len = field->max_length;
               }
               if (col_len < 4 && !sql_field_is_not_null(mdb, field->flags)) {
                  col_len = 4;                 /* 4 = length of the word "NULL" */
               }
               field->max_length = col_len;    /* reset column info */
            }
         }

         pctx->num_rows++;

         Dmsg0(800, "list_result finished first loop\n");
         if (type == VERT_LIST) {
            break;
         }

         Dmsg1(800, "list_result starts second loop looking at %d fields\n", num_fields);

         /*
          * Keep the result to display the same line at the end of the table
          */
         list_dashes(mdb, last_line_handler, pctx);
         send(ctx, pctx->line);

         send(ctx, "|");
         sql_field_seek(mdb, 0);
         for (i = 0; i < num_fields; i++) {
            Dmsg1(800, "list_result looking at field %d\n", i);
            field = sql_fetch_field(mdb);
            if (!field) {
               break;
            }
            max_len = max_length(field->max_length);
            bsnprintf(buf, sizeof(buf), " %-*s |", max_len, field->name);
            send(ctx, buf);
         }
         send(ctx, "\n");
         list_dashes(mdb, send, ctx);
      }
      break;
   default:
      break;
   }

   switch (type) {
   case NF_LIST:
   case RAW_LIST:
      Dmsg1(800, "list_result starts third loop looking at %d fields\n", num_fields);
      sql_field_seek(mdb, 0);
      for (i = 0; i < num_fields; i++) {
         field = sql_fetch_field(mdb);
         if (!field) {
            break;
         }
         if (row[i] == NULL) {
            bsnprintf(buf, sizeof(buf), " %s", "NULL");
         } else {
            bsnprintf(buf, sizeof(buf), " %s", row[i]);
         }
         send(ctx, buf);
      }
      if (type != RAW_LIST) {
         send(ctx, "\n");
      }
      break;
   case HORZ_LIST:
      Dmsg1(800, "list_result starts third loop looking at %d fields\n", num_fields);
      sql_field_seek(mdb, 0);
      send(ctx, "|");
      for (i = 0; i < num_fields; i++) {
         field = sql_fetch_field(mdb);
         if (!field) {
            break;
         }
         max_len = max_length(field->max_length);
         if (row[i] == NULL) {
            bsnprintf(buf, sizeof(buf), " %-*s |", max_len, "NULL");
         } else if (sql_field_is_numeric(mdb, field->type) && !jcr->gui && is_an_integer(row[i])) {
            bsnprintf(buf, sizeof(buf), " %*s |", max_len,
                      add_commas(row[i], ewc));
         } else {
            bsnprintf(buf, sizeof(buf), " %-*s |", max_len, row[i]);
         }
         send(ctx, buf);
      }
      send(ctx, "\n");
      break;
   case VERT_LIST:
      Dmsg1(800, "list_result starts vertical list at %d fields\n", num_fields);
      sql_field_seek(mdb, 0);
      for (i = 0; i < num_fields; i++) {
         field = sql_fetch_field(mdb);
         if (!field) {
            break;
         }
         if (row[i] == NULL) {
            bsnprintf(buf, sizeof(buf), " %*s: %s\n", max_len, field->name, "NULL");
         } else if (sql_field_is_numeric(mdb, field->type) && !jcr->gui && is_an_integer(row[i])) {
            bsnprintf(buf, sizeof(buf), " %*s: %s\n", max_len, field->name,
                      add_commas(row[i], ewc));
         } else {
            bsnprintf(buf, sizeof(buf), " %*s: %s\n", max_len, field->name, row[i]);
         }
         send(ctx, buf);
      }
      send(ctx, "\n");
      break;
   default:
      break;
   }
   return 0;
}
Пример #7
0
/*
 * int_to_string() - convert integer value to string
 *   return: formatted string
 *   int_value(in): integer value to convert
 *   field_width(in): the desired field width
 *   leading_zeros(in): whether or not to show leading zeros
 *   leading_symbol(in): whether or not to show the leading symbol
 *   commas(in): whether or not to display commas
 *   conversion(in): conversion format charactern
 */
static char *
bigint_to_string (DB_BIGINT int_value, int field_width, bool leading_zeros, bool leading_symbol, bool commas,
		  char conversion)
{
  char numeric_conversion_string[1024];
  char format_string[10];
  char long_decimal = 'l';
  int i = 0;
  int overall_fieldwidth;

  if (field_width < 0)
    {
      field_width = 0;
    }

  overall_fieldwidth = field_width;
  format_string[i++] = '%';

  if (leading_zeros == true)
    {
      format_string[i++] = '0';
    }

  if (leading_symbol == true)
    {
      format_string[i++] = '#';
      if (overall_fieldwidth)
	{
	  switch (conversion)
	    {
	    case INT_FORMAT_SIGNED_DECIMAL:
	      {
		if (overall_fieldwidth && (int_value < 0))
		  {
		    overall_fieldwidth++;
		  }
		break;
	      }
	    case INT_FORMAT_HEXADECIMAL:
	      {
		if (overall_fieldwidth)
		  {
		    overall_fieldwidth += 2;
		  }
		break;
	      }
	    default:
	      break;
	    }
	}
    }

  if (int_value < 0)
    {
      format_string[i++] = '+';
    }

  format_string[i++] = '*';

  format_string[i++] = long_decimal;
  format_string[i++] = long_decimal;
  format_string[i++] = conversion;
  format_string[i++] = (char) 0;

  if (!sprintf (numeric_conversion_string, (char *) format_string, (int) field_width, int_value))
    {
      return (NULL);
    }
  else
    {
      char *return_string;
      int actual_fieldwidth = strlen (numeric_conversion_string);

      if (field_width == 0)
	{
	  if ((return_string =
	       (char *) malloc (actual_fieldwidth + COMMAS_OFFSET (commas, actual_fieldwidth) + 1)) == NULL)
	    {
	      return (NULL);
	    }
	  (void) strcpy (return_string, (const char *) &numeric_conversion_string[0]);
	}
      else
	{
	  if ((return_string =
	       (char *) malloc (overall_fieldwidth + COMMAS_OFFSET (commas, actual_fieldwidth) + 1)) == NULL)
	    {
	      return (NULL);
	    }
	  if (actual_fieldwidth <= overall_fieldwidth)
	    {
	      return_string = strcpy (return_string, numeric_conversion_string);
	    }
	  else
	    {
	      return_string[overall_fieldwidth] = numeric_conversion_string[actual_fieldwidth];
	      while (overall_fieldwidth)
		{
		  return_string[--overall_fieldwidth] = numeric_conversion_string[--actual_fieldwidth];
		}
	    }
	}
      if (commas == true)
	{
	  add_commas (return_string);
	}
      return (return_string);
    }
}
Пример #8
0
/*
 * double_to_string() - convert double value to string
 *   return: formatted string
 *   double_value(in): double value to convert
 *   field_width(in): the overall fieldwidth
 *   precision(in): the number of places after the decimal point
 *   leading_sign(in): true if leading sign '+' should be forced to show
 *   leading_str(in): the leading symbols to show, NULL if none desired
 *   trailing_str(in): the traling symbols to show, NULL if none desired
 *   leading_zeros(in): whether or not to show leading zeros
 *   trailing_zeros(in): whether or not to show trailing zeros
 *   commas(in): whether or not to show commas (every three digits)
 *   conversion(in): conversion format character, scientific or decimal
 */
static char *
double_to_string (double double_value, int field_width, int precision, const bool leading_sign, const char *leading_str,
		  const char *trailing_str, bool leading_zeros, bool trailing_zeros, bool commas, char conversion)
{
  char numeric_conversion_string[1024];
  char precision_string[16];
  char format_string[32];
  int i, overall_fieldwidth;

  if (field_width < 0)
    {
      field_width = 0;
    }

  overall_fieldwidth = field_width;

  if (precision < 0)
    {
      precision = 0;
    }

  snprintf (precision_string, sizeof (precision_string) - 1, ".%u", (int) precision);

  i = 0;

  format_string[i++] = '%';

  if ((double_value < (double) 0) || (leading_sign == true))
    {
      format_string[i++] = '+';
      if (overall_fieldwidth)
	{
	  overall_fieldwidth++;
	}
    }

  if (leading_zeros == true)
    {
      format_string[i++] = '0';
    }

  if ((trailing_zeros == true) && (precision))
    {
      format_string[i++] = '#';
    }

  format_string[i++] = '*';
  format_string[i] = 0;


  strcat ((char *) format_string, (char *) precision_string);
  i = strlen (format_string);
  format_string[i++] = conversion;
  format_string[i] = 0;
  if ((overall_fieldwidth > 0) && (conversion == DOUBLE_FORMAT_SCIENTIFIC))
    {
      overall_fieldwidth += 4;
    }

  if (!sprintf ((char *) numeric_conversion_string, (char *) format_string, (int) field_width, double_value))
    {
      return (NULL);
    }
  else
    {
      char *return_string;
      int actual_fieldwidth = strlen (numeric_conversion_string);
      int leading_size = (leading_str != NULL) ? strlen (leading_str) : 0;
      int trailing_size = (trailing_str != NULL) ? strlen (trailing_str) : 0;

      if ((size_t) (leading_size + actual_fieldwidth + 1) > sizeof (numeric_conversion_string))
	{
	  return NULL;
	}
      if (leading_size > 0)
	{
	  memmove (numeric_conversion_string + leading_size, numeric_conversion_string, actual_fieldwidth);
	  memcpy (numeric_conversion_string, leading_str, leading_size);

	  numeric_conversion_string[actual_fieldwidth + leading_size] = '\0';
	  actual_fieldwidth += leading_size;
	}
#if defined(HPUX)
      /* workaround for HP's broken printf */
      if (strstr (numeric_conversion_string, "+.+") || strstr (numeric_conversion_string, "++"))
	sprintf (numeric_conversion_string, "Inf");
      if (strstr (numeric_conversion_string, "-.-") || strstr (numeric_conversion_string, "--"))
	sprintf (numeric_conversion_string, "-Inf");
#endif

      if (trailing_zeros == false)
	{
	  strip_trailing_zeros (numeric_conversion_string);
	  actual_fieldwidth = strlen (numeric_conversion_string);
	}

      if ((size_t) (trailing_size + actual_fieldwidth + 1) > sizeof (numeric_conversion_string))
	{
	  return NULL;
	}

      if (trailing_size > 0)
	{
	  memcpy (numeric_conversion_string + actual_fieldwidth, trailing_str, trailing_size);
	  numeric_conversion_string[actual_fieldwidth + trailing_size] = '\0';
	  actual_fieldwidth += trailing_size;
	}

      if (field_width == 0)
	{
	  if ((return_string =
	       (char *) malloc (actual_fieldwidth + COMMAS_OFFSET (commas, actual_fieldwidth) + 1)) == NULL)
	    {
	      return (NULL);
	    }
	  (void) strcpy (return_string, numeric_conversion_string);
	}
      else
	{
	  if ((return_string =
	       (char *) malloc (overall_fieldwidth + COMMAS_OFFSET (commas, actual_fieldwidth) + 1)) == NULL)
	    {
	      return (NULL);
	    }
	  if (actual_fieldwidth <= overall_fieldwidth)
	    {
	      return_string = strcpy (return_string, numeric_conversion_string);
	    }
	  else
	    {
	      return_string[overall_fieldwidth] = numeric_conversion_string[actual_fieldwidth];
	      while (overall_fieldwidth)
		{
		  return_string[--overall_fieldwidth] = numeric_conversion_string[--actual_fieldwidth];
		}
	    }
	}
      if (commas == true)
	{
	  add_commas (return_string);
	}
      return (return_string);
    }
}