Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
stream-rahul committed Jan 26, 2025
1 parent 991c97c commit 9c0a459
Show file tree
Hide file tree
Showing 19 changed files with 179 additions and 0 deletions.
Empty file removed src/lessons/lesson3_c.md
Empty file.
83 changes: 83 additions & 0 deletions src_cpp/cat.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#include <windows.h>
#include <stdio.h>
#include <tchar.h>

#define BUF_SIZE 512

static void CatFile(HANDLE hIn, HANDLE hOut);
static void ReportError(LPCTSTR msg, DWORD errCode, BOOL showErrMsg);

int _tmain(int argc, TCHAR* argv[]) {
HANDLE hIn, hStdIn = GetStdHandle(STD_INPUT_HANDLE);
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
BOOL dashS = FALSE;
int iFirstFile = 1;

/* Flow Step 1a: Parse Options */
for (int i = 1; i < argc; i++) {
if (_tcscmp(argv[i], _T("-s")) == 0) {
dashS = TRUE;
iFirstFile++;
}
else if (argv[i][0] == _T('-')) {
iFirstFile++;
}
else {
break;
}
}

/* Flow Step 2: Input Source Decision */
if (iFirstFile >= argc) {
CatFile(hStdIn, hStdOut);
return 0;
}

/* Flow Step 3: File Processing Loop */
for (int i = iFirstFile; i < argc; i++) {
hIn = CreateFile(argv[i], GENERIC_READ, 0, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

if (hIn == INVALID_HANDLE_VALUE) {
if (!dashS) ReportError(_T("File open error"), GetLastError(), TRUE);
continue;
}

CatFile(hIn, hStdOut);

DWORD err = GetLastError();
if (err != 0 && !dashS) {
ReportError(_T("Processing error"), err, TRUE);
}

CloseHandle(hIn);
}
return 0;
}

static void CatFile(HANDLE hIn, HANDLE hOut) {
BYTE buffer[BUF_SIZE];
DWORD nRead, nWritten;

while (ReadFile(hIn, buffer, BUF_SIZE, &nRead, NULL) && nRead > 0) {
if (!WriteFile(hOut, buffer, nRead, &nWritten, NULL) || nWritten != nRead) {
break;
}
}
}

static void ReportError(LPCTSTR msg, DWORD errCode, BOOL showErrMsg) {
_ftprintf(stderr, _T("ERROR: %s"), msg);

if (showErrMsg) {
LPVOID errMsg;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM,
NULL, errCode, 0,
(LPTSTR)&errMsg, 0, NULL);

_ftprintf(stderr, _T(" (%s)"), (LPTSTR)errMsg);
LocalFree(errMsg);
}
_ftprintf(stderr, _T("\n"));
}
Binary file added src_cpp/cat.ilk
Binary file not shown.
Binary file added src_cpp/cat.obj
Binary file not shown.
Binary file added src_cpp/pwd.ilk
Binary file not shown.
Binary file added src_cpp/pwd.obj
Binary file not shown.
10 changes: 10 additions & 0 deletions src_cpp/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <iostream>
#include <string>

int main() {
std::string name;
std::cout << "Enter your name: ";
std::getline(std::cin, name);
std::cout << "Hello, " << name << "!\n";
return 0;
}
Binary file added src_cpp/test.ilk
Binary file not shown.
Binary file added src_cpp/test.obj
Binary file not shown.
Binary file added src_rst/cat
Binary file not shown.
81 changes: 81 additions & 0 deletions src_rst/cat.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@

use std::env;
use std::ffi::OsString;
use std::fs::File;
use std::io::{self, Read, Write};

const BUF_SIZE: usize = 512;

fn main() {
let (dash_s, files) = parse_args();

if files.is_empty() {
cat_stream(&mut io::stdin(), &mut io::stdout(), dash_s);
return;
}

for path in files {
match File::open(&path) {
Ok(mut file) => {
if let Err(e) = cat_stream(&mut file, &mut io::stdout(), dash_s) {
if !dash_s {
eprintln!("Error processing file: {}: {}", path.to_string_lossy(), e);
}
}
}
Err(e) => {
if !dash_s {
eprintln!("Error opening file: {}: {}", path.to_string_lossy(), e);
}
}
}
}
}

fn parse_args() -> (bool, Vec<OsString>) {
let mut dash_s = false;
let mut files = vec![];

for arg in env::args_os().skip(1) {
if arg == "-s" {
dash_s = true;
} else if arg.to_str().map_or(false, |s| s.starts_with('-')) {
// Ignore other options
continue;
} else {
files.push(arg);
}
}

(dash_s, files)
}

fn cat_stream<R: Read, W: Write>(
input: &mut R,
output: &mut W,
suppress_errors: bool,
) -> io::Result<()> {
let mut buffer = [0u8; BUF_SIZE];

loop {
let bytes_read = match input.read(&mut buffer) {
Ok(0) => break, // EOF
Ok(n) => n,
Err(e) if suppress_errors => return Ok(()),
Err(e) => return Err(e),
};

let mut bytes_written = 0;
while bytes_written < bytes_read {
let write_result = output.write(&buffer[bytes_written..bytes_read]);

match write_result {
Ok(n) => bytes_written += n,
Err(e) if suppress_errors => return Ok(()),
Err(e) => return Err(e),
}
}
}

Ok(())
}
File renamed without changes.
2 changes: 2 additions & 0 deletions src_rst/file1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
File
1
2 changes: 2 additions & 0 deletions src_rst/file2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
File
2
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions src_rst/test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is a test file.

0 comments on commit 9c0a459

Please sign in to comment.