8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / audio / utilities / AudioRawPipe.cc
blobe95580589253dda97a6590dc0061a8939fed2a5f
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
20 * CDDL HEADER END
23 * Copyright 1991-2003 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <sys/stat.h>
33 #include <AudioRawPipe.h>
34 #include <libaudio.h>
35 #include <audio_hdr.h>
37 // class AudioPipe methods
39 // Constructor with file descriptor, mode, and optional name
40 AudioRawPipe::
41 AudioRawPipe(
42 const int desc, // file descriptor
43 const FileAccess acc, // access mode
44 const AudioHdr& hdr_local, // header
45 const char *name_local, // name
46 const off_t off // offset
47 ):AudioPipe(desc, acc, name_local), offset(off)
49 isopened = FALSE;
50 setfd(desc);
51 SetHeader(hdr_local);
54 // The create routine for pipes writes a file header
55 AudioError AudioRawPipe::
56 Create()
58 AudioError err;
60 // Was the header properly set?
61 err = GetHeader().Validate();
62 if (err != AUDIO_SUCCESS)
63 return (RaiseError(err));
65 // Open fd supplied by constructor
66 if (!isfdset() || opened()) {
67 return (RaiseError(AUDIO_ERR_NOEFFECT, Warning));
70 // set flag for opened() test
71 isopened = TRUE;
73 // Set the actual output length to zero
74 setlength(0.);
76 return (AUDIO_SUCCESS);
79 // The open routine for raw pipes validates the header and
80 // init's the read pos to offset and sets the opened flag.
81 AudioError AudioRawPipe::
82 Open()
84 AudioError err;
85 struct stat st;
87 // The constructor should have supplied a valid fd
88 // If fd is not open, or file header already decoded, skip it
89 if (!isfdset() || opened())
90 return (RaiseError(AUDIO_ERR_NOEFFECT, Warning));
92 // Stat the file, to see if it is a regular file
93 if (fstat(getfd(), &st) < 0)
94 return (RaiseError(AUDIO_UNIXERROR));
96 // check validity of file header
97 err = GetHeader().Validate();
98 if (err != AUDIO_SUCCESS) {
99 (void) close(getfd());
100 setfd(-1);
101 return (err);
104 // Only trust the file size for regular files
105 if (S_ISREG(st.st_mode)) {
106 // for raw files - no hdr, so it's the whole file minus
107 // the offset.
108 setlength(GetHeader().Bytes_to_Time(st.st_size - offset));
109 } else {
110 // don't know ...
111 setlength(AUDIO_UNKNOWN_TIME);
114 // set flag for opened() test
115 isopened = TRUE;
117 err = SetOffset(offset);
119 // reset logical position to 0.0, since this is, in effect,
120 // the beginning of the file.
121 SetReadPosition(0.0, Absolute);
123 return (err);
126 Boolean AudioRawPipe::
127 opened() const
129 return (isopened);
132 AudioError AudioRawPipe::
133 SetOffset(off_t val)
135 off_t setting = 0;
136 AudioError err;
138 // only read only files for now
139 if (GetAccess().Writeable()) {
140 return (AUDIO_ERR_NOEFFECT);
143 // only allow this if we haven't read anything yet (i.e. current
144 // position is 0).
145 if (ReadPosition() != 0.) {
146 return (AUDIO_ERR_NOEFFECT);
149 if ((err = seekread(GetHeader().Bytes_to_Time(val), setting))
150 != AUDIO_SUCCESS) {
151 return (err);
154 // this should *never* happen 'cause seekread just sets setting
155 // to GetHeader().Time_to_Bytes....
156 if (setting != val) {
157 // don't really know what error is apropos for this.
158 return (AUDIO_ERR_BADFRAME);
161 offset = val;
162 return (AUDIO_SUCCESS);
165 off_t AudioRawPipe::
166 GetOffset() const
168 return (offset);