GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Fri Aug 22, 2014 3:58 am

All times are UTC




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Some Example Glib Utility Functions ( Beginners C )
PostPosted: Thu Feb 12, 2009 3:01 am 
Offline
GTK+ Guru

Joined: Thu Jun 21, 2007 1:52 pm
Posts: 198
Location: Wilkes Barre Pa
Here are some example glib functions for our beginner c programmers.

Code:
#include <glib.h>

/*
Show example usage of ...
GLib Utilities | File Utilities | g_file_test, g_mkdir(), g_rmdir(), g_rename(), g_chmod()
GLib Utilities | Miscellaneous Utility Functions | g_get_user_name, g_set_prgname, g_get_prgname, g_setenv, g_getenv, g_unsetenv
GLib Utilities | String Utility Functions | g_strcmp0(), g_str_has_prefix(), g_str_has_suffix()
GLib Utilities | URI Functions | g_uri_parse_scheme()

gcc -o simple simple.c `pkg-config --libs --cflags glib-2.0`
*/

int main( int argc, char *argv[])
{

  /* g_file_test */
  /* Test if a file exists. */
  if (!g_file_test("simple", G_FILE_TEST_EXISTS)) {
      g_print ("File not found. \n");
  } else {
      g_print ("File found. \n");   
  }
 
  /* g_get_user_name() */
  /* Get name of current user. */
  const gchar *guname = g_get_user_name();
  g_print("%s \n", guname);
 
  /* g_set_prgname() */
  /* Set our programs name. */
  const gchar *prgname = "Test Application";
  g_set_prgname(prgname);
 
  /* g_get_prgname() */
  /* Get our programs name. */
  gchar *ganame = g_get_prgname();
  g_print("%s \n", ganame);
 
  /* g_setenv() */
  /* Apply value to TEST Enviroment Varible. */
  g_setenv("TEST", "Hello, Gtk+ Forum", TRUE);
                   
  /* g_getenv() */
  /* Get value of TEST Enviroment Varible. */
  const gchar *env_return;
  env_return = g_getenv("TEST");
  g_print("%s \n", env_return);
 
  /* g_unsetenv() */
  /* Unset the TEST Enviromen Varible. */
  g_unsetenv("TEST");
   
  /* g_uri_parse_scheme() */
  /* Check a URI to see if it is ftp, http or other. */
  const gchar *uri_type;
  uri_type = g_uri_parse_scheme("ftp://www.example.com/packages/example-1.0-1.tar.gz");
  g_print("%s \n", uri_type);
 
  /* g_strcmp0() */
  /* Compare two strings and determine if they match.*/
  const gchar *cmp = "ftp";
  if(g_strcmp0(uri_type,cmp) == 0) {
        g_print("True \n");
  }
  else {
        g_print("False \n");
  }

  /* g_str_has_prefix() */
  /* Check if a string has a particular prefix.*/
  gboolean prefix_value;
  prefix_value = g_str_has_prefix("ftp://www.example.com/packages/example-1.0-1.tar.gz", "ftp:");
  if(prefix_value == TRUE) {
        g_print("Prefix is true \n");
  }
  else {
        g_print("Prefix is false \n");
  }

  /* g_str_has_suffix() */
  /* Check if a string has a particular suffix.*/
  gboolean suffix_value;
  suffix_value = g_str_has_suffix("ftp://www.example.com/packages/example-1.0-1.tar.gz", ".tar.gz");
  if(suffix_value == TRUE) {
        g_print("Suffix is true \n");
  }
  else {
        g_print("Suffix is false \n");
  }

  /* g_mkdir() */
  /* Create a new dir with these permissions if it deosn't already exist.*/
  const gchar *dir_name = "Example";
  if (!g_file_test(dir_name, G_FILE_TEST_EXISTS)) {
      g_print ("Creating %s directory. \n", dir_name);
      g_mkdir(dir_name, 0700);
  } else {
      g_print ("Directory %s already exists. \n", dir_name);   
  }

  /* g_rmdir() */
  /* Remove a dir if it exist.*/
  if (!g_file_test(dir_name, G_FILE_TEST_EXISTS)) {
      g_print ("Directory %s doesn't exists. \n", dir_name);
  } else {
      g_print ("Removing %s directory. \n", dir_name);
      g_rmdir(dir_name);   
  }

  /* g_rename() */
  /* Rename a file or directory. */
  const gchar *old_name = "Example1";
  const gchar *new_name = "Example2";
  if (!g_file_test(old_name, G_FILE_TEST_EXISTS)) {
      g_print ("Directory %s doesn't exists. \n", old_name);
  } else {
      g_print ("Renaming %s directory to %s. \n", old_name, new_name);
      g_rename(old_name, new_name);   
  }

  /* g_chmod() */
  /* Change the permissions of a file or directory. */
  const gchar *file_name = "Test.txt";
  if (!g_file_test(file_name, G_FILE_TEST_EXISTS)) {
      g_print ("File %s doesn't exists. \n", file_name);
  } else {
      g_print ("Changing %s permissions. \n", file_name);
      g_chmod(file_name, 0700);   
  }

  return 0;
}


Last edited by caracal on Sun Mar 01, 2009 2:50 pm, edited 8 times in total.

Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 12, 2009 6:12 pm 
Offline
Never Seen the Sunlight

Joined: Thu Jun 14, 2007 11:02 pm
Posts: 923
Location: Falun, WI USA
Docs for reference:
http://library.gnome.org/devel/glib/sta ... -file-test
http://library.gnome.org/devel/glib/sta ... -user-name
http://library.gnome.org/devel/glib/sta ... et-prgname
http://library.gnome.org/devel/glib/sta ... et-prgname
http://library.gnome.org/devel/glib/sta ... l#g-setenv
http://library.gnome.org/devel/glib/sta ... l#g-getenv
http://library.gnome.org/devel/glib/sta ... g-unsetenv


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 12, 2009 11:30 pm 
Offline
GTK+ Guru

Joined: Thu Jun 21, 2007 1:52 pm
Posts: 198
Location: Wilkes Barre Pa
http://library.gnome.org/devel/glib/sta ... ml#g-mkdir
http://library.gnome.org/devel/glib/sta ... ml#g-rmdir
http://library.gnome.org/devel/glib/sta ... l#g-rename
http://library.gnome.org/devel/glib/sta ... ml#g-chmod
http://library.gnome.org/devel/glib/sta ... #g-strcmp0
http://library.gnome.org/devel/glib/sta ... has-prefix
http://library.gnome.org/devel/glib/sta ... has-suffix
http://library.gnome.org/devel/glib/sta ... rse-scheme


Top
 Profile  
 
 Post subject: file opening code
PostPosted: Sun Mar 21, 2010 6:53 am 
Offline
Familiar Face

Joined: Mon Mar 01, 2010 3:21 pm
Posts: 37
Location: India
Your contribution is valuable Caracal - for opening a file the following code could be used and it opens a file...but I am unable to get rid of the compilation error....could you please help?

Code:
#include <glib.h>
#include <gtk/gtk.h>

int main( int argc, char *argv[])
{
  /* opening a file */
  FILE *file = g_fopen("Text","w");
  fputs ("This is a data",file);
  return 0;
}


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 21, 2010 10:16 am 
Offline
Never Seen the Sunlight

Joined: Wed Jul 23, 2008 10:31 am
Posts: 2406
Location: Slovenia
Hi.

You'll probably want to include <glib/gstdio.h> header file in your sample application (this will pull in g_fopen() declaration).

Tadej


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 1 guest


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 post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group