?? gsegyfileaccessor.c
字號:
guint8 *trace_buffer;
if (double_buffer)
data = (gfloat*)g_try_malloc (2 * private->number_of_samples * traces_num * sizeof (gfloat));
else
data = (gfloat*)g_try_malloc (private->number_of_samples * (traces_num + 1) * sizeof (gfloat));
if (NULL == data) {
if (file_error)
file_error->id = G_SEGY_FILE_MEMORY_ALLOCATION_FAIL;
return NULL;
}
trace_buffer = (guint8*)&data[private->number_of_samples * traces_num];
fd = g_io_channel_unix_get_fd (private->io_channel);
#if defined LINUX || defined WIN32
if (private->file_mutex)
g_mutex_lock (private->file_mutex);
#endif
for (i = 0; i < traces_num; i++) {
#if defined LINUX || defined WIN32
seek_pos = lseek (fd, g_array_index (private->trace_positions, guint64, traces_indices[i]), SEEK_SET);
if (seek_pos == (guint64)LSEEK_ERR) {
#else
status = g_io_channel_seek_position (private->io_channel,
g_array_index (private->trace_positions, guint64, traces_indices[i]),
G_SEEK_SET, &gerror);
if (status != G_IO_STATUS_NORMAL) {
#endif
if (file_error) {
if (status == G_IO_STATUS_EOF)
file_error->id = G_SEGY_FILE_PREMATURE_EOF;
else
file_error->id = G_SEGY_FILE_READ_ERROR;
file_error->gerror = gerror;
}
break;
}
#if defined LINUX || defined WIN32
bytes_read = read (fd, trace_buffer, private->trace_header_size);
if (bytes_read != private->trace_header_size) {
if (bytes_read && file_error) {
file_error->id = G_SEGY_FILE_PREMATURE_EOF;
#else
status = g_io_channel_read_chars (private->io_channel, trace_buffer, private->trace_header_size,
&bytes_read, &gerror);
if (status != G_IO_STATUS_NORMAL || bytes_read != private->trace_header_size) {
if (file_error) {
if (status == G_IO_STATUS_EOF && bytes_read != 0)
file_error->id = G_SEGY_FILE_PREMATURE_EOF;
else
file_error->id = G_SEGY_FILE_READ_ERROR;
file_error->gerror = gerror;
#endif
}
break;
}
g_segy_format_wizard_get_number_of_samples (private->format_wizard, trace_buffer, &number_of_samples);
samples_to_read = number_of_samples > private->number_of_samples || 0 == number_of_samples
? private->number_of_samples : number_of_samples;
bytes_to_read = samples_to_read * private->sample_size;
#if defined LINUX || defined WIN32
bytes_read = read (fd, trace_buffer, bytes_to_read);
if (bytes_read != bytes_to_read) {
if (file_error)
file_error->id = G_SEGY_FILE_READ_ERROR;
#else
status = g_io_channel_read_chars (private->io_channel, trace_buffer, bytes_to_read,
&bytes_read, &gerror);
if (status != G_IO_STATUS_NORMAL || bytes_read != bytes_to_read) {
if (file_error) {
if (status == G_IO_STATUS_EOF && bytes_read != 0)
file_error->id = G_SEGY_FILE_PREMATURE_EOF;
else
file_error->id = G_SEGY_FILE_READ_ERROR;
file_error->gerror = gerror;
}
#endif
break;
}
g_segy_format_wizard_decode_trace (private->format_wizard, private->sample_id, samples_to_read,
trace_buffer, &data[trace_num * private->number_of_samples]);
if (samples_to_read < private->number_of_samples) {
for (j = samples_to_read; j < private->number_of_samples; j++)
data[trace_num * private->number_of_samples + j] = 0;
}
trace_num++;
}
if (0 == trace_num) {
g_free (data);
return NULL;
}
#if defined LINUX || defined WIN32
if (private->file_mutex)
g_mutex_unlock (private->file_mutex);
#endif
if (slow_num)
*slow_num = trace_num;
if (fast_num)
*fast_num = private->number_of_samples;
return data;
}
static void g_segy_file_accessor_init (GSEGYFileAccessor *self) {
GSEGYFileAccessorPrivate *private = G_SEGY_FILE_ACCESSOR_GET_PRIVATE (self);
private->format_wizard = NULL;
private->io_channel = NULL;
private->format_wizard = NULL;
private->ebcdic_header = NULL;
private->binary_header = NULL;
private->sorting_contents = NULL;
private->trace_positions = NULL;
private->trace_buffer = NULL;
private->trace_buffer_size = 0;
private->sorting_data_buffer = NULL;
if (g_thread_supported ())
private->file_mutex = g_mutex_new ();
#ifdef DEBUG
g_print ("<GSEGYFileAccessor is inited>\n");
#endif
}
static void g_segy_file_accessor_finalize (GObject *object) {
GSEGYFileAccessorPrivate *private = G_SEGY_FILE_ACCESSOR_GET_PRIVATE (G_SEGY_FILE_ACCESSOR (object));
if (private->format_wizard)
g_object_unref (G_OBJECT (private->format_wizard));
if (private->io_channel)
g_io_channel_unref (private->io_channel);
if (private->ebcdic_header_size)
g_free (private->ebcdic_header);
if (private->binary_header_size)
g_free (private->binary_header);
if (private->sorting_contents)
g_array_free (private->sorting_contents, TRUE);
if (private->trace_positions)
g_array_free (private->trace_positions, TRUE);
if (private->trace_buffer)
g_free (private->trace_buffer);
if (private->sorting_data_buffer)
g_free (private->sorting_data_buffer);
if (private->file_mutex)
g_mutex_free (private->file_mutex);
#ifdef DEBUG
g_print ("<GSEGYFileAccessor is finalized>\n");
#endif
if (G_OBJECT_CLASS (g_segy_file_accessor_parent_class)->finalize)
G_OBJECT_CLASS (g_segy_file_accessor_parent_class)->finalize (object);
}
enum {
PROP_0,
PROP_IO_CHANNEL,
PROP_FORMAT_WIZARD
};
static void g_segy_file_accessor_set_property (GObject *object, guint prop_id,
const GValue *value, GParamSpec *pspec) {
GSEGYFileAccessorPrivate *private = G_SEGY_FILE_ACCESSOR_GET_PRIVATE (G_SEGY_FILE_ACCESSOR (object));
switch (prop_id) {
case PROP_IO_CHANNEL: {
GIOChannel *io_channel = (GIOChannel*)g_value_get_pointer (value);
private->io_channel = io_channel;
g_io_channel_ref (private->io_channel);
if (private->format_wizard)
g_segy_file_accessor_setup (G_SEGY_FILE_ACCESSOR (object));
g_object_notify (object, "io_channel");
}
break;
case PROP_FORMAT_WIZARD: {
GSEGYFormatWizard *format_wizard = G_SEGY_FORMAT_WIZARD (g_value_get_object (value));
private->format_wizard = format_wizard;
g_object_ref (G_OBJECT (private->format_wizard));
if (private->io_channel)
g_segy_file_accessor_setup (G_SEGY_FILE_ACCESSOR (object));
g_object_notify (object, "format_wizard");
}
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void g_segy_file_accessor_get_property (GObject *object, guint prop_id,
GValue *value, GParamSpec *pspec) {
GSEGYFileAccessorPrivate *private = G_SEGY_FILE_ACCESSOR_GET_PRIVATE (G_SEGY_FILE_ACCESSOR (object));
switch (prop_id) {
case PROP_IO_CHANNEL:
g_value_set_pointer (value, private->io_channel);
break;
case PROP_FORMAT_WIZARD:
g_value_set_object (value, private->format_wizard);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void g_segy_file_accessor_class_init (GSEGYFileAccessorClass *klass) {
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
g_type_class_add_private (klass, sizeof (GSEGYFileAccessorPrivate));
gobject_class->finalize = (GObjectFinalizeFunc) g_segy_file_accessor_finalize;
gobject_class->set_property = g_segy_file_accessor_set_property;
gobject_class->get_property = g_segy_file_accessor_get_property;
klass->scan_fraction = NULL;
klass->scan_fraction_id = g_signal_new ("scan_fraction",
G_TYPE_FROM_CLASS ((gpointer)G_OBJECT_CLASS (klass)),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (GSEGYFileAccessorClass, scan_fraction),
NULL, NULL,
g_segy_marshal_BOOLEAN__FLOAT_BOXED,
G_TYPE_BOOLEAN, 2,
G_TYPE_FLOAT, G_TYPE_POINTER);
g_object_class_install_property (gobject_class,
PROP_IO_CHANNEL,
g_param_spec_pointer ("io_channel", "IOChannel",
"System object describing opened files",
G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
g_object_class_install_property (gobject_class,
PROP_FORMAT_WIZARD,
g_param_spec_object ("format_wizard", "FormatWizard",
"Object responsible for the knowledge of how to read opened files",
G_SEGY_TYPE_FORMAT_WIZARD,
G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
#ifdef DEBUG
g_print ("<GSEGYFileAccessor class is inited>\n");
#endif
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -