How to read a variable/value from file? (Solved)

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
ITSMERSH

How to read a variable/value from file? (Solved)

#1 Post by ITSMERSH »

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: Select all

#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: Select all

ICON_WIDTH=24
or even simply like that:

Code: Select all

56
Any hints for me available?
Last edited by ITSMERSH on Thu 20 Dec 2018, 00:28, edited 1 time in total.

musher0
Posts: 14629
Joined: Mon 05 Jan 2009, 00:54
Location: Gatineau (Qc), Canada

#2 Post by musher0 »

Hi Rainer.

Per chance, would this technique be of help?

BFN
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

User avatar
rcrsn51
Posts: 13096
Joined: Tue 05 Sep 2006, 13:50
Location: Stratford, Ontario

#3 Post by rcrsn51 »

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: Select all

#define SETTING 10
and test.txt contains the new value

Code: Select all

20
then use

Code: Select all

 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?

ITSMERSH

#4 Post by ITSMERSH »

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

User avatar
puppy_apprentice
Posts: 299
Joined: Tue 07 Feb 2012, 20:32

#5 Post by puppy_apprentice »

Try this:

Code: Select all

#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: Select all

param1=25
param2=35
param3=48
Compile:

Code: Select all

gcc -o fileread fileread.c

ITSMERSH

#6 Post by ITSMERSH »

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: Select all

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: Select all

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: Select all

			else
			{
				w = ICON_WIDTH;
				h = ICON_HEIGHT;
			}
to

Code: Select all

			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: Select all

	width = MIN(image->width, ICON_WIDTH);
	height = MIN(image->height, ICON_HEIGHT);
to

Code: Select all

	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: Select all

	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: Select all

#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: Select all

#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: Select all

	normal_pixbuf = scale_pixbuf(src_pixbuf, ICON_WIDTH, ICON_HEIGHT);
	g_return_val_if_fail(normal_pixbuf != NULL, NULL);
to

Code: Select all

	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: Select all

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:

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...
:shock:

Any hints?

User avatar
puppy_apprentice
Posts: 299
Joined: Tue 07 Feb 2012, 20:32

#7 Post by puppy_apprentice »

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

Code: Select all

#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, 18:05, edited 1 time in total.

ITSMERSH

#8 Post by ITSMERSH »

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

Code: Select all

Here will be some code later, strictly for you example code
I'm just hacking - as usual... :wink: :lol:

ITSMERSH

#9 Post by ITSMERSH »

Hi puppy_apprentice!

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

Thank you very much! :D

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.
Attachments
RoxFiler-with-24px-icons-by-cfg-file.jpg
(60.76 KiB) Downloaded 182 times

User avatar
puppy_apprentice
Posts: 299
Joined: Tue 07 Feb 2012, 20:32

#10 Post by puppy_apprentice »

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 ;)

ITSMERSH

#11 Post by ITSMERSH »

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!

:D

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#12 Post by technosaurus »

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: Select all

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 [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

Post Reply