~ubuntu-branches/ubuntu/oneiric/libfm/oneiric

« back to all changes in this revision

Viewing changes to src/base/fm-utils.c

  • Committer: Bazaar Package Importer
  • Author(s): Julien Lavergne
  • Date: 2011-02-21 21:55:43 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: james.westby@ubuntu.com-20110221215543-m7gn2snkhpk1kk9u
Tags: 0.1.15+git-3625952cea-0ubuntu1
* New upstream snapshot (2011-02-15)
 - Use x-schemas-handler (LP: #683922)
* debian/patches/
 - 90_add_gobject_link.patch: Remove, merged upstream.
 - 02-libfm-0.1.14-API-changes.patch: Refresh.
 - 03_disable_deprecated_gio_module.patch: Refresh.
 - 04_fix_docs_linker.patch: Fix DSO linking in docs/.
* debian/libfm0.install:
 - Remove usr/lib/libfm/gnome-terminal, dropped upstream.
 - Remove gio module, dropped upstream.
* debian/rules:
 - Add --enable-gtk-doc to configure.
* debian/libfm-gtk0.symbols:
 - Update with new symbols.
* debian/apport/source_libfm.py
 - Fix location of pcmanfm config files.

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
 
30
30
#include <stdio.h>
31
31
#include <stdlib.h>
 
32
#include <string.h>
32
33
#include "fm-utils.h"
33
34
#include "fm-file-info-job.h"
34
35
 
35
 
#define BI_KiB  ((gdouble)1024.0)
36
 
#define BI_MiB  ((gdouble)1024.0 * 1024.0)
37
 
#define BI_GiB  ((gdouble)1024.0 * 1024.0 * 1024.0)
38
 
#define BI_TiB  ((gdouble)1024.0 * 1024.0 * 1024.0 * 1024.0)
 
36
#define BI_KiB  ((gdouble)1024.0)
 
37
#define BI_MiB  ((gdouble)1024.0 * 1024.0)
 
38
#define BI_GiB  ((gdouble)1024.0 * 1024.0 * 1024.0)
 
39
#define BI_TiB  ((gdouble)1024.0 * 1024.0 * 1024.0 * 1024.0)
39
40
 
40
 
#define SI_KB   ((gdouble)1000.0)
41
 
#define SI_MB   ((gdouble)1000.0 * 1000.0)
42
 
#define SI_GB   ((gdouble)1000.0 * 1000.0 * 1000.0)
43
 
#define SI_TB   ((gdouble)1000.0 * 1000.0 * 1000.0 * 1000.0)
 
41
#define SI_KB   ((gdouble)1000.0)
 
42
#define SI_MB   ((gdouble)1000.0 * 1000.0)
 
43
#define SI_GB   ((gdouble)1000.0 * 1000.0 * 1000.0)
 
44
#define SI_TB   ((gdouble)1000.0 * 1000.0 * 1000.0 * 1000.0)
44
45
 
45
46
char* fm_file_size_to_str( char* buf, goffset size, gboolean si_prefix )
46
47
{
47
48
    const char * unit;
48
49
    gdouble val;
49
50
 
50
 
        if( si_prefix ) /* 1000 based SI units */
51
 
        {
52
 
                if(size < (goffset)SI_KB)
53
 
                {
54
 
                        sprintf( buf, ngettext("%u byte", "%u bytes", (guint)size), (guint)size);
55
 
                        return buf;
56
 
                }
57
 
                val = (gdouble)size;
58
 
                if(val < SI_MB)
59
 
                {
60
 
                        val /= SI_KB;
61
 
                        unit = _("KB");
62
 
                }
63
 
                else if(val < SI_GB)
64
 
                {
65
 
                        val /= SI_MB;
66
 
                        unit = _("MB");
67
 
                }
68
 
                else if(val < SI_TB)
69
 
                {
70
 
                        val /= SI_GB;
71
 
                        unit = _("GB");
72
 
                }
73
 
                else
74
 
                {
75
 
                        val /= SI_TB;
76
 
                        unit = _("TB");
77
 
                }
78
 
        }
79
 
        else /* 1024-based binary prefix */
80
 
        {
81
 
                if(size < (goffset)BI_KiB)
82
 
                {
83
 
                        sprintf( buf, ngettext("%u byte", "%u bytes", (guint)size), (guint)size);
84
 
                        return buf;
85
 
                }
86
 
                val = (gdouble)size;
87
 
                if(val < BI_MiB)
88
 
                {
89
 
                        val /= BI_KiB;
90
 
                        unit = _("KiB");
91
 
                }
92
 
                else if(val < BI_GiB)
93
 
                {
94
 
                        val /= BI_MiB;
95
 
                        unit = _("MiB");
96
 
                }
97
 
                else if(val < BI_TiB)
98
 
                {
99
 
                        val /= BI_GiB;
100
 
                        unit = _("GiB");
101
 
                }
102
 
                else
103
 
                {
104
 
                        val /= BI_TiB;
105
 
                        unit = _("TiB");
106
 
                }
107
 
        }
 
51
    if( si_prefix ) /* 1000 based SI units */
 
52
    {
 
53
        if(size < (goffset)SI_KB)
 
54
        {
 
55
            sprintf( buf, ngettext("%u byte", "%u bytes", (guint)size), (guint)size);
 
56
            return buf;
 
57
        }
 
58
        val = (gdouble)size;
 
59
        if(val < SI_MB)
 
60
        {
 
61
            val /= SI_KB;
 
62
            unit = _("KB");
 
63
        }
 
64
        else if(val < SI_GB)
 
65
        {
 
66
            val /= SI_MB;
 
67
            unit = _("MB");
 
68
        }
 
69
        else if(val < SI_TB)
 
70
        {
 
71
            val /= SI_GB;
 
72
            unit = _("GB");
 
73
        }
 
74
        else
 
75
        {
 
76
            val /= SI_TB;
 
77
            unit = _("TB");
 
78
        }
 
79
    }
 
80
    else /* 1024-based binary prefix */
 
81
    {
 
82
        if(size < (goffset)BI_KiB)
 
83
        {
 
84
            sprintf( buf, ngettext("%u byte", "%u bytes", (guint)size), (guint)size);
 
85
            return buf;
 
86
        }
 
87
        val = (gdouble)size;
 
88
        if(val < BI_MiB)
 
89
        {
 
90
            val /= BI_KiB;
 
91
            unit = _("KiB");
 
92
        }
 
93
        else if(val < BI_GiB)
 
94
        {
 
95
            val /= BI_MiB;
 
96
            unit = _("MiB");
 
97
        }
 
98
        else if(val < BI_TiB)
 
99
        {
 
100
            val /= BI_GiB;
 
101
            unit = _("GiB");
 
102
        }
 
103
        else
 
104
        {
 
105
            val /= BI_TiB;
 
106
            unit = _("TiB");
 
107
        }
 
108
    }
108
109
    sprintf( buf, "%.1f %s", val, unit );
109
 
        return buf;
 
110
    return buf;
110
111
}
111
112
 
112
113
gboolean fm_key_file_get_int(GKeyFile* kf, const char* grp, const char* key, int* val)
212
213
        g_free(_cwd);
213
214
    return ret;
214
215
}
 
216
 
 
217
char* fm_str_replace(char* str, char* old, char* new)
 
218
{
 
219
    int i;
 
220
    int len = strlen(str);
 
221
    char* found;
 
222
    GString* buf = g_string_sized_new(len);
 
223
    while(found = strstr(str, old))
 
224
    {
 
225
        g_string_append_len(buf, str, (found - str));
 
226
        g_string_append(buf, new);
 
227
        str = found + 1;
 
228
    }
 
229
    for(; *str; ++str)
 
230
        g_string_append_c(buf, *str);
 
231
    return g_string_free(buf, FALSE);
 
232
}