8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / audio / utilities / AudioPipe.cc
blob18b1508216d1fddc5c19b11c03d6a12273652605
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 (c) 1990-2001 by Sun Microsystems, Inc.
24 * All rights reserved.
27 #pragma ident "%Z%%M% %I% %E% SMI"
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <AudioPipe.h>
33 // class AudioPipe methods
36 // Constructor with file descriptor, mode, and optional name
37 AudioPipe::
38 AudioPipe(
39 const int desc, // file descriptor
40 const FileAccess acc, // access mode
41 const char *name_local): // name
42 AudioUnixfile(name_local, acc)
44 setfd(desc);
47 // The create routine for pipes writes a file header
48 AudioError AudioPipe::
49 Create()
51 AudioError err;
53 // Was the header properly set?
54 err = GetHeader().Validate();
55 if (err != AUDIO_SUCCESS)
56 return (RaiseError(err));
58 // Open fd supplied by constructor
59 if (!isfdset())
60 return (RaiseError(AUDIO_ERR_NOEFFECT));
62 // Write the file header with current (usually unknown) size
63 err = encode_filehdr();
64 if (err != AUDIO_SUCCESS) {
65 (void) close(getfd()); // If error, remove file
66 setfd(-1);
67 return (err);
70 // Set the actual output length to zero
71 setlength(0.);
73 return (AUDIO_SUCCESS);
76 // The open routine for pipes decodes the header
77 AudioError AudioPipe::
78 Open()
80 AudioError err;
82 // The constructor should have supplied a valid fd
83 if (!isfdset())
84 return (RaiseError(AUDIO_ERR_NOEFFECT));
86 // Decode a file header
87 err = decode_filehdr();
88 if (err != AUDIO_SUCCESS) {
89 (void) close(getfd());
90 setfd(-1);
91 return (err);
94 return (AUDIO_SUCCESS);
97 // Read data from underlying pipe into specified buffer.
98 // No data format translation takes place.
99 // Since there's no going back, the object's read position pointer is updated.
100 AudioError AudioPipe::
101 ReadData(
102 void* buf, // destination buffer address
103 size_t& len, // buffer length (updated)
104 Double& pos) // start position (updated)
106 AudioError err;
107 char *tbuf; // current buffer pointer
108 size_t remain; // number of bytes remaining
109 size_t cnt; // accumulated number of bytes read
111 tbuf = (char *)buf;
112 remain = len;
113 cnt = 0;
115 // Pipes return short reads. If non-blocking i/o, try to read all.
116 do {
117 // Call the real routine
118 err = AudioUnixfile::ReadData((void*)tbuf, remain, pos);
120 // Update the object's read position
121 if (!err) {
122 (void) SetReadPosition(pos, Absolute);
123 if (remain == 0)
124 break;
125 cnt += remain;
126 tbuf += remain;
127 remain = len - cnt;
129 } while (!err && (remain > 0) && GetBlocking());
130 len = cnt;
131 if (len > 0)
132 return (AUDIO_SUCCESS);
133 return (err);
136 // Write data to underlying file from specified buffer.
137 // No data format translation takes place.
138 // Since there's no going back, the object's write position pointer is updated.
139 AudioError AudioPipe::
140 WriteData(
141 void* buf, // source buffer address
142 size_t& len, // buffer length (updated)
143 Double& pos) // start position (updated)
145 AudioError err;
147 // Call the real routine
148 err = AudioUnixfile::WriteData(buf, len, pos);
150 // Update the object's write position
151 if (err == AUDIO_SUCCESS)
152 (void) SetWritePosition(pos, Absolute);
153 return (err);