implement ability to pipe in text (#3)
This commit is contained in:
		
							parent
							
								
									80d0f8c377
								
							
						
					
					
						commit
						a7b917d3e9
					
				
					 1 changed files with 14 additions and 9 deletions
				
			
		
							
								
								
									
										23
									
								
								src/main.zig
									
										
									
									
									
								
							
							
						
						
									
										23
									
								
								src/main.zig
									
										
									
									
									
								
							|  | @ -24,7 +24,7 @@ const LINES = 8; | ||||||
| // Device specifications | // Device specifications | ||||||
| const PAGES = 8; | const PAGES = 8; | ||||||
| 
 | 
 | ||||||
| var lines: [LINES]*const [:0]u8 = undefined; | var lines: [LINES]*[:0]u8 = undefined; | ||||||
| 
 | 
 | ||||||
| fn usage(args: [][]u8) !void { | fn usage(args: [][]u8) !void { | ||||||
|     const writer = std.io.getStdErr().writer(); |     const writer = std.io.getStdErr().writer(); | ||||||
|  | @ -53,25 +53,34 @@ pub fn main() !void { | ||||||
|     // from the file |     // from the file | ||||||
|     const stdin_file = std.io.getStdIn(); |     const stdin_file = std.io.getStdIn(); | ||||||
|     var stdin_data: [WIDTH * HEIGHT + 1]u8 = undefined; |     var stdin_data: [WIDTH * HEIGHT + 1]u8 = undefined; | ||||||
|  |     // Need this to support deallocation of memory | ||||||
|     var line_inx: usize = 0; |     var line_inx: usize = 0; | ||||||
|  |     var stdin_lines: [LINES][:0]u8 = undefined; | ||||||
|     defer { |     defer { | ||||||
|         for (0..line_inx) |i| { |         for (0..line_inx) |i| { | ||||||
|             alloc.free(@constCast(lines[i].*)); |             alloc.free(stdin_lines[i]); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  |     var nothing: [:0]u8 = @constCast(""); | ||||||
|  |     for (lines, 0..) |_, i| { | ||||||
|  |         lines[i] = ¬hing; | ||||||
|  |     } | ||||||
|     if (!stdin_file.isTty()) { |     if (!stdin_file.isTty()) { | ||||||
|         const read = try stdin_file.readAll(&stdin_data); |         const read = try stdin_file.readAll(&stdin_data); | ||||||
|         if (read == stdin_data.len) { |         if (read == stdin_data.len) { | ||||||
|             try std.io.getStdErr().writer().print("ERROR: data provided exceeds what can be sent to device!\n", .{}); |             try std.io.getStdErr().writer().print("ERROR: data provided exceeds what can be sent to device!\n", .{}); | ||||||
|             try usage(args); |             try usage(args); | ||||||
|         } |         } | ||||||
|         var it = std.mem.split(u8, &stdin_data, "\n"); |         var read_data = stdin_data[0..read]; | ||||||
|  |         var it = std.mem.split(u8, read_data, "\n"); | ||||||
|         while (it.next()) |line| { |         while (it.next()) |line| { | ||||||
|             const bwahahaha = try alloc.dupeZ(u8, line); |             if (line.len == 0) continue; | ||||||
|             lines[line_inx] = &bwahahaha; |             stdin_lines[line_inx] = try alloc.dupeZ(u8, line); | ||||||
|  |             lines[line_inx] = &stdin_lines[line_inx]; | ||||||
|             line_inx += 1; |             line_inx += 1; | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  |     std.debug.print("delme: {s}\n", .{lines[0].*}); | ||||||
|     const opts = try processArgs(alloc, args, &lines); |     const opts = try processArgs(alloc, args, &lines); | ||||||
|     defer alloc.destroy(opts); |     defer alloc.destroy(opts); | ||||||
|     if (opts.background_filename.len > 0) try stdout.print("Converting {s}\n", .{opts.background_filename}); |     if (opts.background_filename.len > 0) try stdout.print("Converting {s}\n", .{opts.background_filename}); | ||||||
|  | @ -94,10 +103,6 @@ fn processArgs(allocator: std.mem.Allocator, args: [][:0]u8, line_array: *[LINES | ||||||
|     if (!std.mem.eql(u8, opts.device_file, "-") and !std.mem.startsWith(u8, opts.device_file, prefix)) try usage(args); |     if (!std.mem.eql(u8, opts.device_file, "-") and !std.mem.startsWith(u8, opts.device_file, prefix)) try usage(args); | ||||||
| 
 | 
 | ||||||
|     opts.background_filename = @constCast(""); |     opts.background_filename = @constCast(""); | ||||||
|     const nothing: [:0]u8 = @constCast(""); |  | ||||||
|     for (line_array.*, 0..) |_, i| { |  | ||||||
|         line_array.*[i] = ¬hing; |  | ||||||
|     } |  | ||||||
|     var is_filename = false; |     var is_filename = false; | ||||||
|     var line_number: ?usize = null; |     var line_number: ?usize = null; | ||||||
|     for (args[1..], 1..) |arg, i| { |     for (args[1..], 1..) |arg, i| { | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		
		Reference in a new issue