revert jeff's last commit since it breaks the build
[moon.git] / src / utils.h
blob74d84504bcbd5c3bb340b74e1f401ab67c044a66
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 /* @GeneratePInvoke */
51 gboolean managed_unzip_stream_to_stream_first_file (ManagedStreamCallbacks *source, ManagedStreamCallbacks *dest);
53 gboolean managed_unzip_extract_to_stream (unzFile zipFile, ManagedStreamCallbacks *dest);
55 G_GNUC_INTERNAL void g_ptr_array_insert (GPtrArray *array, guint index, void *item);
57 G_GNUC_INTERNAL void g_ptr_array_insert_sorted (GPtrArray *array, GCompareFunc cmp, void *item);
59 enum CanonMode {
60 CanonModeNone,
61 CanonModeXap,
62 CanonModeResource
65 const char *CanonicalizeFilename (char *filename, int n, CanonMode mode);
67 bool ExtractFile (unzFile zip, int fd);
68 bool ExtractAll (unzFile zip, const char *dir, CanonMode mode);
70 char *MakeTempDir (char *tmpdir);
71 char *CreateTempDir (const char *filename);
73 int RemoveDir (const char *dir);
75 int CopyFileTo (const char *filename, int fd);
77 int write_all (int fd, char *buf, size_t len);
79 cairo_t *measuring_context_create (void);
80 void measuring_context_destroy (cairo_t *cr);
82 GArray *double_garray_from_str (const char *s, gint max);
85 * Returns a pointer to the first token found.
87 * @input: the input string
88 * @c: output, upon return contains the first character not in the token.
89 * @end: output, upon return contains a pointer to the remaining input (NULL if no more data)
91 * Note: the input string is modified.
93 char *parse_rfc_1945_token (char *input, char *c, char **end);
96 * Returns a pointer to the unquoted string.
98 * @input: the input string
99 * @c: output, upon return contains the first character not in the quoted string.
100 * @end: output, upon return contains a pointer to the remaining input (NULL if no more data)
102 * Note: the input string is modified.
104 char *parse_rfc_1945_quoted_string (char *input, char *c, char **end);
106 G_END_DECLS
108 class TextStream {
109 protected:
110 char buffer[4096];
111 size_t buflen;
112 char *bufptr;
113 GIConv cd;
115 char *textbuf;
116 char *textbufptr;
117 int textbufsize;
119 int fd;
120 bool eof;
122 bool fmode;
124 bool ReadBOM (bool force);
125 ssize_t ReadInternal (char *buf, ssize_t n);
127 public:
128 TextStream ();
129 ~TextStream ();
131 bool OpenBuffer (const char *buf, int size);
132 bool OpenFile (const char *filename, bool force);
133 void Close ();
135 bool Eof ();
137 ssize_t Read (char *buf, size_t n);
140 typedef void (*CancelCallback) (gpointer user_data, void *context);
142 class Cancellable {
143 private:
144 CancelCallback cancel_cb;
145 Downloader *downloader;
146 void *context;
147 public:
148 Cancellable ();
149 ~Cancellable ();
151 void Cancel ();
152 void SetCancelFuncAndData (CancelCallback cb, Downloader *user_data, void *context);
155 #endif /* __UTILS_H__ */