Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Wed 23 Oct 2019, 18:01
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
How to read a variable/value from file? (Solved)
Post new topic   Reply to topic View previous topic :: View next topic
Page 1 of 1 [12 Posts]  
Author Message
ITSMERSH

Joined: 02 May 2018
Posts: 911

PostPosted: Sun 16 Dec 2018, 23:49    Post subject:  How to read a variable/value from file? (Solved)
Subject description: C++ issue
 

Hi.

I've got a small problem (for the experts of course) of how to read a parameter-value from a text file instead of defining it hard-coded in .h file?

Example:

Code:
#define ICON_WIDTH 40


I want to read the value for ICON_WIDTH from a text file, so I can edit the value manually and the program shows the icons just in another/different size (dependent on my daily needs Wink )

The text file should include then:
Code:
ICON_WIDTH=24

or even simply like that:
Code:
56


Any hints for me available?

_________________
RSH

Beware of the Dog ähem nic007! Wink

Last edited by ITSMERSH on Wed 19 Dec 2018, 20:28; edited 1 time in total
Back to top
View user's profile Send private message 
musher0

Joined: 04 Jan 2009
Posts: 14414
Location: Gatineau (Qc), Canada

PostPosted: Mon 17 Dec 2018, 06:36    Post subject:  

Hi Rainer.

Per chance, would this technique be of help?

BFN

_________________
musher0
~~~~~~~~~~
Je suis né pour aimer et non pas pour haïr. (Sophocle) /
I was born to love and not to hate. (Sophocles)
Back to top
View user's profile Send private message 
rcrsn51


Joined: 05 Sep 2006
Posts: 12748
Location: Stratford, Ontario

PostPosted: Mon 17 Dec 2018, 08:15    Post subject:  

I'm not clear on what you want to do.

Are you looking for a "sed" command that would change the value in an xxx.h file with another value that is stored in a separate file?

For example, if test.h contains the line
Code:
#define SETTING 10

and test.txt contains the new value
Code:
20

then use
Code:
 sed -i "s/define SETTING.*/define SETTING $(cat test.txt)/" test.h


Do you want this to happen BEFORE you compile the code?

Or do you want to add code so the program looks up the value of SETTING in test.txt when it starts?
Back to top
View user's profile Send private message 
ITSMERSH

Joined: 02 May 2018
Posts: 911

PostPosted: Mon 17 Dec 2018, 12:13    Post subject:  

Quote:
Or do you want to add code so the program looks up the value of SETTING in test.txt when it starts?

Exactly.

_________________
RSH

Beware of the Dog ähem nic007! Wink
Back to top
View user's profile Send private message 
puppy_apprentice


Joined: 07 Feb 2012
Posts: 300

PostPosted: Mon 17 Dec 2018, 16:56    Post subject:  

Try this:

Code:
#include <stdio.h>
#include <string.h>

#define MAX 255

void get_params(char *name)
{
   FILE *f;
   char line[MAX];
   char delim[] = "=";
   char *parname = NULL;
   char *parvalue = NULL;

   f = fopen(name, "r");
   if (f == NULL){
      printf("\nWrong file name.\n\n");
      return;
   }
   
      while (fgets(line, MAX, f) != NULL) {
         if (strstr(line, delim) != NULL) {
            parname = strtok(line, delim);
            parvalue = strtok(NULL, delim);
            printf("parname is %s\nparvalue is %s", parname, parvalue);
         }
      }
   
   fclose(f);
}

int main()
{
   get_params("params.txt");
   return(0);
}


where params.txt:
Code:
param1=25
param2=35
param3=48


Compile:
Code:
gcc -o fileread fileread.c
Back to top
View user's profile Send private message 
ITSMERSH

Joined: 02 May 2018
Posts: 911

PostPosted: Wed 19 Dec 2018, 00:59    Post subject:  

Thanks @puppy_apprentice.

This was some useful code and helpful also.

Though, I'm now faced to another issue.

I implemented the code (a little modified for my needs) into file pixmaps.c. Code snippet:
Code:
static gchar *thumbnail_path(const gchar *path);
static gchar *thumbnail_program(MIME_type *type);
static GdkPixbuf *extract_tiff_thumbnail(const gchar *path);

/****************************************************************
 *         Get parameters for Icon Sizes         *
 ****************************************************************/

