2009-11-08 Chris Toshok <toshok@ximian.com>
[moon.git] / src / utils.h
blob0d202313f1cfe0503a9916bba04a2da4a75406c7
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3 * utils.h:
5 * Contact:
6 * Moonlight List (moonlight-list@lists.ximian.com)
8 * Copyright 2007 Novell, Inc. (http://www.novell.com)
10 * See the LICENSE file included with the distribution for details.
14 #ifndef __UTILS_H__
15 #define __UTILS_H__
17 #include <glib.h>
18 #include <cairo.h>
19 #include <sys/types.h>
21 #include "downloader.h"
22 #include "zip/unzip.h"
24 G_BEGIN_DECLS
26 typedef gboolean (*Stream_CanSeek) (void *handle);
27 typedef gboolean (*Stream_CanRead) (void *handle);
28 typedef gint64 (*Stream_Length) (void *handle);
29 typedef gint64 (*Stream_Position) (void *handle);
30 typedef gint32 (*Stream_Read) (void *handle, void *buffer, gint32 offset, gint32 count);
31 typedef void (*Stream_Write) (void *handle, void *buffer, gint32 offset, gint32 count);
32 typedef void (*Stream_Seek) (void *handle, gint64 offset, gint32 origin);
33 typedef void (*Stream_Close) (void *handle);
35 struct ManagedStreamCallbacks {
36 void *handle;
37 Stream_CanSeek CanSeek;
38 Stream_CanRead CanRead;
39 Stream_Length Length;
40 Stream_Position Position;
41 Stream_Read Read;
42 Stream_Write Write;
43 Stream_Seek Seek;
44 Stream_Close Close;
47 /* @GeneratePInvoke */
48 gboolean managed_unzip_stream_to_stream (ManagedStreamCallbacks *source, ManagedStreamCallbacks *dest, const char *partname);
50 gboolean managed_unzip_extract_to_stream (unzFile zipFile, ManagedStreamCallbacks *dest);
52 G_GNUC_INTERNAL void g_ptr_array_insert (GPtrArray *array, guint index, void *item);
54 G_GNUC_INTERNAL void g_ptr_array_insert_sorted (GPtrArray *array, GCompareFunc cmp, void *item);
56 enum CanonMode {
57 CanonModeNone,
58 CanonModeXap,
59 CanonModeResource
62 const char *CanonicalizeFilename (char *filename, int n, CanonMode mode);
64 bool ExtractFile (unzFile zip, int fd);
65 bool ExtractAll (unzFile zip, const char *dir, CanonMode mode);
67 char *MakeTempDir (char *tmpdir);
68 char *CreateTempDir (const char *filename);
70 int RemoveDir (const char *dir);
72 int CopyFileTo (const char *filename, int fd);
74 int write_all (int fd, char *buf, size_t len);
76 cairo_t *measuring_context_create (void);
77 void measuring_context_destroy (cairo_t *cr);
79 GArray *double_garray_from_str (const char *s, gint max);
82 * Returns a pointer to the first token found.
84 * @input: the input string
85 * @c: output, upon return contains the first character not in the token.
86 * @end: output, upon return contains a pointer to the remaining input (NULL if no more data)
88 * Note: the input string is modified.
90 char *parse_rfc_1945_token (char *input, char *c, char **end);
93 * Returns a pointer to the unquoted string.
95 * @input: the input string
96 * @c: output, upon return contains the first character not in the quoted string.
97 * @end: output, upon return contains a pointer to the remaining input (NULL if no more data)
99 * Note: the input string is modified.
101 char *parse_rfc_1945_quoted_string (char *input, char *c, char **end);
103 G_END_DECLS
105 class TextStream {
106 protected:
107 char buffer[4096];
108 size_t buflen;
109 char *bufptr;
110 GIConv cd;
112 char *textbuf;
113 char *textbufptr;
114 int textbufsize;
116 int fd;
117 bool eof;
119 bool fmode;
121 bool ReadBOM (bool force);
122 ssize_t ReadInternal (char *buf, ssize_t n);
124 public:
125 TextStream ();
126 ~TextStream ();
128 bool OpenBuffer (const char *buf, int size);
129 bool OpenFile (const char *filename, bool force);
130 void Close ();
132 bool Eof ();
134 ssize_t Read (char *buf, size_t n);
137 typedef void (*CancelCallback) (gpointer user_data, void *context);
139 class Cancellable {
140 private:
141 CancelCallback cancel_cb;
142 Downloader *downloader;
143 void *context;
144 public:
145 Cancellable ();
146 ~Cancellable ();
148 void Cancel ();
149 void SetCancelFuncAndData (CancelCallback cb, Downloader *user_data, void *context);
152 #endif /* __UTILS_H__ */