void get_params(char *name)
{
   FILE *f;
   char line[MAXSLEN];
   char delim[] = "=";
   char *pname = NULL;
   char *pvalue = NULL;

   f = fopen(name, "r");
   if (f == NULL){
      printf("\nWrong file name.\n\n");
      return;
   }
   
      while (fgets(line, MAXSLEN, f) != NULL) {
         if (strstr(line, delim) != NULL) {
            pname = strtok(line, delim);
            pvalue = strtok(NULL, delim);
            printf("%s", pvalue);
         }
      }
   
   fclose(f);
}

/****************************************************************
 *         EXTERNAL INTERFACE         *
 ****************************************************************/

void pixmaps_init(void)
{
   GtkIconFactory *factory;
   int i;
   
   gtk_widget_push_colormap(gdk_rgb_get_colormap());


Commented out the lines in pixmaps.h (snippet):
Code:
extern GtkIconSize mount_icon_size;

/* If making the huge size larger, be sure to update SMALL_IMAGE_THRESHOLD! */
#define HUGE_WIDTH 96
#define HUGE_HEIGHT 96

//#define ICON_HEIGHT 40
//#define ICON_WIDTH 40

#define SMALL_HEIGHT 24
#define SMALL_WIDTH 24

typedef struct _MaskedPixmapClass MaskedPixmapClass;


Following by the first attempt to compile the program.

Got error on cell_icon.o. So I edited cell_icon.c and changed
Code:
         else
         {
            w = ICON_WIDTH;
            h = ICON_HEIGHT;
         }

to
Code:
         else
         {
            w = get_params("height.txt");
            h = get_params("width.txt");
         }

and compiled again.

Next error was on display.o. So I edited display.c and changed
Code:
   width = MIN(image->width, ICON_WIDTH);
   height = MIN(image->height, ICON_HEIGHT);

to
Code:
   width = MIN(image->width, get_params("width.txt"));
   height = MIN(image->height, get_params("height.txt"));

and compiled again.

Next error was on pinboard.o. So I edited pinboard.c and added the the ICON_WIDTH & ICON_HEIGHT lines
Code:
   else
      ICON_WIDTH = get_params("width.txt")
      ICON_HEIGHT = get_params("height.txt")
      pinboard_pin(home_dir, "Home",
            4 + ICON_WIDTH / 2,
            4 + ICON_HEIGHT / 2,
            NULL);
   loading_pinboard--;

Then compiled again. It complained about undeclared ICON_WIDTH & ICON_HEIGHT, first used in this function. So I added almost on top of pinboard.c
Code:
#define _ICON_WIDTH
#define _ICON_HEIGHT

Compiled again. Complained again, so I added this to main.h.

Compiled again. Complained again. Removed addition from main.h. Added almost on top of pinboard.c:
Code:
#define ICON_WIDTH = get_params("width.txt")
#define ICON_HEIGHT = get_params("height.txt")

Compiled again. It complained the =, so I removed it and compiled again. Success!

Next error was on pixmaps.o. So I edited pixmaps.c (the file where I implemented the get_params function). I changed
Code:
   normal_pixbuf = scale_pixbuf(src_pixbuf, ICON_WIDTH, ICON_HEIGHT);
   g_return_val_if_fail(normal_pixbuf != NULL, NULL);

to
Code:
   normal_pixbuf = scale_pixbuf(src_pixbuf, get_params("width.txt"), get_params("height.txt"));
   g_return_val_if_fail(normal_pixbuf != NULL, NULL);

Compiled again. It complained a missing prototype for the get_params function. So I added
Code:
static void get_params(char *name)
to pixmaps.c and compiled again (it complained also a invalid use of void impression?).

Now I stuck, as it comes up with thousands of errors:


Quote:
gcc -I. -I/mnt/sda2/Downloads/00-Beaver-64/00-Compile/Compiling-RoxFiler/rox-filer-rsh-2.12-beta/ROX-Filer/src -g -O2 -Wall -Wmissing-prototypes -Wno-pointer-sign -fno-stack-protector `pkg-config --cflags gtk+-2.0 libxml-2.0 sm ice` -c -o pinboard.o /mnt/sda2/Downloads/00-Beaver-64/00-Compile/Compiling-RoxFiler/rox-filer-rsh-2.12-beta/ROX-Filer/src/pinboard.c

/mnt/sda2/Downloads/00-Beaver-64/00-Compile/Compiling-RoxFiler/rox-filer-rsh-2.12-beta/ROX-Filer/src/pinboard.c: In function ‘pinboard_activate’:
/mnt/sda2/Downloads/00-Beaver-64/00-Compile/Compiling-RoxFiler/rox-filer-rsh-2.12-beta/ROX-Filer/src/pinboard.c:58:20: warning: implicit declaration of function ‘get_params’; did you mean ‘getpass’? [-Wimplicit-function-declaration]
#define ICON_WIDTH get_params("width.txt")
^
/mnt/sda2/Downloads/00-Beaver-64/00-Compile/Compiling-RoxFiler/rox-filer-rsh-2.12-beta/ROX-Filer/src/pinboard.c:365:9: note: in expansion of macro ‘ICON_WIDTH’
4 + ICON_WIDTH / 2,
^~~~~~~~~~
gcc -I. -I/mnt/sda2/Downloads/00-Beaver-64/00-Compile/Compiling-RoxFiler/rox-filer-rsh-2.12-beta/ROX-Filer/src -g -O2 -Wall -Wmissing-prototypes -Wno-pointer-sign -fno-stack-protector `pkg-config --cflags gtk+-2.0 libxml-2.0 sm ice` -c -o pixmaps.o /mnt/sda2/Downloads/00-Beaver-64/00-Compile/Compiling-RoxFiler/rox-filer-rsh-2.12-beta/ROX-Filer/src/pixmaps.c
/mnt/sda2/Downloads/00-Beaver-64/00-Compile/Compiling-RoxFiler/rox-filer-rsh-2.12-beta/ROX-Filer/src/pixmaps.c: In function ‘get_params’:
/mnt/sda2/Downloads/00-Beaver-64/00-Compile/Compiling-RoxFiler/rox-filer-rsh-2.12-beta/ROX-Filer/src/pixmaps.c:112:13: error: storage class specified for parameter ‘load_default_pixmaps’
static void load_default_pixmaps(void);
^~~~~~~~~~~~~~~~~~~~
/mnt/sda2/Downloads/00-Beaver-64/00-Compile/Compiling-RoxFiler/rox-filer-rsh-2.12-beta/ROX-Filer/src/pixmaps.c:113:13: error: storage class specified for parameter ‘purge’
static gint purge(gpointer data);
^~~~~
/mnt/sda2/Downloads/00-Beaver-64/00-Compile/Compiling-RoxFiler/rox-filer-rsh-2.12-beta/ROX-Filer/src/pixmaps.c:114:22: error: storage class specified for parameter ‘image_from_file’
static MaskedPixmap *image_from_file(const char *path);
^~~~~~~~~~~~~~~
/mnt/sda2/Downloads/00-Beaver-64/00-Compile/Compiling-RoxFiler/rox-filer-rsh-2.12-beta/ROX-Filer/src/pixmaps.c:115:22: error: storage class specified for parameter ‘image_from_desktop_file’
static MaskedPixmap *image_from_desktop_file(const char *path);
^~~~~~~~~~~~~~~~~~~~~~~
/mnt/sda2/Downloads/00-Beaver-64/00-Compile/Compiling-RoxFiler/rox-filer-rsh-2.12-beta/ROX-Filer/src/pixmaps.c:116:22: error: storage class specified for parameter ‘get_bad_image’
static MaskedPixmap *get_bad_image(void);
^~~~~~~~~~~~~
/mnt/sda2/Downloads/00-Beaver-64/00-Compile/Compiling-RoxFiler/rox-filer-rsh-2.12-beta/ROX-Filer/src/pixmaps.c:117:19: error: storage class specified for parameter ‘scale_pixbuf_up’
static GdkPixbuf *scale_pixbuf_up(GdkPixbuf *src, int max_w, int max_h);
^~~~~~~~~~~~~~~
/mnt/sda2/Downloads/00-Beaver-64/00-Compile/Compiling-RoxFiler/rox-filer-rsh-2.12-beta/ROX-Filer/src/pixmaps.c:118:19: error: storage class specified for parameter ‘get_thumbnail_for’
static GdkPixbuf *get_thumbnail_for(const char *path);
^~~~~~~~~~~~~~~~~
/mnt/sda2/Downloads/00-Beaver-64/00-Compile/Compiling-RoxFiler/rox-filer-rsh-2.12-beta/ROX-Filer/src/pixmaps.c:119:13: error: storage class specified for parameter ‘thumbnail_child_done’
static void thumbnail_child_done(ChildThumbnail *info);
^~~~~~~~~~~~~~~~~~~~
/mnt/sda2/Downloads/00-Beaver-64/00-Compile/Compiling-RoxFiler/rox-filer-rsh-2.12-beta/ROX-Filer/src/pixmaps.c:120:13: error: storage class specified for parameter ‘child_create_thumbnail’
static void child_create_thumbnail(const gchar *path, MIME_type *type);
^~~~~~~~~~~~~~~~~~~~~~
/mnt/sda2/Downloads/00-Beaver-64/00-Compile/Compiling-RoxFiler/rox-filer-rsh-2.12-beta/ROX-Filer/src/pixmaps.c:121:15: error: storage class specified for parameter ‘thumbs_purge_cache’
static GList *thumbs_purge_cache(Option *option, xmlNode *node, guchar *label);
^~~~~~~~~~~~~~~~~~
/mnt/sda2/Downloads/00-Beaver-64/00-Compile/Compiling-RoxFiler/rox-filer-rsh-2.12-beta/ROX-Filer/src/pixmaps.c:122:15: error: storage class specified for parameter ‘thumbnail_path’
static gchar *thumbnail_path(const gchar *path);
^~~~~~~~~~~~~~
/mnt/sda2/Downloads/00-Beaver-64/00-Compile/Compiling-RoxFiler/rox-filer-rsh-2.12-beta/ROX-Filer/src/pixmaps.c:123:15: error: storage class specified for parameter ‘thumbnail_program’
static gchar *thumbnail_program(MIME_type *type);
^~~~~~~~~~~~~~~~~
/mnt/sda2/Downloads/00-Beaver-64/00-Compile/Compiling-RoxFiler/rox-filer-rsh-2.12-beta/ROX-Filer/src/pixmaps.c:124:19: error: storage class specified for parameter ‘extract_tiff_thumbnail’
static GdkPixbuf *extract_tiff_thumbnail(const gchar *path);
^~~~~~~~~~~~~~~~~~~~~~
/mnt/sda2/Downloads/00-Beaver-64/00-Compile/Compiling-RoxFiler/rox-filer-rsh-2.12-beta/ROX-Filer/src/pixmaps.c:131:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
{
^
/mnt/sda2/Downloads/00-Beaver-64/00-Compile/Compiling-RoxFiler/rox-filer-rsh-2.12-beta/ROX-Filer/src/pixmaps.c:160:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
{
^
/mnt/sda2/Downloads/00-Beaver-64/00-Compile/Compiling-RoxFiler/rox-filer-rsh-2.12-beta/ROX-Filer/src/pixmaps.c:208:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
{
^
/mnt/sda2/Downloads/00-Beaver-64/00-Compile/Compiling-RoxFiler/rox-filer-rsh-2.12-beta/ROX-Filer/src/pixmaps.c:226:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
{
^
/mnt/sda2/Downloads/00-Beaver-64/00-Compile/Compiling-RoxFiler/rox-filer-rsh-2.12-beta/ROX-Filer/src/pixmaps.c:251:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
{
^
/mnt/sda2/Downloads/00-Beaver-64/00-Compile/Compiling-RoxFiler/rox-filer-rsh-2.12-beta/ROX-Filer/src/pixmaps.c:274:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
{
^
/mnt/sda2/Downloads/00-Beaver-64/00-Compile/Compiling-RoxFiler/rox-filer-rsh-2.12-beta/ROX-Filer/src/pixmaps.c:298:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
{
^
/mnt/sda2/Downloads/00-Beaver-64/00-Compile/Compiling-RoxFiler/rox-filer-rsh-2.12-beta/ROX-Filer/src/pixmaps.c:402:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
{
^
/mnt/sda2/Downloads/00-Beaver-64/00-Compile/Compiling-RoxFiler/rox-filer-rsh-2.12-beta/ROX-Filer/src/pixmaps.c:479:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
{
^
/mnt/sda2/Downloads/00-Beaver-64/00-Compile/Compiling-RoxFiler/rox-filer-rsh-2.12-beta/ROX-Filer/src/pixmaps.c:554:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
{
^
/mnt/sda2/Downloads/00-Beaver-64/00-Compile/Compiling-RoxFiler/rox-filer-rsh-2.12-beta/ROX-Filer/src/pixmaps.c:586:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
{
^
/mnt/sda2/Downloads/00-Beaver-64/00-Compile/Compiling-RoxFiler/rox-filer-rsh-2.12-beta/ROX-Filer/src/pixmaps.c:611:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
{
^
/mnt/sda2/Downloads/00-Beaver-64/00-Compile/Compiling-RoxFiler/rox-filer-rsh-2.12-beta/ROX-Filer/src/pixmaps.c:629:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
{
^
/mnt/sda2/Downloads/00-Beaver-64/00-Compile/Compiling-RoxFiler/rox-filer-rsh-2.12-beta/ROX-Filer/src/pixmaps.c:657:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
{
^
/mnt/sda2/Downloads/00-Beaver-64/00-Compile/Compiling-RoxFiler/rox-filer-rsh-2.12-beta/ROX-Filer/src/pixmaps.c:714:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
{
^
/mnt/sda2/Downloads/00-Beaver-64/00-Compile/Compiling-RoxFiler/rox-filer-rsh-2.12-beta/ROX-Filer/src/pixmaps.c:738:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
{
^
/mnt/sda2/Downloads/00-Beaver-64/00-Compile/Compiling-RoxFiler/rox-filer-rsh-2.12-beta/ROX-Filer/src/pixmaps.c:798:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
{
^
/mnt/sda2/Downloads/00-Beaver-64/00-Compile/Compiling-RoxFiler/rox-filer-rsh-2.12-beta/ROX-Filer/src/pixmaps.c:828:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
{
^
/mnt/sda2/Downloads/00-Beaver-64/00-Compile/Compiling-RoxFiler/rox-filer-rsh-2.12-beta/ROX-Filer/src/pixmaps.c:856:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
{
^
/mnt/sda2/Downloads/00-Beaver-64/00-Compile/Compiling-RoxFiler/rox-filer-rsh-2.12-beta/ROX-Filer/src/pixmaps.c:869:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
{
^
/mnt/sda2/Downloads/00-Beaver-64/00-Compile/Compiling-RoxFiler/rox-filer-rsh-2.12-beta/ROX-Filer/src/pixmaps.c:875:17: error: storage class specified for parameter ‘parent_class’
static gpointer parent_class;
^~~~~~~~~~~~
/mnt/sda2/Downloads/00-Beaver-64/00-Compile/Compiling-RoxFiler/rox-filer-rsh-2.12-beta/ROX-Filer/src/pixmaps.c:878:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
{
^
/mnt/sda2/Downloads/00-Beaver-64/00-Compile/Compiling-RoxFiler/rox-filer-rsh-2.12-beta/ROX-Filer/src/pixmaps.c:908:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
{
^
/mnt/sda2/Downloads/00-Beaver-64/00-Compile/Compiling-RoxFiler/rox-filer-rsh-2.12-beta/ROX-Filer/src/pixmaps.c:917:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
{
^
/mnt/sda2/Downloads/00-Beaver-64/00-Compile/Compiling-RoxFiler/rox-filer-rsh-2.12-beta/ROX-Filer/src/pixmaps.c:936:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
{
^
/mnt/sda2/Downloads/00-Beaver-64/00-Compile/Compiling-RoxFiler/rox-filer-rsh-2.12-beta/ROX-Filer/src/pixmaps.c:962:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
{
^
/mnt/sda2/Downloads/00-Beaver-64/00-Compile/Compiling-RoxFiler/rox-filer-rsh-2.12-beta/ROX-Filer/src/pixmaps.c:987:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
{
^
/mnt/sda2/Downloads/00-Beaver-64/00-Compile/Compiling-RoxFiler/rox-filer-rsh-2.12-beta/ROX-Filer/src/pixmaps.c:1020:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or __attribute__’ before ‘{’ token
{
^
/mnt/sda2/Downloads/00-Beaver-64/00-Compile/Compiling-RoxFiler/rox-filer-rsh-2.12-beta/ROX-Filer/src/pixmaps.c:1060:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or __attribute__’ before ‘{’ token
{
^
/mnt/sda2/Downloads/00-Beaver-64/00-Compile/Compiling-RoxFiler/rox-filer-rsh-2.12-beta/ROX-Filer/src/pixmaps.c:1085:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or __attribute__’ before ‘{’ token
{
^
/mnt/sda2/Downloads/00-Beaver-64/00-Compile/Compiling-RoxFiler/rox-filer-rsh-2.12-beta/ROX-Filer/src/pixmaps.c:1099:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or __attribute__’ before ‘{’ token
{
^
/mnt/sda2/Downloads/00-Beaver-64/00-Compile/Compiling-RoxFiler/rox-filer-rsh-2.12-beta/ROX-Filer/src/pixmaps.c:1113:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or __attribute__’ before ‘{’ token
{
^
/mnt/sda2/Downloads/00-Beaver-64/00-Compile/Compiling-RoxFiler/rox-filer-rsh-2.12-beta/ROX-Filer/src/pixmaps.c:1132:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or __attribute__’ before ‘{’ token
{
^
/mnt/sda2/Downloads/00-Beaver-64/00-Compile/Compiling-RoxFiler/rox-filer-rsh-2.12-beta/ROX-Filer/src/pixmaps.c:111:13: error: old-style parameter declarations in prototyped function definition
static void get_params(char *name)
^~~~~~~~~~
/mnt/sda2/Downloads/00-Beaver-64/00-Compile/Compiling-RoxFiler/rox-filer-rsh-2.12-beta/ROX-Filer/src/pixmaps.c:1225:1: error: expected ‘{’ at end of input
}
^
At top level:
/mnt/sda2/Downloads/00-Beaver-64/00-Compile/Compiling-RoxFiler/rox-filer-rsh-2.12-beta/ROX-Filer/src/pixmaps.c:111:13: warning: ‘get_params’ defined but not used [-Wunused-function]
static void get_params(char *name)
^~~~~~~~~~
/mnt/sda2/Downloads/00-Beaver-64/00-Compile/Compiling-RoxFiler/rox-filer-rsh-2.12-beta/ROX-Filer/src/pixmaps.c:99:20: warning: ‘stocks’ defined but not used [-Wunused-variable]
static const char *stocks[] = {
^~~~~~
/mnt/sda2/Downloads/00-Beaver-64/00-Compile/Compiling-RoxFiler/rox-filer-rsh-2.12-beta/ROX-Filer/src/pixmaps.c:63:21: warning: ‘bad_xpm’ defined but not used [-Wunused-variable]
static const char * bad_xpm[] = {
^~~~~~~
<builtin>: recipe for target 'pixmaps.o' failed
make: *** [pixmaps.o] Error 1
Compile failed
Press Return...

Shocked

Any hints?

_________________
RSH

Beware of the Dog ähem nic007! Wink
Back to top
View user's profile Send private message 
puppy_apprentice


Joined: 07 Feb 2012
Posts: 300

PostPosted: Wed 19 Dec 2018, 02:29    Post subject:  

It was an example of technique not something ready to use ad hoc.

Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

//those two should be placed instead of #define ICON_HEIGHT 40 and #define ICON_WIDTH 40
#define MAX 255
int ICON_WIDTH, ICON_HEIGHT; //both are global now and will be reached by all functions

void get_params(char *name)
{
   FILE *f;
   char line[MAX];
   char delim[] = "=";
   char *parname = NULL;
   char *parvalue = NULL;
   
   f = fopen(name, "r");
   
   if (f == NULL)
   {
      //without params.txt file default values will be used
      ICON_WIDTH = 40;
      ICON_HEIGHT = 40;
      return;
   }
   
   while (fgets(line, MAX, f) != NULL)
   {
      if (strstr(line, delim) != NULL)
      {
         parname = strtok(line, delim);
         parvalue = strtok(NULL, delim);
         
         if (strcmp(parname, "ICON_WIDTH") == 0)
         {
            ICON_WIDTH = atoi(parvalue);
         }
         if (strcmp(parname, "ICON_HEIGHT") == 0)
         {
            ICON_HEIGHT = atoi(parvalue);
         }
      }
   }
   
   fclose(f);
}
int main()
{
   //i suppose this should be the first call in main
   get_params("params.txt");
   
   //this is for testing purpose so erase this before pasting
   printf("ICON_WIDTH = %d\nICON_HEIGHT = %d", ICON_WIDTH, ICON_HEIGHT);
   
   //this should be erased too before pasting
   return(0);
}

Last edited by puppy_apprentice on Wed 19 Dec 2018, 14:05; edited 1 time in total
Back to top
View user's profile Send private message 
ITSMERSH

Joined: 02 May 2018
Posts: 911

PostPosted: Wed 19 Dec 2018, 03:43    Post subject:  

puppy_apprentice wrote:
It was an example of technique not something ready to use ad hoc.

Code:

Here will be some code later, strictly for you example code

I'm just hacking - as usual... Wink Laughing

_________________
RSH

Beware of the Dog ähem nic007! Wink
Back to top
View user's profile Send private message 
ITSMERSH

Joined: 02 May 2018
Posts: 911

PostPosted: Wed 19 Dec 2018, 20:28    Post subject:  

Hi puppy_apprentice!

So, you're a real C programmer...

Thank you very much! Very Happy

The above code worked.

I made a small tweak, to read the file from a useful location (/root/.config/rox.sourceforge.net/ROX-Filer) plus a function-related file name (nemesis-rox-default-icon-sizes).

Rox Filer comes up with icons sized 24 pixels by the icons size configuration file. Tested different sizes like also 64 pixel. All worked fine.

Thanks again.
RoxFiler-with-24px-icons-by-cfg-file.jpg
 Description   
 Filesize   60.76 KB
 Viewed   179 Time(s)

RoxFiler-with-24px-icons-by-cfg-file.jpg


_________________
RSH

Beware of the Dog ähem nic007! Wink
Back to top
View user's profile Send private message 
puppy_apprentice


Joined: 07 Feb 2012
Posts: 300

PostPosted: Thu 20 Dec 2018, 03:57    Post subject:  

ITSMERSH wrote:
So, you're a real C programmer...


No, i'm only curious person who like to know how things works.

Nice to hear that my code works Wink
Back to top
View user's profile Send private message 
ITSMERSH

Joined: 02 May 2018
Posts: 911

PostPosted: Thu 20 Dec 2018, 15:23    Post subject:  

Oh, at least you can do more C programming than I'm able to do.
However, I think I learned and still can learn from that code and how it is implemented into Rox Filer now.

I have to correct my previous post, for the reason of Rox Filer seems to refuse icons sized greater that 48 pixel for the pinboard.

E.g. 96 pixel works fine for the windows contents but not for the pinboard.

Never mind, as my intention was to have ability to make the icons smaller by configuration file. So, I'm 100% satisfied!

Very Happy

_________________
RSH

Beware of the Dog ähem nic007! Wink
Back to top
View user's profile Send private message 
technosaurus


Joined: 18 May 2008
Posts: 4870
Location: Blue Springs, MO

PostPosted: Thu 20 Dec 2018, 18:03    Post subject:  

FWIW, the easiest way to read and write parameters from a file (though not editable as a text file) is to use a struuct.

Code:
struct myparams {
  unsigned short width;
  unsigned short height;
  unsigned int foreground;
  unsigned int background;
};

static const struct myparams default_params = {
  48,48,0xFFFFFF,0
};
static struct myparams params;

void get_params(void){
  int fd = open("path/to/file.conf", O_RDONLY);
  if (fd < 0 || sizeof(struct myparams) != read(fd, &params,sizeof(struct myparams))
    params = default_params;
}

void save_params(void){
  int fd = open("path/to/file.conf", O_WRONLY);
  if (fd >= 0 ) write(fd, &params, sizeof(struct myparams))
}


To do this with a text file you use a combination of *scanf and *printf to read and write the files and various methods to parse the input and serialize it. For C it helps to learn X-macros and _Generic if you are going to have a lot of parameters, but with C++ you can just use streams. For the example above, it would take at least 4 calls to fscanf to get the parameters as well as some code to put it in the right variable... although text based conf files are easier for the user, its not the case for the dev unless you use a specialized library for the task.

_________________
Check out my github repositories. I may eventually get around to updating my blogspot.
Back to top
View user's profile Send private message Visit poster's website 
Display posts from previous:   Sort by:   
Page 1 of 1 [12 Posts]  
Post new topic   Reply to topic View previous topic :: View next topic
 Forum index » Off-Topic Area » Programming
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.1259s ][ Queries: 13 (0.0194s) ][ GZIP on ]